02 February, 2011

Check in MS SQL............Validation

Check constraint..................
Create Table Books
(
BookID varchar(8) primary key
check (BookID LIKE 'B[0-9][0-9][0-9][0-9][0-9]'),
Title varchar(50) NOT NULL,
Author varchar(50) NOT NULL,
Publisher varchar(50) NULL,
Category varchar(10) NOT NULL check (Category in ('Tech','Busi','Arts')),
Price int NOT NULL CHECK (Price >=0),
NoBookInHand int NOT NULL check (NoBookInHand>=0)
);

In this table you can enter data where the following must be satisfied:-
BookID must be first character B and Rest five are number.
Category must in ('Tech','Busi','Arts')
Price is not negative
NoBookInHand is not negative.

Add Check Constraint:
ALTER TABLE TableName
ADD CONSTRAINT ConstraintName CHECK (Condition......)

Drop Check Constraint:
ALTER TABLE TableName
DROP CONSTRAINT ConstraintName

No comments:

Post a Comment