All. SQL--the order in which constraints, Check, triggers are executed

Source: Internet
Author: User
Tags getdate

Let's look at the sequencing of constraints, Check, and triggers in this process, and perhaps deepen our understanding of the transaction.

CREATE TABLE TestTable

(

ID INT CONSTRAINT pk_testtable_id PRIMARY KEY,

UniqueID INT UNIQUE,

Number INT CHECK (number >= and number<=100),

Nonnull INT not NULL

);

CREATE TABLE logtable

(

Logdesc VARCHAR (50),

Logdate DATETIME

);

CREATE TRIGGER [tri_testtable] on TestTable

After Insert,update

As

INSERT into logtable VALUES (' TestTable ', GETDATE ());

Verification step, first step Insert new value, OK

INSERT into TestTable (id,uniqueid,number,nonnull) VALUES (1,1,11,10);

Insert a record that does not meet the constraints and Check conditions, prompting you cannot insert the value NULL into the column ' nonnull '

INSERT into TestTable (id,uniqueid,number,nonnull) VALUES (1,1,1,null);

Change NULL value to 10, continue insert, prompt violation of PRIMARY KEY constraint ' pk_testtable_id '

INSERT into TestTable (id,uniqueid,number,nonnull) VALUES (1,1,1,10);

Change the ID from 1 to 2 to continue inserting, prompting for violation of the UNIQUE KEY constraint ' uq__testtable__023d5a04 '

INSERT into TestTable (id,uniqueid,number,nonnull) VALUES (2,1,1,10);

Change UniqueID from 1 to 2, continue inserting, prompt for Nsert statement conflict with CHECK constraint "Ck__testtable__numbe__03317e3d"

INSERT into TestTable (id,uniqueid,number,nonnull) VALUES (2,2,1,10);

Change number from 1 to 11, insert OK

INSERT into TestTable (id,uniqueid,number,nonnull) VALUES (2,2,11,10);

Note that the trigger is executed only after successful execution, and if a link fails, the entire transaction is rolled back.

Modify the trigger again, note that this is set to number 1 to determine whether check and constraint judgment is still required after the trigger execution

ALTER TRIGGER [tri_testtable] on TestTable

After Insert,update

As

DECLARE @ID int, @UniqueID int, @Number int, @NonNULL int

SELECT @ID =id, @UniqueID =uniqueid, @Number =number, @NonNULL =nonnull

From INSERTED

SET @Number =1

SET @NonNULL =null

INSERT into logtable VALUES (' TestTable ', GETDATE ());

UPDATE testtable SET [email protected],[email protected] where [email protected]

Execute statements that meet all constraints and Check conditions

INSERT into TestTable (id,uniqueid,number,nonnull) VALUES (3,3,10,10);

Hint msg 515, Level 16, State 2, procedure tri_testtable, line 9th

Cannot insert value NULL into column ' nonnull ', table ' test.dbo.TestTable '; column does not allow null values. UPDATE failed.

SELECT * FROM TestTable

SELECT * from Logtable

This can be seen

When an Insert statement executes, the constraint is first validated, and the constraint itself has a sequence of precedence

1. Verifying non-null constraints

2. Verifying PRIMARY KEY constraints

3. Verification of uniqueness constraints

Verify the relevant Check again

The final execution of the trigger, if the trigger must also be guaranteed not to violate the relevant constraints and Check


This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1622854

All. SQL--the order in which constraints, Check, triggers are executed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.