首页 > 代码库 > 数据库使用truncate清理非常多表时碰到外键约束时怎么高速解决

数据库使用truncate清理非常多表时碰到外键约束时怎么高速解决

问题处理思路:

1. 先将数据库中涉及到外键约束的表置为无效状态

2.待清除全然部表数据后再将外键约束的表置为可用状态

详细实现脚本:

declare

begin

for vv_sql in (SELECT ‘ALTER TABLE ‘|| table_name ||‘ DISABLE CONSTRAINT ‘ || CONSTRAINT_NAME alter_sql FROM USER_CONSTRAINTS

where CONSTRAINT_TYPE = ‘R‘)

loop

execute immediate vv_sql.alter_sql;

end loop;

execute immediate ‘truncate table 表名;

................. ---此处省略,可用写多个executesql语句

for vv_sql in (SELECT ‘ALTER TABLE ‘|| table_name ||‘ ENABLE CONSTRAINT ‘ || CONSTRAINT_NAME alter_sql FROM USER_CONSTRAINTS

where CONSTRAINT_TYPE = ‘R‘)

loop

execute immediate vv_sql.alter_sql;

end loop;

end;

/

数据库使用truncate清理非常多表时碰到外键约束时怎么高速解决