这是网上能够搜索到的比较可信的结果:
Unfortunately you can't use SYSDATE in a check constraint as it is a dynamic Oracle variable. A better way to enforce this constraint would be to use a trigger. For example:
CREATE OR REPLACE TRIGGER trg_emp_dob BEFORE INSERT OR UPDATE ON emp
BEGIN
IF :new.dob > SYSDATE THEN
RAISE_APPLICATION_ERROR (num => -20000, msg => 'DOB Cannot be in the future');
END IF;
END;
不过嘿嘿,我可不信邪,万事问大哥啊,于是在asktom上找到了这个解决方案:
Just create an additional column using as default SYSDATE. Then you can use this column to compare.
And if you want to hide this column create a view around it...
SQL> create table t
2 (x date,
3 y date default sysdate,
4 constraint x_y check
5 (
6 x >= y
7 )
8 );
Table created.
SQL>
SQL> create view v as select x from t;
View created.
SQL>
SQL> insert into v values (sysdate);
1 row created.
SQL>
SQL> commit;
Commit complete.
SQL>
SQL> insert into v values (sysdate-1);
insert into v values (sysdate-1)
*
ERROR at line 1:
ORA-02290: check constraint (ADBM.X_Y) violated
总之oracle不能非常漂亮的解决这个问题,这里两个方案你看着选吧。
Unfortunately you can't use SYSDATE in a check constraint as it is a dynamic Oracle variable. A better way to enforce this constraint would be to use a trigger. For example:
CREATE OR REPLACE TRIGGER trg_emp_dob BEFORE INSERT OR UPDATE ON emp
BEGIN
IF :new.dob > SYSDATE THEN
RAISE_APPLICATION_ERROR (num => -20000, msg => 'DOB Cannot be in the future');
END IF;
END;
不过嘿嘿,我可不信邪,万事问大哥啊,于是在asktom上找到了这个解决方案:
Just create an additional column using as default SYSDATE. Then you can use this column to compare.
And if you want to hide this column create a view around it...
SQL> create table t
2 (x date,
3 y date default sysdate,
4 constraint x_y check
5 (
6 x >= y
7 )
8 );
Table created.
SQL>
SQL> create view v as select x from t;
View created.
SQL>
SQL> insert into v values (sysdate);
1 row created.
SQL>
SQL> commit;
Commit complete.
SQL>
SQL> insert into v values (sysdate-1);
insert into v values (sysdate-1)
*
ERROR at line 1:
ORA-02290: check constraint (ADBM.X_Y) violated
总之oracle不能非常漂亮的解决这个问题,这里两个方案你看着选吧。