Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Foreign Key Doubt

Author  Topic 

rohan_chikhale
Starting Member

7 Posts

Posted - 2009-05-05 : 13:52:21
Can i use non-primary key as foreign key...will it affect the performance and can cause headache of maintaing the relationships....?
plz clear my doubt....thank you...

Rohan Chikhale

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-05 : 14:23:49
A foreign key does not have to reference a primary key constraint in the parent table. It can reference a unique constraint too.

It will not cause any extra negative performance. There isn't any headache in maintaining relationships, so I'm not sure what you are referring to with that question.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-05-05 : 15:20:39
There is over head associated with foreign keys, yes. But, I'd much rather have correct data (hardware is cheap) :)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-05 : 15:22:27
But it won't cause any extra overhead than if you used a primary key in the foreign key relationship.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-05-05 : 15:26:54
I would agree the overhead is minimal and I suggest one always uses RI to it's fullest.

But, you can notice a difference in speed when, for example, inserting million(s) of rows into a table with a foreign key reference to another table that also has millions of rows.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-05 : 17:13:17
I'm not disagreeing with that. I'm only commenting on the non-PK performance FK question. There is no performance difference in a foreign key relationship if it's using a PK or a non-PK.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

rohan_chikhale
Starting Member

7 Posts

Posted - 2009-05-06 : 01:22:21
k freinds.....thank you for clearing my doubt.....i was confused whether to go for the primary key or non-primary key of the parent table ......

Rohan Chikhale
Go to Top of Page
   

- Advertisement -