SQL主外來鍵約束

2021-06-20 08:41:10 字數 3699 閱讀 4240

主鍵

primary key

,唯一標示乙個實體。是保證資料庫的實體完整性,保證資料中資料的正確性和合理性,取值非空

唯一。外來鍵

foreign

,是用來使表與表之間聯絡。用來保證資料庫的參照完整性,外來鍵的取值必須來自參照表參照列的

值,可以為空也可不為空。

(1)外來鍵取值規則:空值或參照的主鍵值。

(2)插入非空值時,如果主鍵表中沒有這個值,則不能插入。

(3)更新時,不能改為主鍵表中沒有的值。

(4)刪除主鍵表記錄時,你可以在建外鍵時選定外來鍵記錄一起級聯刪除還是拒絕刪除。

(5)更新主鍵記錄時,同樣有級聯更新和拒絕執行的選擇。

不同的外來鍵約束方式將可以使兩張表緊密的結合起來,特別是修改或者刪除的級聯操作將使得日常的維護工作更

加輕鬆。常見的外來鍵有級聯

(cascade)

方式,置空

(set null)

方式及禁止

(noaction / restrict)方式

我們以班級表(

t_class

)和學生表(

t_student

)為例,這是典型的一對多的關係,乙個班級可以有多名學生,

而乙個學生只能屬於乙個班級。

首先建立班級表:

create table t_class (

id int not null,

name varchar(30),

primary key (id)

);

並插入兩條記錄:

insert into t_class values (1, '一班');

insert into t_class values (2, '二班');

下面建立使用者表,分別以不同的約束方式建立外來鍵引用關係:

--級聯方式

create table t_student (

id int not null,

name varchar(30),

classid int,

primary key (id),

foreign key (classid) references t_class(id) on delete cascade on update cascade

);--參照完整性測試

insert into t_student values (1, '王一', 1); --可以插入

insert into t_student values (2, '李二', 2);   --可以插入

insert into t_student values (3, '張三', 3);  --錯誤,無法插入,班級3不存在,與參照完整性約束不符

--約束方式測試

insert into t_student values (1, '王一', 1);

insert into t_student values (2, '李二', 2);

insert into t_student values (3, '張三', 2);

delete from t_class where id=2;             --導致t_student中的2、3記錄級聯刪除

update t_class set id=2 where id=1;         --導致t_student中的1記錄的classid級聯修改為2

--置空方式

create table t_student (

id int not null,

name varchar(30),

classid int,

primary key (id),

foreign key (classid) references t_class(id) on delete set null on update set null

);--參照完整性測試

insert into t_student values (1, '王一', 1);  --可以插入

insert into t_student values (2, '李二', 2);  --可以插入

insert into t_student values (3, '張三', 3);  --錯誤,無法插入,班級3不存在,與參照完整性約束不符

--約束方式測試

insert into t_student values (1, '王一', 1);

insert into t_student values (2, '李二', 2);

insert into t_student values (3, '張三', 2);

delete from t_class where id=2;             --導致t_student中的2、3記錄的classid被設定為null

update t_class set id=2 where id=1;         --導致t_student中的1記錄的classid被設定為null

--禁止方式

create table t_student (

id int not null,

name varchar(30),

classid int,

primary key (id),

foreign key (classid) references t_class(id) on delete no action on update no action

);--參照完整性測試

insert into t_student values (1, '王一', 1); --可以插入

insert into t_student values (2, '李二', 2);   --可以插入

insert into t_student values (3, '張三', 3);   --錯誤,無法插入,班級3不存在,與參照完整性約束不符

--約束方式測試

insert into t_student values (1, '王一', 1);

insert into t_student values (2, '李二', 2);

insert into t_student values (3, '張三', 2);

delete from t_class where id=2;             --錯誤,從表中有相關引用,因此主表中無法刪除

update t_class set id=2 where id=1;        --錯誤,從表中有相關引用,因此主表中無法修改

主外來鍵是前天總結出來的寶貴經驗,而且一直存在就說明有它自己的優勢,我們要靈活的應用,而不是為了圖一

時簡單而濫用第三張表替代它。當我們的資料是一對多或者多對一的時候,要考慮使用主外來鍵約束,如果是多對多的

關係時,才考慮使用第三張表建立索引表,但是要慎重,第三張表會給我們資料維護帶來很大的麻煩,當然,事情沒

有絕對,大家在了解主外來鍵和第三張表的利弊之後,合理的選擇利用。本篇文章只是提醒大家重視主外來鍵的應用!

SQL 主外來鍵

以下面三張表為例 有三張表,一張表是讀者資訊,有乙個屬性為readno,一張表是圖書的資訊,有乙個屬性是bookno,一張表是借閱關係,有兩個屬性分別以讀者 資訊表中的readno,和圖書資訊表中的bookno為外來鍵,我想問的是,在借閱關係表中插入資料時不是得自己寫入readno和bookno嗎,...

oracle 檢視主外來鍵約束

select a.constraint name,a.table name,b.constraint name from user constraints a,user constraints b where a.constraint type r and b.constraint type p a...

SQL server新增主外來鍵約束

新增主鍵約束 alter table 表名 add constraint 約束名 primary key 主鍵 新增唯一約束 alter table 表名 add constraint 約束名 unique 字段 新增預設約束 alter table 表名 add constraint 約束名 de...