?
Solved

Dependent column constraint

Posted on 2011-03-22
7
Medium Priority
?
404 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 1000 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 1000 total points
ID: 35191296
if col1 is already restricted to be not null then you can remove the "OR col1 is NULL"
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

809 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