SQL Constraints - Check Key Constraint - DotNet and DataBase

Wednesday, 1 August 2018

SQL Constraints - Check Key Constraint





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);

No comments:

Post a Comment

x

Get Updates On

Discussion updates

Straight Into Your INBOX!

Enter your email address to subscribe to this website and receive notifications of new posts by email.