Solved

How can I perform a select in an check constraint?

Posted on 2004-03-24
4
2,378 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
ID: 10666753
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
ID: 10666829
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
ID: 10667143
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
ID: 10668017
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.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
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.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

829 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