Thursday, March 22, 2012

Duplicate Foreign keys.

Hi all,

SQL server allows to create as many as foreign key constraints on a same table for a same column.

Will this affect the design or performance in anyway ?

Naming the constraint would be a good way to avoid this.But in case if someone has already created, How do I remove the existing duplicate keys ?

======================
For Example , I have 2 tables Author and Book. I could execute the below query n times and create as many as foreign keys I want.

ALTER TABLE Books
ADD
FOREIGN KEY (AuthorID)
REFERENCES Authors (AuthorID)

======================

Thanks is advance,

DBAnalyst

this may affect the performance. foreign keys shud be named..eg

ALTER TABLE Books with nocheck

ADD constraint fkname

FOREIGN KEY (AuthorID)

REFERENCES Authors (AuthorID).

if u want to check the existing foreign keys to check ne duplicacy use the REFERENTIAL_CONSTRAINTS view of information_schema..

select * from information_schema.REFERENTIAL_CONSTRAINTS

No comments:

Post a Comment