Friday 16 December 2016

Create an unique Cluster INDEX over multiple columns

Unique Cluster Index on multiple columns is very helpful when we are trying to insert same values on that columns.
Suppose we have four columns and every time we want to insert some unique combination values on the tables .
If the user inserting same  data then it should through an error.
Below check the Example :

create table Test
(
i int not null, j int not null, s
varchar(10) not null, t varchar(10) not null
)
go


alter table Test add constraint pk primary key (i, j,s,t)

select * from Test


Now trying to insert some values :

insert into Test values(1,1,'c','d')--row inserted
insert into Test values(1,1,'c','d') --error (same data)
insert into Test values(1,1,'c','c') --row inserted
insert into Test values(1,1,'d','c') --row inserted
insert into Test values(1,1,'d','c') --error (same data)
insert into Test values(1,2,'d','c') --row inserted

No comments:

Post a Comment