In this tutorial, I am going to explain you about Check Constraint in SQL Server. We are already familiar with Primary Key Constraint and Foreign Key Constraint in SQL Server.
After Completing this tutorial you will be able to understand:
- What is Check Constraint in SQL Server?
- How to Create Check Constraint in SQL Server?
Introduction:
A Check Constraint is used to specify a condition on a row in a table. A Check Constraint must refer to only columns of that table. It cannot refer to columns of other tables. We can define a Check Constraint either using CREATE TABLE statement or be using ALTER TABLE Statement.
The syntax for defining a Check Constraint:
Where;
tablename specifies the name of the table.
column1, column2 specifies the name of the columns of the tablename.
check_constraintname specifies the name of Check Constraint.
column_name is the column on which you want to create your Check Constraint.
condition is the condition that must be met to succeed Check Constraint.
Example:
Suppose you have a condition that your table should accept Employee Records that have age between 18 to 40. Then in such situation, we can create a Check Constraint on Age column. Here is the example for such scenario:
You can see in the following CREATE TABLE Statement we have a created a Constraint called ageConstraint on Age column that will accept the value between 18 and 40. Let’s this Check Constraint with a Record Insertion. Run the following query.
If you run above query you will get the following error.
Example 2:
Let’s create another example for Constraint. This time I want to create a Check Constraint for Gender Column. The value for Gender column should be Male or Female.
Try to insert the record with Gender value other than Male and Female.
You will get the following error after running above query.
Create Check Constraint After Table Creation:
You can also create Check Constraint using ALTER TABLE Statement.
The syntax for creating Check Constraint using ALTER TABLE Statement:
Where;
tablename specifies the name of the table.
check_constraintname specifies the name of Check Constraint.
columnname is the column on which you want to create your Check Constraint.
condition is the condition that must be met to succeed Check Constraint.
Example:
How to drop a Check Constraint:
The Syntax for dropping a Check Constraint is:
Where;
tablename specifies the name of the table.
constraintname specifies the name of Check Constraint.
Example:
How to disable Check Constraint:
Syntax for disabling Check Constraint in SQL Server.
Where;
tablename specifies the name of the table.
constraintname specifies the name of Check Constraint.
Example:
How to enable Check Constraint:
The syntax for disabling Check Constraint in SQL Server.
Where;
tablename specifies the name of the table.
constraintname specifies the name of Check Constraint.
Example:
Conclusion:
Hope you understand how to create your own rule for the column in SQL Server. I will appreciate, feedback and Comments.
View More:
Thank You.
I have taken note of your suggestion and our address now shows on our home page above the comments
Thank goodness you’re here !
online pharmacy no prescription best drug store mascara best drug store foundation
best drugstore shampoo best drugstore face moisturizer rx pharmacy
drugs from canada best drugstore blush express scripts pharmacy
pharmacy discount card rx plus pharmacy pharmacies near me
people’s pharmacy pharmacy prices indian pharmacy online
ed treatment pharmacy near me board of pharmacy
online pharmacy canada best ed medication ed treatment
It’s hard to find well-informed people in this particular topic, however, you seem like you know what you’re talking about! Thanks|
Good web site you have here.. It’s hard to find quality writing like yours these days. I truly appreciate people like you! Take care!!|
Please let me know if you’re looking for a article author for your site. You have some really good articles and I feel I would be a good asset. If you ever want to take some of the load off, I’d really like to write some material for your blog in exchange for a link back to mine. Please send me an email if interested. Cheers!|