表的約束:
關鍵字:constraint
約束是一種表級別的限制,它通過對表的資料限制來保證資料的完整性和一致性
常見約束:
主鍵約束(primary key)
用途:就是用來約束其中的一列,作為所有列中的識別符號(這一列的唯一代表),
在一張表中通過主鍵可以準確定位到一列,可以避免列中資料的重復,
主鍵的特性:
1.唯一約束
2.非空約束
語法:
1.create table [庫名].表名 (列名1 資料型別1(長度)
primary key,列名2 資料型別2(長度));
2.create table [庫名].表名 (列名1 資料型別1(長度),
列名2 資料型別2(長度),primary key(列名1));
第一種創建主鍵方式
create table school.bbq(
id int(2) primary key,
name varchar(3),
tall int(4),
age int(3)
);
insert into school.bbq values
(1001,'小賀',170,20),
(1002,'小竇',184,20),
(1003,'小張',175,20),
(1004,'小王',170,20);
-- 驗證主鍵的唯一性約束
insert into school.bbq(id,name) values (1002,'小周');
-- 驗證主鍵的非空約束
insert into school.bbq name) values ('小楊');
-- 第二種主鍵創建方式
create table school.qqq(
id int(2),
name varchar(3),
age int(2),
sex varchar(2),
primary key(id)
);
2.唯一約束(unique)
用途:用來約束一列中的所有資料,不能重復,
create table school.aaa(
id int(3) unique,
name varchar(3),
age int(4)
);
3.非空約束(not null)
用途:用來約束一列中的所有資料,不能為null,
注意:所有資料型別都可以是空,
create table school.bbb(
id int(3) not null,
name varchar(3),
age int(4)
);
默認約束(default)
用途:在規定了的默認值約束的列時,不向該列插入其他資料,則該資料為默認資料
語法:create table 表名(列名1 資料型別1(長度)default 默認值,列名2 資料型別2(長度));
外鍵約束(foregin key)
用途:也能確保資料的完整性也能展現和其他表的關系,
一個表可以一個或多個外鍵,每個外鍵必須(references)另一個表的主鍵或唯一鍵,
語法:
create table 表名(列名1 資料型別1(長度),列名2 資料型別2(長度),
forrign key(本表外鍵列名) references 被參考的表(被參考的列));
create table school.bbp(
id int(3),
tall int(3),
brithday date,
foreign key(id) references bbq(id)
);
檢查約束(check)
該約束在mysql上停用了,陳述句不會報錯,但沒有實際用處
作用:用于限制列中的值的范圍,比如 check了一列,那么該列只允許特定的值,
語法:
create table 表名 (列名1 資料型別1(長度),列名2,
資料型別2(長度),check(運算式))
例如:check(id>0)
create table school.qqq(
id int(2),
name varchar(3),
check(id>0)
);
查看一個表的鍵值
語法:show keys from 表名;
表的修改
為表添加主鍵約束
語法:alter table 表名 add primary key(列名);
create table school.bbq(
id int(2),
name varchar(3),
age int(2)
);
desc school.bbq;
-- 查找展示主鍵數量
show keys from school.bbq;
-- 為表添加主鍵
alter table school.bbq add primary key(id);
show keys from school.bbq;
desc school.bbq;
修改表的名字
語法:alter table 舊表名 rename to 新表名;
alter table school.bbq rename to school.bbb;
將表中的主鍵洗掉
語法:alter table 表名 drop primary key;
alter table school.bbq drop primary key;
為表中添加非空約束 modify :重新定義的意思
語法:alter table 表名 modify 列名 資料型別(長度) not null;
為表中的列添加非空約束---重新定義列的型別與約束
語法: alter table 表名 modify 列名 資料型別(長度) 列約束;
alter table school.bbq -- 修改的表
modify name varchar(4) -- 修改表中的某個物件列
not null; -- 不為空
alter table school.bbq
modify name varchar(3);
修改表中的某個列名----可以修改原型別的長度載資料兼容的情況下也可以修改原型別
語法:alter table 表名 change 舊列名 新列名 資料型別(長度);
注意:空串不等于空,空串是字串型別
alter table school.student change id Sid int(3);
alter table school.student change Sid sid int(3);
alter table school.student change sid id int(2);
alter table school.student change id ID int(5);
desc school.student;
alter table school.student change ID Id int(5);
為表添加一列或多列
-- 單列
語法:alter table 表名 add column 列名 資料型別(長度);
alter table school.student add column hahaha int(2);
-- 改變表中某列的列名
alter table school.student change hahaha card int(2);
-- 多列
語法:alter table 表名 add column
(列名1 資料型別1(長度),列名2 資料型別2(長度));
alter table school.student add column (idcard1 int(2),idcard2 int(3));
修改表中指定的列的資料型別
語法1:alter table 表名 modify [column]列名 新資料型別(長度);
alter table school.student modify column card varchar(3);
洗掉列
語法:alter table 表名 drop column 列名;
語法;alter table 表名 drop column 列名1,
drop column 列名2,drop column 列名3;
alter table school.student drop column idcard1;
alter table school.student drop column id1;
alter table school.student drop column id2;
alter table school.student drop column idcard;
alter table school.student drop column id1,drop id2,drop id3;
增加多個列
alter table school.student add column(id1 int(2),id2 int(2),id3 int(2));
創建表時指定列為主鍵
語法:create table 表名 (列名1 資料型別1(長度) primary key,
列名2 資料型別2(長度) primary key)
語法:create table 表名 (列名1 資料型別1(長度),
列名2 資料型別2(長度) primary key(列名1,列名2));
創建表時指定某一列為外鍵
語法: create table 表名(列名1 資料型別1(長度),列名2 資料型別2(長度),
foreign key(本表列) references 被參考的表(被參考的列);
在添加外鍵時指定該約束的名字
語法:alter table 表名 表名(列名1 資料型別1(長度),列名2 資料型別2(長度),
constraint 約束名 foreign key(本表列) references 被參考的表(被參考的列);
注意:參考被參考的列的資料型別(包括約束)需要一致
create table school.student1 (
id int(2),
name varchar(2),
age int(2),
constraint id foreign key (id) references student(id)
);
洗掉指定表中的外鍵約束
1.洗掉外鍵
語法:alter table 表名 drop foreign key 外鍵名;
2.洗掉索引
語法:drop index 索引名 on 表名;
alter table school.student1 drop foreign key student1_ibfk_1;
drop index id on school.student1;
show keys from school.student1;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/499110.html
標籤:MySQL
