http://blog.csdn.net/iteye1011/article/details/11228255---百问百答10.数据库表中没有设置级联删除.怎样用SQL语句实现,例如: EMP表中有字段DEPT_NO是外键 POS表中有字段DEPT_NO是外键 DEPT表中有字段DEPT_NO, 如何实现删除DEPT表中数据时将EMP表,POS表中的相关数据也删除?方法一:触发器解决 create or replace trigger delete_dept before delete on DEPT for each row begin delete from EMP where DEPT_NO = :old.DEPT_NO; delete from POS where DEPT_NO = :old.DEPT_NO; end;方法二:修改从表的外键设置,改为“on delete cascade”模式 a)先查询出EMP表和POS表中外键的名称(如果已知道外键名,这一步可以省略) select CONSTRAINT_NAME,TABLE_NAME from user_constraints where CONSTRAINT_TYPE ='R' and TABLE_NAME in('EMP','POS'); b)删除EMP表和POS表上的外键后重新建立允许级联删除的外键模式 alter table EMP drop constraint 外键名; alter table POS drop constraint 外键名; alter table EMP add constraint 外键名 foreign key(DEPT_NO) references DEPT(DEPT_NO) on delete cascade; alter table POS add constraint 外键名 foreign key(DEPT_NO) references DEPT(DEPT_NO) on delete cascade;
EMP表中有字段DEPT_NO是外键
POS表中有字段DEPT_NO是外键
DEPT表中有字段DEPT_NO,
如何实现删除DEPT表中数据时将EMP表,POS表中的相关数据也删除?方法一:触发器解决
create or replace trigger delete_dept
before delete on DEPT
for each row
begin
delete from EMP where DEPT_NO = :old.DEPT_NO;
delete from POS where DEPT_NO = :old.DEPT_NO;
end;方法二:修改从表的外键设置,改为“on delete cascade”模式 a)先查询出EMP表和POS表中外键的名称(如果已知道外键名,这一步可以省略)
select CONSTRAINT_NAME,TABLE_NAME from user_constraints where CONSTRAINT_TYPE ='R' and TABLE_NAME in('EMP','POS');
b)删除EMP表和POS表上的外键后重新建立允许级联删除的外键模式
alter table EMP drop constraint 外键名;
alter table POS drop constraint 外键名;
alter table EMP add constraint 外键名 foreign key(DEPT_NO) references DEPT(DEPT_NO) on delete cascade;
alter table POS add constraint 外键名 foreign key(DEPT_NO) references DEPT(DEPT_NO) on delete cascade;