Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use foreign keys? [duplicate]

Possible Duplicate:
What’s wrong with foreign keys?

I use MS Sql Server with a large database about 4 GB data.

I search around the web why I should use foreign keys. by now I only indexed the keys used to join tables. Performance is all fine, dataintegrety is no problem.

Should I use foreign keys? Will I get even more performance with foreign keys?


2 Answers

Foreign key's don't actually improve performance, in fact they incur a small performance penalty on all write operations, to ensure that the constraint is followed.

The reason why you want to use these is to prevent destructive write operations. If you don't have them, buggy code, or a bad sql statement can remove rows that are expected to be there.

like image 188
Dana the Sane Avatar answered Sep 14 '25 11:09

Dana the Sane


Integrity may not be a problem today, but that's the exact attitude that makes it a problem tomorrow or two weeks from now.

like image 25
Tom H Avatar answered Sep 14 '25 10:09

Tom H