数据库使用truncate清理很多表时碰到外键约束时怎么快速解决
程序员文章站
2022-06-06 13:48:19
...
问题处理思路: 1. 先将数据库中涉及到外键约束的表置为无效状态 2.待清除完所有表数据后再将外键约束的表置为可用状态 具体实现脚本: declare begin for vv_sql in (SELECT ALTER TABLE || table_name || DISABLE CONSTRAINT || CONSTRAINT_NAME alter_sql
问题处理思路:
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;
/