在轉(zhuǎn)移數(shù)據(jù)庫,進(jìn)行數(shù)據(jù)導(dǎo)入的時(shí)候,遇到一件麻煩事,就是表間外鍵約束的存在,導(dǎo)致insert頻頻報(bào)錯(cuò),批量執(zhí)行sql語句又是順序執(zhí)行,沒辦法我只好手動(dòng)輸入。
然后輸入到一半靈光一閃,為什么不先把外鍵約束全部禁用先呢?
于是我百度到以下資料:
oracle 刪除(所有)約束 禁用(所有)約束 啟用(所有)約束
執(zhí)行以下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中輸入語句后,生成了很多語句,這些語句其實(shí)是沒執(zhí)行的,復(fù)制下來執(zhí)行一遍就好了。
然后我們可以根據(jù)這個(gè)腳本一樣的sql語句進(jìn)行拼裝,得到我們需要的語句:
禁用所有外鍵約束:
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';
關(guān)于Oracle批量執(zhí)行sql語句之禁用所有表的外鍵的相關(guān)內(nèi)容,就給大家介紹這么多,后續(xù)還會(huì)持續(xù)更新,感興趣的朋友請繼續(xù)關(guān)注VeVb武林網(wǎng)網(wǎng)站,謝謝!