alter table 添加约束时出错

Having error for alter table to add constraint

我想为我的 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

请教我如何添加约束。谢谢。

如果您只希望约束适用于未来的数据更改,如您在评论中所说,那么您可以使用 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;

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