Monday, March 14, 2011

Integrity Constraint in Sql Server

Data integrity means  maintaining  proper data .If we want to maintain proper data  we are provided INTEGRITY CONSTRAINTS.

A Constraint is a restriction or set of rules that restrict the DML operation if the data integrity is not follow.

5 integrity constraint are

1.NOT NULL
2.UNIQUE
3.PRIMARY KEY
4.CHECK 
5.FOREIGN KEY

NOT NULL :  If this constraint is imposed on a column that column will not allow null on it.

create table <tname> ( col1<Dtype>[width][Not Null],col2<Dtype>[width][Not Null],......colN<Dtype>[width][Not Null])

e.g. create table  bank(custId int Not Null,CName varchar(50),BAL Deci9mal(7,2) Not Null)

Drawback of Not Null  constraint is it allow duplicate values into the column.

UNIQUE:   if we impose unique constraint on a column the column will not allow duplicate values into it.

create table <tablename>(col1<Dtype>[width][constraint <colname>]<cons type>,..... colN<Dtype>[width][constraint <colname>]<cons type>)

e.g. create table Bank(CustID int Unique,CName varchar(50) ,Bal decimal(7,2) not null)







No comments:

Post a Comment