Oct 21, 2014

CHECK constraint in sql server

CHECK- The CHECK responsible to enables a condition to check the value being entered into a record

Example-
In following example we create PRODUCT table it has PRICE column and its value should be greater than 20.00,
so that you cannot have any product which price is less than or equal to 20.00

CREATE TABLE PRODUCT
(
ID INT NOT NULL IDENTITY(1,1),
SKU VARCHAR(20) NOT NULL,
TITLE VARCHAR(200) NOT NULL,
PRICE MONEY NOT NULL CHECK (PRICE>20.00),
DISCRIPTION VARCHAR(2000)  NULL,
DTCREATE DATETIME NULL
CONSTRAINT pk_ID PRIMARY KEY (ID)
)

If table already exists then use following query to create check constraints.

ALTER TABLE PRODUCT
ADD CHECK (PRICE>20.00)