MySQL簡單使用例項

2021-10-10 23:18:08 字數 2885 閱讀 3682

1)建立學生表student

要求:

(1)屬性包括學號(sno)、學生姓名(sname)、年齡(age)、性別(***)、所在系(dept);

(2)學號為表的主碼;

(3)姓名非空;

(4)年齡預設值為18;

(5)性別男或女;

2)建立課程表course (cno, cname, credit, teacher, pcno)

要求:

(1)屬性包括課程號(cno)、課程名稱(cname)、學分(credit)、任課老師(teacher)、選修課(pcno);

(2)課程號為表的主碼;

(3)課程名非空;

(4)學分型別為實數;

(5)選修課為外碼,參考屬性為本表的課程號;

3)建立選課表sc

要求:

(1)該錶屬性包括學號(sno)、課程號(cno)、成績(score);

(2)學號與課程號聯合作為表的主碼;

(3)學號參照student表;

(4)課程號參照course表;

(5)成績在0和100之間;

#建立乙個名為ss的資料庫

create

database ss;

#使用該資料庫

use ss;
#建立學生表student

create

table student(

sno char(10

)primary

key,

sname char(20

)not

null

, s*** char(2

)check

(s*** in

('男'

,'女'))

, sage smallint(20

),sdept char(20));

#建立課程表course

create

table course(

cno char(10

)primary

key,

cname char(20

)not

null

, credit smallint

, teacher char(20

),pcon char(20

),foreign

key(pcon)

references course(cno)

);

#建立選課表sc

create

table sc(

sno char(10

),cno char(10

),primary

key(sno,cno)

, score smallint

check

(score>=

0and score<=

100)

,foreign

key(sno)

references student(sno)

,foreign

key(cno)

references course(cno)

);

#顯示建立的表

#顯示表的結構

與約束相關操作

1.主鍵約束

新增:alter table table_name add primary key (字段)

刪除:alter table table_name drop primary key

2.非空約束

新增:alter table table_name modify 列名 資料型別 not null

刪除:alter table table_name modify 列名 資料型別 null

3.唯一約束

新增:alter table table_name add unique 約束名(字段)

刪除:alter table table_name drop key 約束名

4.自動增長

新增:alter table table_name modify 列名 int auto_increment

刪除:alter table table_name modify 列名 int

5.外來鍵約束

新增:alter table table_name add constraint 約束名 foreign key(外來鍵列)

references 主鍵表(主鍵列)

刪除:第一步:刪除外來鍵

alter table table_name drop foreign key 約束名

第二步:刪除索引

alter table table_name drop index 索引名

約束名和索引名一樣

6.預設值

新增:alter table table_name alter 列名 set default 『值』

刪除:alter table table_name alter 列名 drop default

mysql 使用例項 MySQL使用例項

誤刪除了vps上的phpmyadmin,不得已翻閱了半天mysql指南,以下是一些mysql使用例項 連線管理 鏈結資料庫 mysql h localhost u root p 退出資料庫 mysql quit 資料庫管理 檢視資料庫 mysql show databases 建立資料庫 mysql...

MySQL使用例項

使用資料庫 use test 建表 student create table student id int,named varchar 20 not null comment 學生姓名 chinese float default 0 english float default 0 math floa...

Detou簡單使用例項

開啟vs2008 新建 專案 win32專案 輸入detourhook 確定 下一步 windows應用程式 其它預設 完成。把下面 完全拷貝到detourhook.cpp中,detourhook.cpp中原來 完全刪除。編譯。通過。有警告忽略處理。include stdafx.h include ...