Solved

How can I perform a select in an check constraint?

Posted on 2004-03-24
4
2,377 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

Suggested Solutions

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
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…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

808 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