SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Constraint Execution Order


Constraint Execution Order

Author
Message
sandhya_vemulla
sandhya_vemulla
Old Hand
Old Hand (390 reputation)Old Hand (390 reputation)Old Hand (390 reputation)Old Hand (390 reputation)Old Hand (390 reputation)Old Hand (390 reputation)Old Hand (390 reputation)Old Hand (390 reputation)

Group: General Forum Members
Points: 390 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.
Lowell
Lowell
SSC Guru
SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)

Group: General Forum Members
Points: 73245 Visits: 40960
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
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search