如何在Oracle中向表中添加列的特定值约束?

时间:2014-04-17 07:31:59

标签: sql oracle oracle11g

我的表员工有一个名为“性别”的列。 为什么这种改变不起作用:

ALTER TABLE employee
WITH NOCHECK ADD CONSTRAINT EMP_SEX
CHECK (Sex in ('M','F'));

或者对于表格库,这不起作用:

ALTER TABLE Account
WITH NOCHECK ADD CONSTRAINT chk_account_type CHECK (Type IN ('saving', 'credit', 'HOME LOAN', 'PERSONAL LOAN', 'TERM DEPOSIT', 'CHECK', 'ISAVER', 'SHARE' ));

错误:

SQL> ALTER TABLE employee
WITH NOCHECK ADD CONSTRAINT EMP_SEX
CHECK (Sex in ('M','F'));
  2    3  WITH NOCHECK ADD CONSTRAINT EMP_SEX
*
ERROR at line 2:
ORA-01735: invalid ALTER TABLE option

甚至我试过这个 - >

ALTER TABLE Account
ADD CONSTRAINT chk_account_type CHECK ( Type = 'a'));

2 个答案:

答案 0 :(得分:0)

语法错误。

这是一个有效的例子:

SQL>
SQL> create table t(
  2     i_has_cheezburger varchar2(20)
  3  );

Table created.

SQL>
SQL> alter table t 
       add constraint check_burger check(i_has_cheezburger = 'CHEEZBURGER');

Table altered.

SQL>
SQL> insert into t values('CHEEZBURGER');

1 row created.

SQL> insert into t values('TOMATOES');
insert into t values('TOMATOES')
*
ERROR at line 1:
ORA-02290: check constraint (RNBN.CHECK_BURGER) violated

答案 1 :(得分:0)

Oracle中没有SQL Server中的WITH NOCHECK