主鍵約束 primary key:一個(gè)表中只能有一個(gè)主鍵,主鍵值不能為空,不能重復(fù)
設(shè)置主鍵約束:1 在創(chuàng)建表時(shí)就設(shè)置好主鍵約束
create table person(id int primary key);
或 create table person(id int ,name char(10),primary key(id));
2 通過(guò)修改表中列的屬性來(lái)設(shè)置主鍵約束
alter table person add primary key(id);
或 alter table person modify id int primary key;
設(shè)置聯(lián)合主鍵:1:在創(chuàng)建時(shí)就設(shè)置為聯(lián)合主鍵約束
create table person(id int,name char(10),primary key(id,name));
2:修改列的屬性設(shè)置聯(lián)合主鍵約束
alter table person add primary key(id,name);
不同記錄的聯(lián)合主鍵的列值不能同時(shí)相同,可以分別相同,不能同時(shí)為空
刪除主鍵約束:alter table person drop primary key;
遞增約束 auto_increment:設(shè)置為自動(dòng)遞增的列,自己沒(méi)給他添加值,系統(tǒng)會(huì)自動(dòng)給他賦值,一般把主鍵設(shè)為遞增約束
create table student(id int primary key auto_increment);
唯一約束 unique:設(shè)置為唯一約束的列值不能重復(fù)
設(shè)置唯一約束: 1 在創(chuàng)建表時(shí)就設(shè)置好唯一約束
create table person(id int unique);
或 create table person(id int ,name char(10),unique(id));
2 通過(guò)修改表中列的屬性來(lái)設(shè)置唯一約束
alter table person add unique(id);
或 alter table person modify id int unique;
設(shè)置聯(lián)合唯一約束:1:在創(chuàng)建時(shí)就設(shè)置為聯(lián)合唯一約束
create table person(id int,name char(10),primary key(id,name));
2:修改列的屬性設(shè)置聯(lián)合唯一約束
alter table person add unique(id,name);
不同記錄的聯(lián)合唯一約束的列值不能同時(shí)相同,可以分別相同,不能同時(shí)為空
刪除主鍵約束:alter table person drop unique;
默認(rèn)約束 default:設(shè)置某個(gè)列在沒(méi)有記錄時(shí)使用默認(rèn)值
create table person (id int,name char(10) default 'zhangsan');
非空約束 not null:非空約束的列值不能為空
create table person(id int,name char(10) not null);
外鍵約束 foreige key references:副表被約束的列值從主表約束列值中選擇,主表約束的列值被副表列值使用時(shí),這個(gè)主表的列值不能被刪除。主表的列必須是主鍵;
create table person(id int primary key);
create table student(id int,foreign key(id) references person(id));
浙公網(wǎng)安備 33010602011771號(hào)