Quantcast
Viewing latest article 1
Browse Latest Browse All 4

Answer by Kev Riley

You can have a primary key and a composite index - you can only have one of them as clustered. Also a foreign key does not need to reference a primary key, it can reference a unique constraint (unique index). So you can have a composite foreign key ...eg: if object_id('kev3','U') is not null drop table kev3; if object_id('kev2','U') is not null drop table kev2; if object_id('kev','U') is not null drop table kev; create table kev ( id int not null, partid1 int not null, partid2 int not null, ) alter table kev add constraint PK_kev primary key (id) create unique index IX_kev_composite on kev(partid1, partid2) create table kev2 ( id int not null foreign key references kev(id), blah varchar(100) ) create table kev3 ( id int not null, part1 int not null, part2 int not null, blah varchar(100) ) alter table kev3 add constraint FK_kev3 foreign key (part1, part2) references kev (partid1, partid2) In this example table kev has both a primary key and a unique composite constraint. kev2 has a foreign key reference to the PK, and kev3 has a composite foreign key reference to the unique constraint

Viewing latest article 1
Browse Latest Browse All 4

Trending Articles