Oracle批量執行sql語句之禁用所有表的外鍵_oracle

來源:互聯網
上載者:User

在轉移資料庫,進行資料匯入的時候,遇到一件麻煩事,就是表間外鍵約束的存在,導致insert頻頻報錯,批量執行sql語句又是順序執行,沒辦法我只好手動輸入。

然後輸入到一半靈光一閃,為什麼不先把外鍵約束全部禁用先呢?

於是我百度到以下資料:

oracle 刪除(所有)約束 禁用(所有)約束 啟用(所有)約束

執行以下sql產生的語句即可

1刪除所有外鍵約束

select 'alter table '||table_name||' drop constraint '||constraint_name||';' from user_constraints where constraint_type='R' 

2禁用所有外鍵約束

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

3啟用所有外鍵約束

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

在SQL Plus中輸入語句後,產生了很多語句,這些語句其實是沒執行的,複製下來執行一遍就好了。
然後我們可以根據這個指令碼一樣的sql語句進行拼裝,得到我們需要的語句:

禁用所有外鍵約束:

select 'ALTER TABLE "QIANHAI"."'||table_name||'" MODIFY CONSTRAINT "'||constraint_name||'" DISABLE;' from user_constraints where constraint_type='R' 

啟用所有外鍵約束:

select 'ALTER TABLE "QIANHAI"."'||table_name||'" MODIFY CONSTRAINT "'||constraint_name||'" ENABLE;' from user_constraints where constraint_type='R';

關於Oracle批量執行sql語句之禁用所有表的外鍵的相關內容,就給大家介紹這麼多,後續還會持續更新,感興趣的朋友請繼續關注云棲社區網站,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.