HOW TO USE CHECK CONSTRAINT IN SQL

CHECK in SQL

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

Example of CHECK constraint in SQL server-

CREATE TABLE Students (ID INT NOT NULL, FirstName VARCHAR(200),Fees INT CHECK (Fees>0));

Naming of CHECK constraint in SQL server during creation of table-

CREATE TABLE Students (ID INT NOT NULL, FirstName VARCHAR(200), Fees INT , Age INT ,

CONSTRAINT chk CHECK (Fees>0 AND Age>21));  –constraint named as chk

CHECK constraint creation on existing table-

ALTER TABLE Students

ADD CHECK (Age>21);

Naming of CHECK constraint in SQL server on existing table-

ALTER TABLE Students

ADD CONSTRAINT chk CHECK (Fees>0 AND Age>21);

DROP a CHECK constraint-

ALTER TABLE Students

DROP CONSTRAINT chk;


Leave a Reply

Your email address will not be published. Required fields are marked *