南京佛搜做網(wǎng)站公司互聯(lián)網(wǎng)營銷具體做什么
一、概述
1.概念:約束是作用于表中字段上的規(guī)則,用于限制存儲在表中的數(shù)據(jù)。
2.目的:保證數(shù)據(jù)庫中數(shù)據(jù)的正確、有效性和完整性。
3.分類
約束 | 描述 | 關(guān)鍵字 |
非空約束 | 限制該字段的數(shù)據(jù)不能為null | not null |
唯一約束 | 保證該字段的所有數(shù)據(jù)都是唯一、不重復(fù)的 | unique |
主鍵約束 | 主鍵是一行數(shù)據(jù)的唯一標(biāo)識,要求非空且唯一 | primary key |
默認(rèn)約束 | 保存數(shù)據(jù)時,如果未指定該字段的值,則采用默認(rèn)值 | default |
檢查約束 | 保證字段值滿足某一個條件 | check |
外鍵約束 | 用來讓兩張表的數(shù)據(jù)之間建立連接,保證數(shù)據(jù)的一致性和完整性 | foreing key |
注意:約束是作用于表中字段上的,可以在創(chuàng)建表 / 修改表的時候添加約束。?
二、約束演示
根據(jù)需求,來完成表結(jié)構(gòu)的創(chuàng)建
字段名 | 字段含義? | 字段類型 | 約束條件 | 約束條件 |
id | ID唯一標(biāo)識 | int | 主鍵,并且自動增長 | primary key , auto_increment |
name | 性別 | varchar(10) | 不為空,并且唯一 | not null , unique |
age | 年齡 | int | 大于0,并且小于等于120 | check |
status | 狀態(tài) | char(1) | 如果沒有指定該值,默認(rèn)為1 | default |
gender | 性別 | char(1) | 無 |
大家不妨先自己動手嘗試嘗試,我就先把答案放在下方啦!?
create table if not exists user(id int primary key auto_increment comment '主鍵',name varchar(10) not null unique comment '姓名',age int check(age>0 && age<120) comment '年齡',status char(1) default '1' comment '狀態(tài)',gender char(1) comment '性別'
)comment '用戶表';
建立的表結(jié)構(gòu)截圖如下:
三、外鍵約束?
1.概念:外鍵用來讓兩張表的數(shù)據(jù)之間建立連接,從而保證數(shù)據(jù)的一致性和完整性。
注意:目前上述的兩張表,在數(shù)據(jù)庫層面,并未建立外鍵關(guān)聯(lián),所以是無法保證數(shù)據(jù)的一致性和完整性的。
在開始之前,我先來進行表結(jié)構(gòu)的創(chuàng)建,代碼如下,大家可以自行參考
create table if not exists dept(id int auto_increment comment 'ID' primary key,name varchar(50) not null comment '部門名稱'
)comment '部門表';
insert into dept(name) values('研發(fā)部'),('市場部'),('財務(wù)部'),('銷售部'),('總經(jīng)辦');
create table if not exists dept_id(id int auto_increment comment 'ID' primary key,name varchar(50) not null comment '姓名',age int comment '年齡',job char(10) comment '職位',salary int comment '薪資',entrydate date comment '入職時間',managerid int comment '直屬領(lǐng)導(dǎo)ID',dept_id int comment '部門ID'
)comment '員工表';insert into emp(name,age,job,salary,entrydate,managerid,dept_id) values('金庸',66,'總裁','20000','2000-01-01',null,5),('張無忌',20,'項目經(jīng)理','12500','2005-12-05',1,1),('楊逍',33,'開發(fā)','8400','2000-11-03',2,1),('韋一笑',48,'開發(fā)','11000','2002-02-05',2,1),('常遇春',43,'開發(fā)','10500','2004-09-07',3,1);
創(chuàng)建好的表如下:
接下來,我們來建立外鍵關(guān)聯(lián),以此來保證數(shù)據(jù)的一致性和完整性。
2.外鍵約束
2.1添加外鍵
create table 表名(
? ? ? ? ? 字段名 數(shù)據(jù)類型 ,
? ? ? ? ? ...
? ? ? ? ? [constraint] [外鍵名稱] foreign key (外鍵字段名) references 主表(主表列名)
);
alter table 表名 add constraint 外鍵名稱 foreign key (外鍵字段名) references? 主表(主表列名);
即
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);
大家看emp表前后的區(qū)別
2.2刪除外鍵
alter table 表名 drop foreign key 外鍵名稱 ;?
即
alter table emp drop foreign key fk_emp_dept_id;
2.3刪除 / 更新行為
行為 | 說明 |
no action | 當(dāng)在父表中刪除 / 更新對應(yīng)記錄時,首先檢查記錄是否有對應(yīng)外鍵,如果有則不允許刪除 / 更新。(與restrict一致) |
restrict | 當(dāng)在父表中刪除 / 更新對應(yīng)記錄時,首先檢查記錄是否有對應(yīng)外鍵,如果有則不允許刪除 / 更新。(與no action一致) |
cascade | 當(dāng)在父表中刪除 / 更新對應(yīng)記錄時,首先檢查記錄是否有對應(yīng)外鍵,如果有,則也刪除 / 更新外鍵在子表中的記錄。 |
set null | 當(dāng)在父表中刪除 / 更新對應(yīng)記錄時,首先檢查記錄是否有對應(yīng)外鍵,如果有,則設(shè)置子表中該外鍵值為 null (這就要求該外鍵允許取 null )。 |
set default | 父表有變更時,子表將外鍵列設(shè)置成一個默認(rèn)的值。? |
alter table 表名 add constraint 外鍵名稱 foreign key (外鍵字段名) references? 主表名(主表列名) on update cascade on delete cascade;
?-- cascade
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update cascade on delete cascade;
?
-- set null
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update set null on delete set null;
?本節(jié)課的內(nèi)容到此結(jié)束啦,我們下期再見!