Hi....
I want to know what is the use of check constraint in SQL Server ? Please tell me , how to use check constraint in SQL Server ?
Thanks........
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 13, 2012, 6:20 AM EST
CHECK Constraint
The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a single column it allows only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
SQL CHECK Constraint on CREATE TABLE
The following SQL creates a CHECK constraint on the "P_Id" column when the "Persons" table is created. The CHECK constraint specifies that the column "P_Id" must only include integers greater than 0.
CREATE TABLE Persons
(
P_Id int NOT NULL CHECK (P_Id>0),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
Refer
http://www.w3schools.com/sql/sql_check.asp
Thanks
Suthish NairRank #92 · 22.8K reputationPosted Feb 13, 2012, 6:16 AM EST
http://msdn.microsoft.com/en-us/library/ms190765.aspx
Vineet Kumar SainiRank #257 · 9.8K reputationPosted Feb 14, 2012, 12:20 PM EST
SenthilkumarRank #387 · 6.5K reputationPosted Feb 28, 2012, 4:30 AM EST
Constraints are used to validate the data while doing DML operations in the sql server.
Lets take an example of Age column or marks column.
The user should be restricted to enter the value which is actually invalid.
For example when you define the column as int then it has the range. when the user insert the numeric value then it is not an issue. Data type is fine but the problem with the data range.
when you set the age constraint, age should not exceed more than 120. Lets take mark as maximum 100 then we should not allow to enter 235. then constraints come into the picture.