从表 oracle 中删除所有约束

2022-01-20 00:00:00 sql foreign-keys oracle plsql

伙计们,我的问题是我不知道我添加到表中的约束名称.但我需要删除或禁用所有这些外键约束.但是怎么做呢?

SQL> desc orders;


Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ORDER_ID                                  NOT NULL VARCHAR2(10)
 PRODUCT_ID                                         VARCHAR2(10)
 DATE_OF_ORDER                                      TIMESTAMP(6)
 CUST_ID                                            VARCHAR2(10)
 QUANTITY                                           NUMBER(38)
 TOTAL_PRICE                                        FLOAT(10)
 DELIVERY_STATUS                                    VARCHAR2(10)

推荐答案

相关的外约束可以通过user_constraints字典视图来确定,可以通过下面的代码块

The related foreign constraints might be determined by using user_constraints dictionary view, and you can disable the foreign key constraint of this table by the following code block

BEGIN
  FOR c IN
    (
      SELECT * 
        FROM user_constraints c
       WHERE c.constraint_type = 'R'
         AND c.table_name = 'ORDERS')
  LOOP
    EXECUTE IMMEDIATE 'ALTER TABLE '||c.table_name||
                      ' DISABLE CONSTRAINT '||c.constraint_name;
  END LOOP;
END;  
/

相关文章