黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

MySQL數(shù)據(jù)庫(kù)概述及數(shù)據(jù)準(zhǔn)備
MySQL數(shù)據(jù)庫(kù)常用命令
MySQL數(shù)據(jù)庫(kù)查看表結(jié)構(gòu)
MySQL查詢字段
MySQL條件查詢
MySQL排序
MySQL函數(shù)
MySQL分組函數(shù)/聚合函數(shù)/多行處理函數(shù)
MySQL分組查詢
MySQL連接查詢
MySQL子查詢
MySQL UNION
MySQL中l(wèi)imit的用法
MySQL表
MySQL存儲(chǔ)引擎
MySQL事務(wù)
MySQL索引
MySQL視圖
MySQL DBA命令
MySQL數(shù)據(jù)庫(kù)設(shè)計(jì)的三大范式
MySQL數(shù)據(jù)庫(kù)練習(xí)題

MySQL創(chuàng)建表并添加約束

常見的約束

● 非空約束,not null
● 唯一約束,unique 
● 主鍵約束,primary key
● 外鍵約束,foreign key
● 自定義檢查約束,check(不建議使用)(在mysql中現(xiàn)在還不支持)

非空約束,not null

非空約束,針對(duì)某個(gè)字段設(shè)置其值不為空,如:學(xué)生的姓名不能為空。

drop table if exists t_student; 
create table t_student(
	student_id  	int(10),
	student_name 	varchar(20) not null,
	sex		char(2)  default  'm',
	birthday	date, 
	email		varchar(30),
	classes_id	int(3)	
)

insert into t_student(student_id, birthday, email, classes_id) 
values
(1002, '1988-01-01', 'qqq@163.com', 10)

以上錯(cuò)誤為加入的學(xué)生姓名為空。 

唯一約束,unique

唯一性約束,它可以使某個(gè)字段的值不能重復(fù),如:email不能重復(fù):

drop table if exists t_student; 
create table t_student(
	student_id  	int(10),
	student_name 	varchar(20) not null,
	sex		char(2)  default  'm',
	birthday	date, 
	email		varchar(30)  unique,
	classes_id	int(3)	
)
insert into t_student(student_id, student_name , sex, birthday, email, classes_id) 
values
(1001,'zhangsan','m', '1988-01-01', 'qqq@163.com', 10)

以上插入了重復(fù)的email,所以出現(xiàn)了“違反唯一約束錯(cuò)誤”,所以u(píng)nique起作用了同樣可以為唯一約束起個(gè)約束名;

我們可以查看一下約束

mysql> use information_schema;

mysql> select * from table_constraints where table_name = 't_student';

關(guān)于約束名稱可以到table_constraints中查詢

以上約束的名稱我們也可以自定義。

drop table if exists t_student; 
create table t_student(
	student_id  	int(10),
	student_name 	varchar(20) not null,
	sex		char(2)  default  'm',
	birthday	date, 
	email		varchar(30)  ,
	classes_id	int(3)	,
constraint email_unique unique(email)/*表級(jí)約束*/
)

主鍵約束,primary key

每個(gè)表應(yīng)該具有主鍵,主鍵可以標(biāo)識(shí)記錄的唯一性,主鍵分為單一主鍵和復(fù)合(聯(lián)合)主鍵,單一主鍵是由一個(gè)字段構(gòu)成的,復(fù)合(聯(lián)合)主鍵是由多個(gè)字段構(gòu)成的。

drop table if exists t_student; 
create table t_student()
	student_id  	int(10)  primary key,/*列級(jí)約束*/
	student_name 	varchar(20) not null,
	sex		char(2)  default  'm',
	birthday	date, 
	email		varchar(30)  ,
	classes_id	int(3)	
)
insert into t_student(student_id, student_name , sex, birthday, email, classes_id) 
values
(1001,'zhangsan','m', '1988-01-01', 'qqq@163.com', 10)

向以上表中加入學(xué)號(hào)為1001的兩條記錄,出現(xiàn)如下錯(cuò)誤,因?yàn)榧尤肓酥麈I約束

我們也可以通過表級(jí)約束為約束起個(gè)名稱:

drop table if exists t_student; 
create table t_student(
	student_id  	int(10),
	student_name 	varchar(20) not null,
	sex		char(2)  default  'm',
	birthday	date, 
	email		varchar(30)  ,
	classes_id	int(3),
    CONSTRAINT p_id PRIMARY key (student_id)
)
insert into t_student(student_id, student_name , sex, birthday, email, classes_id) 
values
(1001,'zhangsan','m', '1988-01-01', 'qqq@163.com', 10)

外鍵約束,foreign  key

外鍵主要是維護(hù)表之間的關(guān)系的,主要是為了保證參照完整性,如果表中的某個(gè)字段為外鍵字段,那么該字段的值必須來源于參照的表的主鍵,如:emp中的deptno值必須來源于dept表中的deptno字段值。

建立學(xué)生和班級(jí)表之間的連接

首先建立班級(jí)表t_classes

drop table if exists t_classes;
create table t_classes(
	classes_id int(3),
	classes_name varchar(40),
	constraint pk_classes_id primary key(classes_id)
)

在t_student中加入外鍵約束

drop table if exists t_student;
create table t_student(
	student_id  	int(10),
	student_name 	varchar(20),
	sex		char(2),
	birthday	date,
	email		varchar(30),
	classes_id	int(3),
	constraint      student_id_pk primary key(student_id),
constraint	fk_classes_id foreign key(classes_id) references t_classes(classes_id)	   
)

向t_student中加入數(shù)據(jù)

insert into t_student(student_id, student_name, sex, birthday, email, classes_id) values(1001, 'zhangsan', 'm', '1988-01-01', 'qqq@163.com', 10)

出現(xiàn)錯(cuò)誤,因?yàn)樵诎嗉?jí)表中不存在班級(jí)編號(hào)為10班級(jí),外鍵約束起到了作用。

存在外鍵的表就是子表,參照的表就是父表,所以存在一個(gè)父子關(guān)系,也就是主從關(guān)系,主表就是班級(jí)表,從表就是學(xué)生表。

以上成功的插入了學(xué)生信息,當(dāng)時(shí)classes_id沒有值,這樣會(huì)影響參照完整性,所以我們建議將外鍵字段設(shè)置為非空。

drop table if exists t_student;
create table t_student(
	student_id  	int(10),
	student_name 	varchar(20),
	sex		char(2),
	birthday	date,
	email		varchar(30),
	classes_id	int (3) not null,
	constraint      student_id_pk primary key(student_id),
	constraint	fk_classes_id foreign key(classes_id) references t_classes(classes_id)	     
)
insert into t_student(student_id, student_name, sex, birthday, email, cla
sses_id) values(1001, 'zhangsan', 'm', '1988-01-01', 'qqq@163.com', null);

再次插入班級(jí)編號(hào)為null的數(shù)據(jù)

添加數(shù)據(jù)到班級(jí)表,添加數(shù)據(jù)到學(xué)生表,刪除班級(jí)數(shù)據(jù),將會(huì)出現(xiàn)如下錯(cuò)誤:

insert into t_classes (classes_id,classes_name) values (10,'366');

insert into t_student(
student_id, student_name, sex, birthday, email, classes_id
) values(
1001, 'zhangsan', 'm', '1988-01-01', 'qqq@163.com', 10
)

mysql> update t_classes set  classes_id = 20 where classes_name = '366';

因?yàn)樽颖?t_student)存在一個(gè)外鍵classes_id,它參照了父表(t_classes)中的主鍵,所以先刪除子表中的引用記錄,再修改父表中的數(shù)據(jù)。
我們也可以采取以下措施 級(jí)聯(lián)更新。

mysql> delete from t_classes where classes_id = 10;

因?yàn)樽颖?t_student)存在一個(gè)外鍵classes_id,它參照了父表(t_classes)中的主鍵,所以先刪除父表,那么將會(huì)影響子表的參照完整性,所以正確的做法是,先刪除子表中的數(shù)據(jù),再刪除父表中的數(shù)據(jù),采用drop table也不行,必須先drop子表,再drop父表。

我們也可以采取以下措施 級(jí)聯(lián)刪除。
級(jí)聯(lián)更新與級(jí)聯(lián)刪除
● on update cascade;

mysql對(duì)有些約束的修改比較麻煩,所以我們可以先刪除,再添加

alter table t_student drop foreign key fk_classes_id;

alter table t_student add constraint fk_classes_id_1 foreign key(classes_id) references t_classes(classes_id) ?on update cascade;

我們只修改了父表中的數(shù)據(jù),但是子表中的數(shù)據(jù)也會(huì)跟著變動(dòng)。

● on delete cascade; 

mysql對(duì)有些約束的修改時(shí)不支持的,所以我們可以先刪除,再添加

alter table t_student drop foreign key fk_classes_id;

alter table t_student add constraint fk_classes_id_1 foreign key(classes_id) references t_classes(classes_id) ?on delete cascade;
delete from t_classes where classes_id = 20;

全部教程
主站蜘蛛池模板: 天天操夜夜夜 | 成年人福利 | 天堂va亚洲va欧美va国产 | 免费黄色欧美 | 国产日皮 | 狠狠色成人综合 | 成人福利在线观看免费视频 | 特级夫妻大片免费在线播放 | 成人欧美日本免费观看 | 亚洲日本va午夜中文字幕一区 | 人人舔人人插 | 一级黄色免费网站 | 亚洲三级视频 | 老司机深夜福利影院 | 91精品国产综合成人 | 国产精品嫩草影院在线观看免费 | 免费观看的黄色网址 | 国产二区精品视频 | 婷婷国产天堂久久综合五月 | 性欧美成人免费观看视 | www大片| 欧美三级日韩 | 欧美日韩亚洲综合在线一区二区 | 免费无毒片在线观看 | av成人在线播放 | 成人网18免费网站在线 | 成人在线精品 | 中国一级全黄的免费观看 | 国产制服丝袜在线观看 | 又黄又爽又色的视频在线看 | 真人一级一级特黄高清毛片 | 久草欧美视频 | 高清一级做a爱过程不卡视频 | 激情欧美一区二区三区 | youjizzxxxx18日本 yy3341殇情影院理论片 | 欧美成人一级 | 91成人在线观看 | 天天看天天摸色天天综合网 | 欧美亚洲高清日韩成人 | 18以下勿进色禁网站 | 中文字幕天天躁日日躁狠狠躁免费 |