SQL check constraint

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.


SYNTAX :
CREATE TABLE Emp
(
E_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CHECK (P_Id>0)
)



SQL Server / Oracle / MS Access:

CREATE TABLE Emp
(
E_Id int NOT NULL CHECK (P_Id>0),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)



MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Emp
(
E_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT chk_Employee CHECK (E_Id>0 AND City='Sandnes')
);



Online Execute/Compile

Quantitative Aptitude
Reasoning
Programming
Interview