How can I drop a “not null” constraint in Oracle when I don't know the name of the constraint?

前端 未结 7 1148
花落未央
花落未央 2020-12-07 20:10

I have a database which has a NOT NULL constraint on a field, and I want to remove this constraint. The complicating factor is that this constraint has a system-defined nam

7条回答
  •  庸人自扰
    2020-12-07 21:01

    alter table MYTABLE modify (MYCOLUMN null);
    

    In Oracle, not null constraints are created automatically when not null is specified for a column. Likewise, they are dropped automatically when the column is changed to allow nulls.

    Clarifying the revised question: This solution only applies to constraints created for "not null" columns. If you specify "Primary Key" or a check constraint in the column definition without naming it, you'll end up with a system-generated name for the constraint (and the index, for the primary key). In those cases, you'd need to know the name to drop it. The best advice there is to avoid the scenario by making sure you specify a name for all constraints other than "not null". If you find yourself in the situation where you need to drop one of these constraints generically, you'll probably need to resort to PL/SQL and the data-definition tables.

提交回复
热议问题