Solved

How can I perform a select in an check constraint?

Posted on 2004-03-24
4
2,375 Views
Last Modified: 2013-12-11
Hi,

I'd like to do the following:
ALTER TABLE local_sale_terms
  ADD CONSTRAINT constr_check_cancelled
CHECK (NOT EXISTS (SELECT 1
                     FROM local_sale_terms
                    WHERE cancelled='N'
                    GROUP BY parent_no,date_of_term,cancelled
                   HAVING COUNT(*)>1))

But Oracle complains about a query in the constraint.

How can i enforce having only ONE Uncancelled row?
I can have many cancelled rows, but only one uncancelled row.

a unique index on (parent_no,date_of_term,cancelled) is therefore inappropriate.

help
thanks
adam
0
Comment
Question by:schwartz8a
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
william_jwd earned 65 total points
Comment Utility
I dont think that you can do it in check constraint.  You can do this using triggers.  You should do the checking in two triggers, before insert and before update.
0
 

Author Comment

by:schwartz8a
Comment Utility
thanks.
I know i can use triggers.

But Joe Celko's book, SQL for Smarties, says that queries within check constraints are preferable to triggers. He even gives an example similar to the one i posted.

any other ideas?
0
 
LVL 6

Assisted Solution

by:musdu
musdu earned 60 total points
Comment Utility
Hi,

according to Oracle documentation you can not use such a statement in check constraint. (Second item about your problem)
Use triggers.

 Restrictions on CHECK Constraints

A CHECK integrity constraint requires that a condition be true or unknown for every row of the table. If a statement causes the condition to evaluate to false, then the statement is rolled back. The condition of a CHECK constraint has the following limitations:

    * The condition must be a Boolean expression that can be evaluated using the values in the row being inserted or updated.

    * The condition cannot contain subqueries or sequences.

    * The condition cannot include the SYSDATE, UID, USER, or USERENV SQL functions.

    * The condition cannot contain the pseudocolumns LEVEL, PRIOR, or ROWNUM;
0
 
LVL 8

Expert Comment

by:william_jwd
Comment Utility
schwartz8a,

What you say is right...  As per the SQL standards SQL queries are allowed in check constraint, but I dont think that it is possible in oracle.

William.
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
Stay Alert! 13 62
how to tune this sql query 61 99
Oracle SQL Syntax 8 56
Oracle 12c patching 1 27
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example, show how to take different types of Oracle backups using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

763 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

8 Experts available now in Live!

Get 1:1 Help Now