oracle中把父表中的值删掉,则子表中相关的值也删掉,并把删掉的值放入另一个表中
2个回答
展开全部
使用级联删除+触发器可以搞定你的需求。
父表A 子表B 备份表C
命令如下:
create table A
(
dept_ID number primary key,
dept_name varchar2(10)
);
create table B
(
no number primary key,
name varchar2(10),
dept_id number
);
ALTER TABLE WWJ.B ADD CONSTRAINT FK_B FOREIGN KEY (DEPT_ID) REFERENCES WWJ.A (DEPT_ID) ON DELETE CASCADE ENABLE VALIDATE;
CREATE OR REPLACE TRIGGER WWJ.TR_B_01
BEFORE DELETE
ON WWJ.B
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
BEGIN
INSERT INTO C VALUES(:old.no,:old.name,:old.dept_id);
END;
父表A 子表B 备份表C
命令如下:
create table A
(
dept_ID number primary key,
dept_name varchar2(10)
);
create table B
(
no number primary key,
name varchar2(10),
dept_id number
);
ALTER TABLE WWJ.B ADD CONSTRAINT FK_B FOREIGN KEY (DEPT_ID) REFERENCES WWJ.A (DEPT_ID) ON DELETE CASCADE ENABLE VALIDATE;
CREATE OR REPLACE TRIGGER WWJ.TR_B_01
BEFORE DELETE
ON WWJ.B
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
BEGIN
INSERT INTO C VALUES(:old.no,:old.name,:old.dept_id);
END;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询