国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > 綜合 > 正文

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

2024-07-21 02:42:28
字體:
供稿:網(wǎng)友
創(chuàng)建外鍵時(shí)的選項(xiàng):

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

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)指定的話,如果刪除父記錄,依賴于他的子記錄也會(huì)自動(dòng)刪除.相當(dāng)于級(jí)聯(lián)刪除.如果指定no action和cascade都會(huì)報(bào)錯(cuò),因?yàn)檫€有子記錄所以無法刪除該記錄.set nul允許刪除父記錄并且l會(huì)將子表中與父表關(guān)聯(lián)的字段設(shè)置為null.

3.On Update 只有兩個(gè)選項(xiàng) no action/restrict.它們?cè)诟潞蛣h除時(shí)并沒有區(qū)別:如果與子表關(guān)聯(lián)不允許刪除.

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 平湖市| 绥芬河市| 武隆县| 藁城市| 大埔区| 清水河县| 沁水县| 长子县| 平安县| 通榆县| 屏边| 洛阳市| 宁远县| 宁国市| 吉首市| 铜梁县| 乐清市| 涞源县| 昌黎县| 民乐县| 台南县| 白朗县| 易门县| 大方县| 金湖县| 邵阳县| 桂平市| 壶关县| 安图县| 宣恩县| 台南县| 云梦县| 双牌县| 五河县| 美姑县| 北安市| 霍州市| 淄博市| 屏边| 宁陕县| 华阴市|