Search code examples
sqldatabaseoraclecheck-constraints

How to make a CHECK constraint that allow user to only input DateTime from today and onwards?


As with the title, I'm trying to solve my CHECK constraint where user are only able to enter Date and Time from today and onward.

Here is the SQL developer version that I'm using now (I queried the version):
- Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
- PL/SQL Release 12.2.0.1.0 - Production

What I have tried:

ALTER TABLE job_assignment
    ADD CONSTRAINT chk_start_date CHECK ( start_date >=
    SYSDATE()
);

but this gave me an error, ORA-00907: missing right parenthesis.

And I have also come across this,
Conditions of check constraints cannot contain the following constructs:
- Subqueries and scalar subquery expressions
- Calls to the functions that are not deterministic (CURRENT_DATE, CURRENT_TIMESTAMP, DBTIMEZONE, LOCALTIMESTAMP, SESSIONTIMEZONE, SYSDATE, SYSTIMESTAMP, UID, USER, and USERENV)

Need help pls.


Solution

  • That would be a trigger, e.g.

    create or replace trigger trg_biu_jobas
      before insert or update on job_assignment
      for each row
    begin
      if :new.start_date < sysdate then
         raise_application_error(-20001, 'Start date must be after "now"');
      end if;
    end;