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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Feb 13, 2012, 11:20 AM
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
SenthilkumarPosted Feb 28, 2012, 9:30 AM
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.
Vineet Kumar SainiPosted Feb 14, 2012, 5:20 PM
Suthish NairPosted Feb 13, 2012, 11:16 AM
http://msdn.microsoft.com/en-us/library/ms190765.aspx