CHECK Constraint IN SQL
- The CHECK constraint is used to limit the value range that can be placed in a column.
- If you define a CHECK constraint on a single column it allows only certain values for this column.
- If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
How to create CHECK Constraint on TABLE in SQL:-
------------CHECK Constraint for single column------------ CREATE TABLE tblEmployees ( EmpID INT NOT NULL CHECK (EmpId>0), EmpName VARCHAR(100), Address varchar(200), MobileNo bigint, Email NVARCHAR(200) ) ---------------CHECK Constraint for multiple columns------------- CREATE TABLE tblEmployees ( EmpID INT PRIMARY KEY, EmpName VARCHAR(100), Address varchar(200), MobileNo bigint, Email NVARCHAR(200), CONSTRAINT chk_tblEmployee CHECK (EmpID>0 AND Email='sa@gmail.com') )
How to Alter CHECK Constraint on TABLE in SQL:-
------------CHECK Constraint Alter for single column------------ ALTER TABLE tblEmployees ADD CHECK (EmpID>0) ---------------CHECK Constraint Alter for multiple columns------------- ALTER TABLE tblEmployees ADD CONSTRAINT chk_tblEmployee CHECK (EmpID>0 AND Email='sa@gmail.com')
How To DROP a CHECK Constraint in SQL:-
ALTER TABLE tblEmployees DROP CONSTRAINT chk_tblEmployee