怎样实现外键级联更新?
有主外键关系的表,若主表的主键更新了,有没有什么语句能使各个子表的外键也随之更新?
举例如下:
SQL> create table t1(c integer,d integer,constraint pk_t1 primary key (c));
表已创建。
SQL> create table t2(c integer,d integer,constraint fk_t2 foreign key (c)
references t1(c) on delete cascade);
表已创建。
SQL> begin
2 for x in 1..5 loop
3 insert into t1 values(x,x+8);
4 insert into t2 values(x,x+16);
5 end loop;
6 commit;
7 end;
8 /
PL/SQL 过程已成功完成。
SQL> select * from t1;
C D
---------- ----------
1 9
2 10
3 11
4 12
5 13
已选择5行。
SQL> select * from t2;
C D
---------- ----------
1 17
2 18
3 19
4 20
5 21
已选择5行。
SQL> update t1 set c=6 where c=5;
update t1 set c=6 where c=5
*
ERROR 位于第 1 行:
ORA-02292: 违反完整约束条件 (CHENNAN.FK_T2) - 已找到子记录日志
.....