Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Oracle > Check constraint to validate Date of Death with Date of Birth

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-04-08, 17:16
NirajPatel NirajPatel is offline
Registered User
 
Join Date: Apr 2008
Posts: 8
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 ?
Reply With Quote
  #2 (permalink)  
Old 04-04-08, 17:49
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 2,716
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>
Reply With Quote
  #3 (permalink)  
Old 04-04-08, 21:31
NirajPatel NirajPatel is offline
Registered User
 
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
Reply With Quote
  #4 (permalink)  
Old 04-04-08, 23:25
flyboy flyboy is offline
Registered User
 
Join Date: Mar 2007
Posts: 114
'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 .
Reply With Quote
  #5 (permalink)  
Old 04-05-08, 01:24
NirajPatel NirajPatel is offline
Registered User
 
Join Date: Apr 2008
Posts: 8
Thanks FlyBoy

I got it

Appretiated
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On