快速删除有外键关联的数据库的数据

某系统有600张表,要求删除业务数据,但保留基础数据(部门和人员等)和字典数据。

如果一张表一张表删除工作量就大了,因为外键关联决定了删除必须有先后顺序。

我们可以在删除前禁用外键,待删除完毕之后再启用外键。

当然,最后启用的时候发现删除了不应该删除的数据,因此删除前最好做完整备份。

 

生成禁用外键的脚本:

select 'alter table '|| t.table_name||' disable constraint '||t.constraint_name||';'
from user_constraints t where t.constraint_type = 'R'
order by t.table_name

 

生成启用外键的脚本:

 

select 'alter table '|| t.table_name ||' enable constraint '||t.constraint_name||';'
from user_constraints t where t.constraint_type = 'R'
order by t.table_name

posted @ 2013-09-16 20:38  深圳大漠  阅读(3073)  评论(0编辑  收藏  举报