Top Menu

Search This Blog

SQL Constraints(CHECK)



The SQL CHECK constraint is used to limit value range that can be placed in a column.

The CHECK Constraint enables a condition to check the value being entered into a record.


CREATE TABLE CANDIDATE
 (
   CandidateId int IDENTITY(1,1) PRIMARY KEY,
   CandidateNumber int NOT NULL UNIQUE  CHECK(CandidateNumber>2),
   LastName varchar(50) NOT NULL,
   FirstName varchar(50) NOT NULL,
   PostCode int NULL,
   Address varchar(50) NULL,
   Phone varchar(50) NULL,
)

In this case, when we try to insert a Customer Number less than two we will get an error message.

Set SQL CHECK Constraints in the MS-SQL Server Designer Tools:
If you want to use MS-SQL Server Designer, right-click on the column where you want to set the constraint and select "Check Constraints..."























Then Click on Check Constraints..




















Then Click "Add" and  then click "..." in this case to open the Expression Window
















In the Expression window  you can type in the Expression you want use.








Post a Comment

Designed by OddThemes | Distributed By Gooyaabi Templates