数据库表中没有设置级联删除.怎样用SQL语句实现: 如:EMP表中有字段DEPT_NO是外键 POS表中有字段DEPT_NO是外键 DEPT表中有字段DEPT_NO, 如何实现删除DEPT表中数据时将EMP表,POS表中的相关数据也删除;
两种方法,个人建议你选择方法一,简单方便 方法一:触发器解决(下面的代码可以不用修改,copy直接用) 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; / 方法二:修改你的外键设置,达到级联删除的目的,具体实现如下: 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;