国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

教你快速掌握DB2數據庫創建外鍵時的選項

瀏覽:2日期:2023-11-08 14:59:30
創建外鍵時的選項:

1.創建測試表:

drop table student;

drop table class;

drop table student_class;

Create table student(student_id integer not null,student_name varchar(200), CONSTRAINT P_KEY_1 primary key (student_id)) in luzl_32k_tb index in luzl_32k_tb ;

Create table class(class_id integer not null,class_name varchar(200), CONSTRAINT P_KEY_2 primary key (class_id)) in luzl_32k_tb index in luzl_32k_tb ;

Create table student_class(student_class_id integer,student_id integer,class_id integer) in luzl_32k_tb index in luzl_32k_tb;

alter table student_class add constraint if_class foreign key(class_id) references class(class_id) ON DELETE cascade ON UPDATE RESTRICT;

alter table student_class add constraint if_student foreign key(student_id) references student(student_id) ON DELETE cascade ON UPDATE RESTRICT;

Insert into student(student_id,student_name) values(1,'luzl');

Insert into class(class_id,class_name) values(1,'db2');

Insert into student_class(student_class_id,student_id,class_id) values(1,1,1);

2.On Delete 的選項有

Restrict/no action/cascade/set null.其中cascade選項指定的話,如果刪除父記錄,依賴于他的子記錄也會自動刪除.相當于級聯刪除.如果指定no action和cascade都會報錯,因為還有子記錄所以無法刪除該記錄.set nul允許刪除父記錄并且l會將子表中與父表關聯的字段設置為null.

3.On Update 只有兩個選項 no action/restrict.它們在更新和刪除時并沒有區別:如果與子表關聯不允許刪除.

4.另外還需要注意一點,父表中的字段必須是主鍵,才能做為子表的外鍵。

標簽: DB2 數據庫
主站蜘蛛池模板: 恭城| 东莞市| 汾阳市| 上蔡县| 徐闻县| 巴林右旗| 逊克县| 镇雄县| 出国| 夹江县| 会理县| 陆丰市| 惠安县| 盐山县| 延吉市| 博客| 梨树县| 收藏| 保山市| 四平市| 揭西县| 洪洞县| 和平县| 陇川县| 湟源县| 江口县| 扎兰屯市| 方正县| 克什克腾旗| 舒兰市| 苏尼特左旗| 兴安县| 九江市| 舒兰市| 伊宁市| 龙泉市| 大名县| 卓资县| 广河县| 景德镇市| 峨山|