Check constraints must be deterministic. That is, a particular line must always satisfy the constraint, or it must always not satisfy the constraint. But SYSDATE
is inherently non-deterministic since the return value is constantly changing. Thus, you cannot define a CHECK
constraint that calls SYSDATE
or any other user-defined function.
If you try to reference SYSDATE
in the constraint definition, you will receive an error message
SQL> ed Wrote file afiedt.buf 1 create table t( 2 birth_date date check( birth_date between date '1900-01-01' and 3 sysdate ) 4* ) SQL> / sysdate ) * ERROR at line 3: ORA-02436: date or system variable wrongly specified in CHECK constraint
You can create a CHECK
constraint where the minimum and maximum dates were hardcoded, but that would not be particularly practical, since you had to constantly reduce and recreate the constraint.
SQL> ed Wrote file afiedt.buf 1 create table t( 2 birth_date date check( birth_date between date '1900-01-01' and 3 date '2011-12-08' ) 4* ) SQL> / Table created.
A practical way to enforce this requirement is to create a trigger in the table
CREATE OR REPLACE TRIGGER check_birth_date BEFORE INSERT OR UPDATE ON employee FOR EACH ROW BEGIN IF( :new.emp_dob < date '1900-01-01' or :new.emp_dob > sysdate ) THEN RAISE_APPLICATION_ERROR( -20001, 'EMployee date of birth must be later than Jan 1, 1900 and earlier than today' ); END IF; END;
source share