Check Key constraint is used to restrict the values in a column
within the specified condition.
Syntax:
CREATE TABLE <Table Name> (
<Column Name> data type CHECK(Condition)
);
Example:
CREATE TABLE Employees (
ID int NOT NULL Primary Key IDENTITY(1,1),
LastName varchar(255) NOT NULL UNIQUE,
FirstName varchar(255),
Age int CHECK(Age>17 and Age<61)
);
Now, it not possible to enter age less than 17 and greater than 60.
Using Alter command
ALTER TABLE Employees
ADD CHECK (Age>17 and Age<61);
ADD CHECK (Age>17 and Age<61);
No comments:
Post a Comment