Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using subquery in a Check statement in Oracle

Tags:

So I was trying to work this out but it seems that the last line (the check) doesn't allow sub queries in it. Any way to make this work Oracle?

CREATE TABLE Tank (     n_id            int,     day             date,     level           int,     CONSTRAINT pk_w_td PRIMARY KEY (n_id,day),     CONSTRAINT fk_w_td_tan FOREIGN KEY (n_id) REFERENCES Tanks ON DELETE CASCADE,     CHECK (level > 0 AND level <= (SELECT capacity FROM Tanks WHERE Tanks.n_id = TanksDay.n_id)) ); 

Here is the error info:

Error at Command Line:7 Column:32 Error report: SQL Error: ORA-02251: subquery not allowed here 02251. 00000 -  "subquery not allowed here" *Cause:    Subquery is not allowed here in the statement. *Action:   Remove the subquery from the statement. 
like image 507
devoured elysium Avatar asked Nov 01 '10 16:11

devoured elysium


People also ask

Can we use subquery in check constraint?

Sub-queries are not allowed in this context. Only scalar expressions are allowed. I have looked at this question about Check Constraint - Subqueries are not allowed in this context.

Can we use subquery in if statement in Oracle?

You cannot include SQL inside an IF statement. hi, Collect the sub query result to a collection and loop through it. It is the feasible solution in this case.

Can you use a subquery in a case statement?

Subquery ExampleThe following query example uses the Subquery inside a Case Statement in SQL Server. First, the Subquery will execute and finds the Average of the Sales amount. Next, it will check whether the Sales are greater than the Average Sales (1970.9055).

How do I create a sub query in Oracle?

In Oracle, a subquery is a query within a query. You can create subqueries within your SQL statements. These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.


2 Answers

There are three basic ways to solve this sort of problem since CHECK constraints cannot be based on a query.

Option 1: Triggers

The most simplistic approach would be to put a trigger on TANK that queries TANKS and throws an exception if the LEVEL exceeds CAPACITY. The problem with this sort of simplistic approach, though, is that it is nearly impossible to handle concurrency issues correctly. If session 1 decreases the CAPACITY, then session 2 increases the LEVEL, and then both transactions commit, triggers will not be able to detect the violation. This may not be an issue if one or both of the tables are seldom modified, but in general it's going to be an issue.

Option 2: Materialized views

You can solve the concurrency issue by creating an ON COMMIT materialized view that joins the TANK and TANKS table and then creating a CHECK constraint on the materialized view that verifies that the LEVEL <= CAPACITY. You can also avoid storing the data twice by having the materialized view contain just data that would violate the constraint. This will require materialized view logs on both the base tables which will add a bit of overhead to inserts (though less than using triggers). Pushing the check to commit-time will solve the concurrency issue but it introduces a bit of an exception management issue since the COMMIT operation can now fail because the materialized view refresh failed. Your application would need to be able to handle that problem and to alert the user to that fact.

Option 3: Change the data model

If you have a value in table A that depends on a limit in table B, that may indicate that the limit in B ought to be an attribute of table A (instead of or in addition to being an attribute of table B). It depends on the specifics of your data model, of course, but it's often worth considering.

like image 199
Justin Cave Avatar answered Oct 11 '22 13:10

Justin Cave


No unfortunately CHECK constraints cannot contain subqueries - see documentation.

like image 20
Tony Andrews Avatar answered Oct 11 '22 14:10

Tony Andrews