Solved

Dependent column constraint

Posted on 2011-03-22
7
392 Views
Last Modified: 2012-05-11
I would like to setup a  dependent column constraint something like so:

check (col1 in('Y') then col2 is not null);  So basically if col1 == 'Y' col2 cannot be null.

Thanks!
0
Comment
Question by:ODOTServer
  • 5
  • 2
7 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 35191256
CHECK (case when col1 = 'Y' and col2 is not null then 1 end = 1)
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 250 total points
ID: 35191286
oops, missed some conditions

CHECK (CASE WHEN col1 = 'Y' AND col2 IS NOT NULL THEN 1 WHEN col1 != 'Y' OR col1 IS NULL THEN 1 ELSE 0 END = 1)
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 250 total points
ID: 35191296
if col1 is already restricted to be not null then you can remove the "OR col1 is NULL"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ODOTServer
ID: 35191379
Okay that allowed null on col2 when col1 ='N' which is good.
It did not enforce col2 not null when col1 = 'Y which is bad.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35191393
which "that"  ?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35191438
Here's my test case,  the first 4 inserts succeed, the 5th fails
If that's not correct, please extend with additional sample data and expected results

CREATE TABLE testtable
(
    col1   VARCHAR2(1),
    col2   VARCHAR2(1)
);

ALTER TABLE sds.testtable ADD
CONSTRAINT ck
 CHECK (CASE WHEN col1 = 'Y' AND col2 IS NOT NULL THEN 1 WHEN col1 != 'Y' OR col1 IS NULL THEN 1 ELSE 0 END = 1)
 ENABLE
 VALIDATE;

INSERT INTO testtable(col1, col2)
     VALUES (NULL, NULL);

INSERT INTO testtable(col1, col2)
     VALUES ('N', NULL);

INSERT INTO testtable(col1, col2)
     VALUES ('N', 'x');

INSERT INTO testtable(col1, col2)
     VALUES ('Y', 'z');

INSERT INTO testtable(col1, col2)
     VALUES ('Y', NULL);

0
 

Author Comment

by:ODOTServer
ID: 35191511
Here is what it looks like (CASE WHEN col1 = 'Y' AND col2 IS NOT NULL THEN 1 WHEN col2!= 'Y' THEN 1 ELSE 0 END = 1)

col1 not null default 'N' and has a check col1 in('Y','N').

Thanks !
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question