Oracle外键级联删除和级联更新
1 级联删除
Oracle在外键的删除上有NO ACTION(类似RESTRICT)、CASCADE和SET NULL三种行为。
下面以学生-班级为例说明不同情况下的外键删除,学生属于班级,班级的主键是学生的外键。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
-- 班级表
CRATE
TABLE
TB_CLASS
(
ID NUMBER
NOT
NULL
,
--班级主键
CONSTRAINT
PK_TB_CLASS
PRIMARY
KEY
(ID)
);
-- 学生表
CREATE
TABLE
TB_STUDENT
(
--学生主键
--学生姓名
CLASS_ID NUMBER,0)!important; background:none!important">--学生所属班级,外键
--主键约束
PK_TB_STUDENT
(ID),
--外键约束
--设置级联删除为NO ACTION
FK_TB_STUDENT_CLASS_ID
FOREIGN
(CLASS_ID)
REFERENCES
TB_CLASS (ID)
);
-- 添加班级数据
INSERT
INTO
TB_CLASS (ID,
)
VALUES
(1,
'一班'
);
(2,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">'二班'
);
(3,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">'三班'
);
-- 添加学生数据
TB_STUDENT (ID,CLASS_ID)
'小明'
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,1);
'小刚'
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,1);
'小王'
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,1);
(4,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">'二明'
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,2);
(5,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">'二刚'
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,2);
(6,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">'二王'
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,2);
(7,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">'大明'
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,3);
(8,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">'大刚'
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,3);
(9,monospace!important; font-size:1em!important; min-height:auto!important; color:blue!important; background:none!important">'大王'
sql plain" style="border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,3);
|
初始班级数据
初始学生数据
1.1 NO ACTION
NO ACTION指当删除主表中被引用列的数据时,如果子表的引用列中包含该值,则禁止该操作执行。
现在学生外键级联删除是NO ACTION,执行删除班级操作。
--删除三班
DELETE
FROM
TB_CLASS
WHERE
ID=3;
|