Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Constraint Execution Order Expand / Collapse
Author
Message
Posted Monday, April 20, 2009 7:49 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, February 23, 2010 8:25 PM
Points: 372, Visits: 25
Hi
If i define a RULE and Check constraint on the same column then which gets executed first.

CREATE TABLE [dbo].[ACCOUNT](
[ACCOUNT_ID] [int] NULL,
[ACCOUNT_LOCATION] [varchar](10) NULL,
[CUSTOMER_ID] [int] NULL,
[CHECK_IN_AMT] [int] NULL,
[SAVINGS_AMT] [int] NULL
)

CREATE RULE Amount_rule
AS
(@CHECK_IN_AMT >= 1000)

EXEC sp_bindrule 'Amount_rule', 'account.CHECK_IN_AMT'

ALTER TABLE ACCOUNT
ADD CONSTRAINT cc_MinBal
CHECK (CHECK_IN_AMT > 800);

I am newbie in SQL Server and trying to learn the concepts.
Post #701116
Posted Monday, April 20, 2009 8:56 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:20 AM
Points: 12,916, Visits: 32,080
they don't really get executed first or second.... it's more like ALL constraints, including whether a column is nullable or not, all get evaluated prior to the the insert or update...if any constraint gets violated, an error is raised.

while it's true that in your example, if i put 100 dollars in the column, and there are two different constraints, one checking > 800 and the other for > 1000, only one of them is going to be returned, but i don't believe you can decide which occurs first.

it could be on my server, the check constraint returns the error, and on your server, the bind rule constraint returns as the error.

I've never seen any info on the order of precedence.

FYI, when i try to insert, the rule raises an error on my machine:
insert into account([CHECK_IN_AMT]) values (100)

Msg 513, Level 16, State 0, Line 1
A column insert or update conflicts with a rule imposed by a previous CREATE RULE statement. The statement was terminated. The conflict occurred in database 'tempdb', table 'dbo.ACCOUNT', column 'CHECK_IN_AMT'.

The statement has been terminated.



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #701130
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse