CHECK constraint on date of birth?

前端 未结 5 850
南方客
南方客 2020-12-07 02:49

I am creating my employee table in Oracle and for emp_date I would like to make it so the date of birth is not way back in the past and can not be set in the future?

相关标签:
5条回答
  • 2020-12-07 03:25

    You need to create a table with DATE_OF_BIRTH and the PRESENT_DATE i.e I am used DATE_JOINING and then use an AGE default value by subtracting the DATE_JOINING minus DATE_OF_BIRTH.

    Finally set CHECK constraint to AGE is it greater than your requirement i.e I am set 20.

    See below

    Createing Table Employee

    CREATE TABLE EMPLOYEE(
        NAME VARCHAR(20) NOT NULL,
        DOB DATE NOT NULL,
        DATE_JOINING DATE DEFAULT NOW(),
        AGE INTEGER DEFAULT TIMESTAMPDIFF(YEAR,DOB,DATE_JOINING) CHECK(AGE>20)
        );
    

    Inserting Data to the Table

    INSERT INTO EMPLOYEE(NAME,DOB) VALUES('NAYAK','1998-2-3');
    Query OK, 1 row affected, 1 warning (0.128 sec)
    

    Age is greater than 20 so it is inserted.

    SELECT * FROM EMPLOYEE;
    +-------+------------+--------------+------+
    | NAME  | DOB        | DATE_JOINING | AGE  |
    +-------+------------+--------------+------+
    | NAYAK | 1998-02-03 | 2019-10-15   |   21 |
    +-------+------------+--------------+------+
    1 row in set (0.001 sec)
    

    Now Insert a DOB less than 20 years

    INSERT INTO EMPLOYEE(NAME,DOB) VALUES('NIRAJAN','2005-2-3');
    ERROR 4025 (23000): CONSTRAINT `EMPLOYEE.AGE` failed for `NAYAK`.`EMPLOYEE`
    

    Not Inserted because of check.

    0 讨论(0)
  • 2020-12-07 03:26

    You can use interval directly:

    ALTER TABLE "students"
      ADD CONSTRAINT "students_must_be_over_18" CHECK (
        CURRENT_TIMESTAMP - "date_of_birth" > INTERVAL '18 years'
      );
    
    0 讨论(0)
  • 2020-12-07 03:29

    What about another workaround

    Write sysdate into a column and use it for validation. This column might be your audit column (For eg: creation date)

    CREATE TABLE "AB_EMPLOYEE22"
    (
       "NAME"     VARCHAR2 ( 20 BYTE ),
       "AGE"      NUMBER,
       "SALARY"   NUMBER,
       "DOB"      DATE,
       "DOJ"      DATE DEFAULT SYSDATE
    );
    
    Table Created    
    
    ALTER TABLE "AB_EMPLOYEE22" ADD CONSTRAINT
    AGE_CHECK CHECK((ROUND((DOJ-DOB)/365)) = AGE) ENABLE;
    
    Table Altered
    
    0 讨论(0)
  • 2020-12-07 03:42

    Another way would be to create a domain birthdate, with the constraint built into the domain. This will allow you to reuse the same type in other table definitions.

    CREATE DOMAIN birthdate AS date DEFAULT NULL
        CHECK (value >= '1900-01-01' AND value <= now())
        ;
    
    CREATE TABLE employee
        ( empno INTEGER NOT NULL PRIMARY KEY
        , dob birthdate NOT NULL
        ...
        );
    
    0 讨论(0)
  • 2020-12-07 03:46

    Check constraints must be deterministic. That is, a particular row must always satisfy the constraint or it must always fail to satisfy the constraint. But SYSDATE is inherently non-deterministic since the returned 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'll get an error

    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 could create a CHECK constraint where both the minimum and maximum date were hard-coded but that would not be particularly practical since you'd have to constantly drop 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.
    

    The practical way to enforce this sort of requirement would be to create a trigger on 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;
    
    0 讨论(0)
提交回复
热议问题