SQL Day 6: Check Constraint

LO 1: Check Constraint

Check Constraints is used to limit the range of values, which can be entered in the column.

Let say we have the AGE column in the tblEmployee table and in reality, a personage cannot be a negative value and may not exceed 150 years. To check whether the inserted values are within the range or not we can use Check Constraints. If the inserted value is outside the range then this constraint will throw an error.

Let’s have a look at the tblEmployee table.



In this table, Age column is in of integer type (you can check the data type by right click on table and select Design or you can select the table and then press Alt+F1)



Age column data type is displayed as int and integer column will accept any value including NULL, negative value and a positive value greater than 150.



We can add Check Constraints in two ways.

Adding Check Constraints Graphically

Using designer, expand the table tblEmployee then right click on Constraints folder and then select New Constraint.



Remember: A constraint is just as a Boolean expression which will return true or false value.

In Expression, we will give the expression that returns true or false.

  • If the value returned true then the value will be accepted and will be inserted into the table. 
  • If the value returned false then the value will not be accepted and will not be inserted into the table and error message will be displayed.


Save this table by pressing Ctrl+S. This will display an error window because when we are creating this constraint “Check Existing Data On Creation Or Re-Enabling” set to “Yes” which means this constraint will check the data in the table according to the specified expression and if found invalid data then it will throw an error.



When the invalid data is deleted from the tblEmployee then this check constraint will we saved.
Or you can simply set “Check Existing Data On Creation Or Be Re-Enabling” set to “No”, in this case, this check constraint will not check the existing data in the tblEmployee table.

After saving the table, Refresh the Constraints folder and the created constraint “CK_tblEmployee_Age” will be displayed.



Let’s try to insert the negative value of value greater than 150. This will display the error message.



Trick: Now let’s try to insert the NULL value in Age column; will this insert or an error will be displayed?

The answer is value will be inserted because when age is specified as NULL then the Boolean expression will be treated as Unknown and will return true.



Let’s try to insert the valid range value. This will be inserted successfully.


Adding Check Constraints Using Query

Simply execute the query and this will create the Check Constraint on the tblEmployee table.

Syntax:

Alter table (table_name)
Add Constraint (constraint_name) check (boolean_expression)

Example:

Alter table tblEmployee
Add Constraint CK_tblEmployee_Age check (Age>0 AND Age<150)<150 code="">


Dropping the Check Constraint

We can drop the check constraint in two ways.

Graphically: Expand the Constraint folder, select the constraint and click on Delete. This will delete the Check constraint.


Query: We need to execute the below query to drop the check constraint.

Syntax:

Alter table (table_name)
Drop Constraint (constraint_name)

Example:

Alter table tblEmployee
Drop Constraint CK_tblEmployee_Age

That’s all for today. Thank You.

Comments

Popular posts from this blog

SQL Day 11: Group By in SQL Server

JAVA Script Executor Code to Click on an Element

SQL Day 2: Creating, Altering and Dropping a Database