alter table有错误添加约束

时间:2015-05-09 10:06:36

标签: sql oracle constraints add alter-table

我想在表名Account,属性名Type中添加约束。 在Type中,我想添加帐户类型,例如:Saving, Credit Card, Home Loan, Personal Loan, Fixed Deposit, Current, iSaver.

这是我的代码:

ALTER TABLE ACCOUNT 
ADD CONSTRAINT AccountCK_Type
CHECK (TYPE IN('Saving','Credit Card','Home Loan','Personal Loan','Fixed Deposit','Current','iSaver'));

错误显示:

>Error report -
>
>SQL Error: ORA-02293: cannot validate (SHT461.ACCOUNTCK_TYPE) - check constraint violated
>02293. 00000 - "cannot validate (%s.%s) - check constraint violated"
>
>*Cause:    an alter table operation tried to validate a check constraint to
>           populated table that had nocomplying values.
>
>*Action:   Obvious

请告诉我如何添加约束。感谢。

1 个答案:

答案 0 :(得分:3)

如果您只希望约束应用于将来的数据更改,正如您在评论中所说,那么您可以使用NOVALIDATE clause忽略其他现有值:

ALTER TABLE ACCOUNT 
ADD CONSTRAINT AccountCK_Type
CHECK (TYPE IN('Saving','Credit Card','Home Loan','Personal Loan',
  'Fixed Deposit','Current','iSaver'))
ENABLE NOVALIDATE;

否则,在创建约束之前,您必须清理现有数据 - 删除或更正该列中任何其他值的行。