Skip to main content

CHECK constraint in sql server

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

Example-
In the following example, we create a PRODUCT table it has a 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),
DESCRIPTION VARCHAR(2000)  NULL,
DTCREATE DATETIME NULL
CONSTRAINT pk_ID PRIMARY KEY (ID)
)

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

ALTER TABLE PRODUCT
ADD CHECK (PRICE>20.00)