Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2008
    Posts
    8

    Unanswered: Check constraint to validate Date of Death with Date of Birth

    I have 2 fields in my table, DateOfBirth & DateOfDeath of data type DATE
    Now DOD must be equal or greater than DOB
    How can I write check constraint for DOD to check it is >= DOB ?

    DOB DATE
    DOD DATE

    I have tried,
    DOB DATE,
    DOD DATE CONSTRAINT CHK_DOD (DOD >= DOB)
    This gives error ORA-02438: Column check cosntraint cannot reference other columns


    How can I write check constraint for DOD to check it is >= DOB ?

  2. #2
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    Don't create a COLUMN constraint - create a TABLE one instead!
    Code:
    SQL> CREATE TABLE bd
      2    (dob DATE,
      3     dod DATE,
      4         CONSTRAINT ch_db CHECK (dod >= dob)
      5    );
    
    Table created.
    
    SQL> INSERT INTO bd VALUES (SYSDATE, SYSDATE + 1);
    
    1 row created.
    
    SQL> INSERT INTO bd VALUES (SYSDATE, SYSDATE - 1);
    INSERT INTO bd VALUES (SYSDATE, SYSDATE - 1)
    *
    ERROR at line 1:
    ORA-02290: check constraint (SCOTT.CH_DB) violated
    
    
    SQL>

  3. #3
    Join Date
    Apr 2008
    Posts
    8
    Thanks Littlefoot, It's working - Appreciated

    I have 1 more query on date type data

    I want to insert data through query...in this table
    CREATE TABLE bd (
    dob DATE,
    dod DATE,
    CONSTRAINT ch_db CHECK (dod >= dob)
    );

    Right now I can only insert date in DD-MMM-YY format
    for ex:
    INSERT INTO bd VALUES ('10-May-97', '10-May-05');

    so here problem is, my expectation 10-May-97 = 10-May-1997 but it is taking it as 10-May-2097 and inserts date

    Would you please let me know, how to restrict oracle to accept only such kind a format for date... like DD-MM-YYYY only

    Thanks

  4. #4
    Join Date
    Mar 2007
    Posts
    623
    '10-May-97' is a STRING.
    TO_DATE( '10-May-97', 'DD-Mon-RR' ) is a DATE.
    Never depend on your NLS_DATE_FORMAT setting.

    For Oracle format models, consult the documentation found eg. online on http://tahiti.oracle.com/.
    For 10gR2, it is placed at http://download.oracle.com/docs/cd/B...004.htm#i34924.
    Especially have a look at http://download.oracle.com/docs/cd/B...04.htm#i116004.
    But it does not people born before 1950. But, tell me, how would you tell to which century does '05-Apr-07' birth-date belong.
    If you somehow tweak it, another problems start in 2050 (your successors will love to fix it).

    In my opinion, year has always four digits. Seems that people did not learn it from Y2K problem .

  5. #5
    Join Date
    Apr 2008
    Posts
    8
    Thanks FlyBoy

    I got it

    Appretiated

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •