我想在表名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


请建议我如何添加约束。谢谢。

最佳答案

如注释中所述,如果仅希望约束应用于将来的数据更改,则可以使用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;


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

10-04 20:11