question

Nani avatar image
Nani asked

sql constraint Vs triggers

For a particular column if both trigger and constraint are assigned. which one is going fire first and why?

t-sqltriggerconstraint
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Steinar avatar image
Steinar answered

My understanding is that the firing order is as follows:

  1. Instead of trigger
  2. Constraints
  3. After trigger

http://msdn.microsoft.com/en-us/library/aa224775(SQL.80).aspx

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

SQL Kiwi avatar image
SQL Kiwi answered

You also asked for the reason. An INSTEAD OF trigger has to fire first since it completely replaces the operation. Then constraints are checked (since we now have all the new data in place). Finally, AFTER triggers fire. AFTER triggers are not part of the query plan for the original data modification - a new plan is created for them - so logically they must come last.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.