MySQL筆記--約束
阿新 • • 發佈:2020-07-15
MySQL筆記--約束
/** 約束 */ show databases; use db_26_homework; show tables; select sname, sex from stu; /** 1 預設約束 default(如果沒有賦值,設定指定的值) */ create table student ( sid int, sname varchar(4) default '無名' # 預設約束 ); insert into student (sid) values (2); # 不寫,是 預設值 select * from student; /** 非空約束 not null (不能為 null) */ create table student1 ( sid int, sname varchar(4) default '無名', sex char(1) not null # 非空約束 ); insert into student1 (sid, sname) values (1, 'he'); # [HY000][1364] Field 'sex' doesn't have a default value insert into student1 values (3, 'h3h', null); # [23000][1048] Column 'sex' cannot be null select * from student1; /** 唯一約束 unique (如果值不為 null,值不能重複) */ drop table student2; create table student2 ( sid int, sname varchar(4) default '無名', sex char(1) not null, sorder int unique # 唯一約束 ); insert into student2 (sid, sex, sorder) values (1, '男', 1); # [23000][1062] Duplicate entry '1' for key 'student2.sorder' # 重複的 key 1 insert into student2 (sid, sex, sorder) values (1, '男', null); # 可以為 null,可以為多個 null /** 檢查約束 check ( 對列的取值範圍設定 ) ! MsSQL 不支援 */ /** 主鍵約束(不能重複,不能為 null,可以被引用) */ create table student3 ( sid int primary key, sname varchar(11) ); insert into student3 (sid, sname) values (1, '韓寒'); # [23000][1062] Duplicate entry '1' for key 'student3.PRIMARY' # 主鍵約束 不能重複 insert into student3 (sid, sname) values (null, '張三'); # [23000][1048] Column 'sid' cannot be null # 主鍵不能為 null insert into student3 (sname) values ('里斯'); # [HY000][1364] Field 'sid' doesn't have a default value # sid 沒有預設值 desc student3; /** 主鍵自增約束 auto_increment 特點1 不賦值、值為 null,值自動增長 特點2 自動自增 = 最大值 + 1 */ create table student4 ( sid float(4, 1) primary key auto_increment, # 只能用於數字型別的主鍵列 sname varchar(11) ); insert into student4 (sname) values ('張三'); # 不賦值,或者為 null,值自動增長 insert into student4 (sid, sname) values (11, '里斯'); # 自動增長 insert into student4 (sid, sname) values (null, '王武'); # 設定 為 null,自動增長 select * from student4; /** 建立表後操作約束 新增 */ create table student5 ( sid int, sname varchar(11) ); desc student5; alter table student5 change sanme sname varchar(11) default '無名'; # 新增預設約束 alter table student5 change sid sid int unique; # 新增 唯一約束 alter table student5 change sid sid int not null; # 新增 非空約束 alter table student5 change sid sid int primary key; # 新增主鍵約束 alter table student5 change sid sid int auto_increment; # 新增 自增長約束 alter table student5 modify sid int auto_increment; # 新增 自增長約束 desc student5; /** 刪除普通約束 not null/default/default */ create table student6 ( sid int primary key default, snmae varchar(11) not null unique, sex char(1) default '女' ); desc student6; alter table student6 change sex sex char(1); # 刪除約束 alter table student6 modify snmae varchar(11); # 刪除約束 desc student6; insert into student6 values (2, '11', '女'); # [23000][1062] Duplicate entry '11' for key 'student6.snmae' /** 刪除 鍵約束 */ desc student6; show create table student6; /** CREATE TABLE `student6` ( `sid` int NOT NULL AUTO_INCREMENT, `snmae` varchar(11) DEFAULT NULL, `sex` char(1) DEFAULT NULL, PRIMARY KEY (`sid`), UNIQUE KEY `snmae` (`snmae`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci */ alter table student6 drop index snmae; # 刪除 唯一約束 desc student6; alter table student6 add unique key hehe (snmae); # 給 sname 新增唯一約束 名字為 hehe,作用為:在刪除時使用 # 如果新增約束時,沒有指定名字,約束名是類名 alter table student6 drop index hehe; # 刪除名為 hehe 的唯一約束 /** 刪除主鍵約束 一個表只能有一個主鍵 */ create table student7 ( sid int primary key auto_increment, sname varchar(11) ); alter table student7 drop primary key; # [42000][1075] Incorrect table definition; there can be only one auto column and it must be defined as a key # 刪除主鍵約束,需要先刪除自增 alter table student7 modify sid int; # 刪除 自增約束 desc student7; /** 建立表之後,新增主鍵約束 */ alter table student7 add primary key heh (sid); # 設定 主鍵別名 heh 或者使用 change | modify desc student7; show create table student7; # 檢視建立表的語句 /** CREATE TABLE `student7` ( `sid` int NOT NULL, `sname` varchar(11) DEFAULT NULL, PRIMARY KEY (`sid`) # 可以在最後一行定義主鍵約束和唯一約束 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci */ /** 聯合主鍵 兩列共同組成一個主鍵 成績表 : 學號+課程號 為聯合主鍵 特點1:兩個值都不能為 null,不能同時重複 */ create table tab_score ( sid int, # 學號 cid int, # 課程編號 score float(4, 1), primary key (sid, cid) ); insert into tab_score (sid, cid, score) values (2, 1, 100); /** 刪除聯合主鍵 */ alter table tab_score drop primary key; # 沒有刪除 not null 的約束 /** 建立表後新增 聯合主鍵 */ desc tab_score; select * from tab_score; delete from tab_score; # 清空表 alter table tab_score add primary key (sid, cid); # [23000][1062] Duplicate entry '2-1' for key 'tab_score.PRIMARY' # 新增失敗,現有的記錄違反了約束 /** 外來鍵約束 a 表中要引用 b 表的記錄,可以在 a 表中定義一列作為外來鍵列,來引用 b 表的主鍵 此時 a 表為從表,b 表為主表 特點1:一個從表中 可以定義多個外來鍵 特點2:可以為 null 特點3:可以重複 特點4:如果外來鍵列有值,次值必須在主鍵的主鍵列中存在 特點5:外來鍵列只能引用主表的主鍵 特帶你6:刪除主表記錄前 需要先刪除從表的引用或者刪除從表的外來鍵約束 */ create table teacher ( tid int primary key, tname varchar(11) ); drop table student8; create table student8 ( sid int primary key, sname varchar(11), mytid int, constraint fk_1 foreign key (mytid) references teacher (tid), # 外來鍵約束 # constraint # fk_1 外來鍵約束名,用於刪除 # foreign key (mytid) 指定從表中的外來鍵列 # references teacher(tid) 指定主表和主表的主鍵列 mytid_heard int, constraint fk_2 foreign key (mytid_heard) references teacher (tid) ); desc student8; # MUL 外來鍵 insert into teacher (tid, tname) values (1, '張老師'); insert into student8 (sid, sname, mytid, mytid_heard) values (100, '韓寒', null, null); insert into student8 (sid, sname, mytid, mytid_heard) values (1, '韓寒', 1, 1); insert into student8 (sid, sname, mytid, mytid_heard) values (2, '韓寒', 1, 1); insert into student8 (sid, sname, mytid, mytid_heard) values (2, '韓寒', 2, 1); # [23000][1062] Duplicate entry '2' for key 'student8.PRIMARY' # /** 刪除外來鍵約束 */ alter table student8 drop foreign key fk_1; # 刪除外來鍵約束 /** 建立表後新增外來鍵約束 */ alter table student8 add constraint foreign key fk_1 (mytid) references teacher (tid); # 新增外來鍵約束