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

您的位置:首頁技術(shù)文章
文章詳情頁

教你快速掌握DB2數(shù)據(jù)庫創(chuàng)建外鍵時的選項(xiàng)

瀏覽:4日期:2023-11-08 14:59:30
創(chuàng)建外鍵時的選項(xiàng):

1.創(chuàng)建測試表:

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 的選項(xiàng)有

Restrict/no action/cascade/set null.其中cascade選項(xiàng)指定的話,如果刪除父記錄,依賴于他的子記錄也會自動刪除.相當(dāng)于級聯(lián)刪除.如果指定no action和cascade都會報錯,因?yàn)檫€有子記錄所以無法刪除該記錄.set nul允許刪除父記錄并且l會將子表中與父表關(guān)聯(lián)的字段設(shè)置為null.

3.On Update 只有兩個選項(xiàng) no action/restrict.它們在更新和刪除時并沒有區(qū)別:如果與子表關(guān)聯(lián)不允許刪除.

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

標(biāo)簽: DB2 數(shù)據(jù)庫
主站蜘蛛池模板: 改则县| 防城港市| 肥乡县| 彝良县| 壶关县| 崇阳县| 岳阳县| 沙田区| 屏山县| 义马市| 五寨县| 色达县| 靖江市| 平顶山市| 当阳市| 河北省| 柞水县| 鄱阳县| 晋城| 宁波市| 杂多县| 敦化市| 甘肃省| 寿阳县| 桦甸市| 珠海市| 长沙市| 咸宁市| 长乐市| 婺源县| 军事| 措美县| 天柱县| 成武县| 阿荣旗| 澄城县| 邓州市| 吉首市| 阜南县| 息烽县| 阿尔山市|