Solved

Dependent column constraint

Posted on 2011-03-22
7
387 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 73

Expert Comment

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

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 73

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 73

Expert Comment

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Oracle SQL Char delimited error 5 30
Format Number Field 10 39
percentage remaining 6 40
Oracle SQL Select Statement 19 58
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and theā€¦
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now