CHECK constraint is used to define a rule or requirement that must be met by each record when we insert or update in a table. It can apply to single or multiple columns of the table. The result of the CHECK constraint rule can be either TRUE, FALSE, or NULL, depending on the column value. If the result evaluates to NULL, then the CHECK constraint will not throw the error and the record can be inserted or updated.

1. Add CHECK constraint on a column

CREATE TABLE TblStudent
(
    -- To validate the SRNo value between 0 to 1000
    SRNo int NOT NULL PRIMARY KEY DEFAULT 1000
    CHECK (SRNo BETWEEN 0 AND 1000),
	
    -- To Validate StudentName should not accept numeric value
    StudentName VARCHAR(50) CONSTRAINT no_names
    CHECK (StudentName NOT LIKE '%[0-9]%'), 

    -- To Validate RowCreatedDate should greater than or equel to current date
    RowCreatedDate DATETIME 
    CHECK (CAST(RowCreatedDate AS DATE)>=CAST(GETDATE() AS DATE))
)     
2. Add CHECK constraint on a table

CREATE TABLE TblStudent
(
    SRNo VARCHAR(15),
    StudentName VARCHAR(50),
    FatherName VARCHAR(50),
    CourseCode VARCHAR(15),
    CityCode VARCHAR(10)
    CONSTRAINT CK_TblStudent_CityCode  CHECK (CityCode IN ('AL','BC','GL','CD')),
    CONSTRAINT CK_TblStudent_SRNo CHECK (SRNo IS NOT NULL)
)     
3. Add CHECK Constraint to an existing table

ALTER TABLE TblStudent
ADD CONSTRAINT CK_TblStudent_CourseCode CHECK (CourseCode NOT LIKE '%test%')     
4. Disable a specific CHECK constraint

ALTER TABLE TblStudent 
NOCHECK CONSTRAINT CK_TblStudent_CourseCode     
5. Disable all CHECK constraints of a table.

ALTER TABLE TblStudent 
NOCHECK CONSTRAINT ALL     
6. Enable a specific CHECK constraint

ALTER TABLE TblStudent 
CHECK CONSTRAINT CK_TblStudent_CourseCode     
7. Enable all CHECK constraints of a table.

ALTER TABLE TblStudent 
CHECK CONSTRAINT ALL     
8. Delete a CHECK constraint

ALTER TABLE TblStudent 
DROP CONSTRAINT CK_TblStudent_CourseCode