首先看一段代碼
alter table employee add constraint FK foreign key (department_sn) references department (sn) on delete restrict on update restrict;
employee和department是兩張表,FK為約束名 department_sn為employee表中的一個屬性,sn為department表中的主鍵
sn為department的主鍵,而depart_sn並不是employee的主鍵,那么則稱department為主表,employee為從表
on delete | update restrict 代表在主表進行刪除和更新時,會查看是否存在外鍵依賴,如果有則不允許刪除
類似的還有:
cascade 在主表進行操作時,如果有外鍵依賴,那么刪除子表中的那條記錄
set null 在主表進行操作時 如果有外鍵依賴 那么將子表中的數據設置為null
no action 同restrict
