The CHECK constraints unlike behaviours

The CHECK constraints enforce integrity by limiting the values that are accepted by one or more columns. You can create a CHECK constraint with any logical expression that returns TRUE or FALSE based on the logical operators.

CHECK constraints are similar to FOREIGN KEY constraints in that they control the values that are put in a column. The difference is in how they determine which values are valid: FOREIGN KEY constraints obtain the list of valid values from another table, and CHECK constraints determine the valid values from a logical expression. You can read more about them here.

In this article I’ll show how can insertion of data be different under unlike settings for a same constraint in a Table. Three examples will be demonstrated.

Example 1

This example creates a database with all the default settings for a database creation statement. A table dbo.Orders is created with a primary key and CHECK constraint on the OrderStatus column. Then some data is inserted.

Now I run the next batch to insert four rows.

What happens?

When the insert batch runs, each insert is treated separately as an implicit transaction. The first two insert rows pass the CHECK constraint and get inserted. The third insert row fails the CHECK constraint and throws an error without inserting the row.

Msg 547, Level 16, State 0, Line 17
The INSERT statement conflicted with the CHECK constraint “Orders_Status_Code”. The conflict occurred in database “check_db_test”, table “dbo.Orders”, column ‘OrderStatus’.

The fourth row does not fail the CHECK constraint, since a NULL is an unknown value, and the row is inserted.

I also want to note that only three execution plans are generated for the INSERT batch from above.

The next query lists all inserted rows.

Example 2

This example overviews the case when the XACT_ABORT is set to ON.

XACT_ABORT specifies whether SQL Server automatically rolls back the current transaction when a Transact-SQL statement raises a run-time error.

There are often situations where this setting is set to ON. It’s default set up is OFF.

Insert the same rows.

On the third try to insert an row the transaction fails and the whole batch exists the execution. The third (implicit) transaction is rollbacked. Next is the message.

Msg 547, Level 16, State 0, Line 14
The INSERT statement conflicted with the CHECK constraint “Orders_Status_Code”.
The conflict occurred in database ” check_db_test”, table “dbo.Orders”, column ‘OrderStatus’.

Two rows are inserted, instead of the expecting three. That is because of the XACT_ABORT setting to ON.

Example 3 

This example shows how the database COLLATION can change the behaviour of a constraint. For that purpose I’m creating another database with Case Sensitive (CS) collation. Make sure that you’re doing this in another query window.

First, for the table creation I got an error for the “OrderId” column because it’s different from “OrderID”. Then I changed it to “OrderID”.

Msg 1911, Level 16, State 1, Line 78
Column name ‘OrderId’ does not exist in the target table or view.
Msg 1750, Level 16, State 0, Line 78
Could not create constraint or index. See previous errors.

SET XACT_ABORT OFF; –make sure the default value is set up.

Run the same insertion of data.

If the XACT_ABORT was set to ON, then none of the insert statements would pass and zero rows will be inserted.

Conclusion

From the examples we’ve seen how insertion of data could be different when some settings take place. If we start from Example 1 as a default set up for which we can assume that is most similar to real production environments and then going through Examples 2 and 3 which could be also present on some production environments, we can see the different results for the same data insertion.

The design of the check constraint is not an easy task as it seems at first glance. It becomes a more problematic task when the column for which the constraint is aimed accepts NULLs. NULLs are unknown or missing data and they are not caught up by constraints. Of course someone would propose the column be created with NOT NULL constraint, but it’s a matter of business logic decisions and/or database design principles.

Tweet about this on TwitterShare on LinkedInShare on FacebookShare on Google+

Leave a Reply

Your email address will not be published. Required fields are marked *

*