清除oracl中有主外键关联的表中的部分数据。

1.禁用主外键
BEGIN
for c in (select 'ALTER TABLE '||TABLE_NAME||' DISABLE CONSTRAINT '||constraint_name||' ' as v_sql from user_constraints where CONSTRAINT_TYPE='R') loop
EXECUTE IMMEDIATE c.v_sql;
end loop;
end;
2.清空数据源。清空所有数据。一般情况是对表进行条件删除。
begin
for ec in
(select table_name from user_tables)
loop
execute immediate 'delete '|| ec.table_name;
end loop;
end;
commit;
3.启动主外键:
BEGIN
for c in (select 'ALTER TABLE '||TABLE_NAME||' ENABLE CONSTRAINT '||constraint_name||' ' as v_sql from user_constraints where CONSTRAINT_TYPE='R') loop
EXECUTE IMMEDIATE c.v_sql;
end loop;
end;
4.启动失败,有时候因为删除了父级数据而子级没删,看数据库报错信息,一步一步往下找,从下到上把子数据删除。
5.再次启动主外键。

posted @ 2014-07-02 12:09  天羽星河落  阅读(239)  评论(0编辑  收藏  举报