資料庫使用truncate清理很多表時碰到外鍵約束時怎麼快速解決

來源:互聯網
上載者:User

問題處理思路:

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;

/

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.