CHIL-ORACLE-主外键约束(primary key / foreign key)
生活随笔
收集整理的這篇文章主要介紹了
CHIL-ORACLE-主外键约束(primary key / foreign key)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
主鍵約束
要求主鍵列數據唯一,并且不允許為空
外鍵約束
用于量表建立關系,需要指定引用朱彪的那列(主表必須是主鍵)
1.主鍵約束 ( primary key )--例如1:create table test(c number(10) primary key );--例如2:create table test1(c number(10) constraint pk_c primary key );--例如3:create table test2(c number(10) ,primary key(c));--例如4:create table test3(c number(10),c1 number(10),primary key (c,c1));--例如5:create table test4(c number(10) ,constraint pk_test4_c primary key (c));2.給建好的表創建主鍵:--例如6:create table test5(c number(10)); alter table test5 add primary key (c);3.給建好的表添加主鍵:不使用默認主鍵名,自定義主鍵名--例如7:create table test6(c number(10)); alter table test6 add constraint pk_test6_c primary key(c);4.外鍵約束 ( foreign key ) --例如1:Fk 使用“列級約束”來進行建表:create table test8(a1 number(10) primary key );create table test9(b1 number(10) primary key,b2 number(10) references test8(a1));--例如2:Fk 使用“表級約束”來進行建表:Create table test10 (a1 number(10) primary key);Create table test11(b number(10) primary key,b2 number(10),foreign key(b2)references test10(a1));--例如3:Create table test12 (a1 number(10) primary key);Create table test13(b number(10) primary key,b2 number(10),foreign key(b2)references test12(a1));--例如4:Create table test14 (a1 number(10) );alter table test14 add constraint pk_test14_c primary key(a1);Create table test16(b number(10),b2 number(10));alter table test16 add constraint pk_test16_c foreign key(b) references test14(a1);5.級聯刪除:--例如一:(如果刪除父表中的某條記錄,子表相應記錄也被刪除)create table test17(id number primary key);插入操作:insert into test17(id)values(1);create table test18(id number primary key,p_id number references test17(id) on delete cascade);插入操作:insert into test18 values(1,1);刪除操作:delete from test17 where id=1;--注意:發現子表的數據已經沒有了--例如二:(如果刪除父表某條記錄,子表相應記錄被置空)create table parent(id number primary key);insert into parent values (1);create table chile(id number primary key,p_id number references parent(id) on delete set null);插入操作:insert into chile values(1,1);刪除操作:delete from parent where id=1; --注意:刪除父表時子表對應列為空6. Foreign Key 的可選參數 ON DELETE CASCADE在創建 Foreign Key 時可以加可選參數:ON DELETE CASCADE 它的含義是如果刪除外鍵主表里的內容,子表里相關的內容將一起被刪除.如果沒有 ON DELETE CASCADE 參數,子表里有內容,父表里的主關鍵字記錄不能被刪除掉.
要求主鍵列數據唯一,并且不允許為空
外鍵約束
用于量表建立關系,需要指定引用朱彪的那列(主表必須是主鍵)
1.主鍵約束 ( primary key )--例如1:create table test(c number(10) primary key );--例如2:create table test1(c number(10) constraint pk_c primary key );--例如3:create table test2(c number(10) ,primary key(c));--例如4:create table test3(c number(10),c1 number(10),primary key (c,c1));--例如5:create table test4(c number(10) ,constraint pk_test4_c primary key (c));2.給建好的表創建主鍵:--例如6:create table test5(c number(10)); alter table test5 add primary key (c);3.給建好的表添加主鍵:不使用默認主鍵名,自定義主鍵名--例如7:create table test6(c number(10)); alter table test6 add constraint pk_test6_c primary key(c);4.外鍵約束 ( foreign key ) --例如1:Fk 使用“列級約束”來進行建表:create table test8(a1 number(10) primary key );create table test9(b1 number(10) primary key,b2 number(10) references test8(a1));--例如2:Fk 使用“表級約束”來進行建表:Create table test10 (a1 number(10) primary key);Create table test11(b number(10) primary key,b2 number(10),foreign key(b2)references test10(a1));--例如3:Create table test12 (a1 number(10) primary key);Create table test13(b number(10) primary key,b2 number(10),foreign key(b2)references test12(a1));--例如4:Create table test14 (a1 number(10) );alter table test14 add constraint pk_test14_c primary key(a1);Create table test16(b number(10),b2 number(10));alter table test16 add constraint pk_test16_c foreign key(b) references test14(a1);5.級聯刪除:--例如一:(如果刪除父表中的某條記錄,子表相應記錄也被刪除)create table test17(id number primary key);插入操作:insert into test17(id)values(1);create table test18(id number primary key,p_id number references test17(id) on delete cascade);插入操作:insert into test18 values(1,1);刪除操作:delete from test17 where id=1;--注意:發現子表的數據已經沒有了--例如二:(如果刪除父表某條記錄,子表相應記錄被置空)create table parent(id number primary key);insert into parent values (1);create table chile(id number primary key,p_id number references parent(id) on delete set null);插入操作:insert into chile values(1,1);刪除操作:delete from parent where id=1; --注意:刪除父表時子表對應列為空6. Foreign Key 的可選參數 ON DELETE CASCADE在創建 Foreign Key 時可以加可選參數:ON DELETE CASCADE 它的含義是如果刪除外鍵主表里的內容,子表里相關的內容將一起被刪除.如果沒有 ON DELETE CASCADE 參數,子表里有內容,父表里的主關鍵字記錄不能被刪除掉.
?
轉載于:https://www.cnblogs.com/ChineseIntelligentLanguage/p/6513269.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的CHIL-ORACLE-主外键约束(primary key / foreign key)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CHIL-ORACLE-唯一约束(uni
- 下一篇: 条码扫描枪----针对MS3391扫码器