在没有给出任何名称的情况下禁用plsql中的约束

时间:2017-07-01 21:01:34

标签: database oracle plsql

这是我的代码

Create table Issue(
Lib_Issue_Id Number(10) Primary Key,
Book_No number(6),
Member_Id number(5),
Issue_Date Date,
Return_Date Date,
check (Issue_date<Return_Date),
FOREIGN KEY (Book_No) REFERENCES Books(Book_No),
FOREIGN KEY (Member_Id) REFERENCES Member(Member_Id)
);

现在创建表后。我想禁用所有约束,但我没有给它任何名称,那我怎么能禁用约束.. ???

1 个答案:

答案 0 :(得分:1)

显而易见的答案是&#34;将名字放在约束上,后来的程序员将永远唱出你的名字,并且#34;。但是,如果你的同事永远感激你并不是the docs for the ALL_CONSTRAINTS view can be found here。使用游标从视图中读取约束名称,然后禁用它们,如:

FOR aRow IN (SELECT *
               FROM ALL_CONSTRAINTS
               WHERE TABLE_NAME = 'ISSUE' AND
                     CONSTRAINT_TYPE = 'R')
LOOP
  EXECUTE IMMEDIATE 'ALTER TABLE ' || 
                     aRow.TABLE_NAME ||
                    ' MODIFY CONSTRAINT ' ||
                    aRow.CONSTRAINT_NAME ||
                    ' DISABLE';

  DBMS_OUTPUT.PUT_LINE('FK constraint ' || aRow.CONSTRAINT_NAME || ' disabled');
END LOOP;