If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Should or should not implement foreign key

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-10, 02:49
GongXi GongXi is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
Should or should not implement foreign key

Hi,

Recently we keep get complaint that implement foreign key cause a lot of problem on implementation on customer side due to keep hit to constraint error.

May i know what is the pros and cons for implement foreign key on database?
if we want to implement foreign key, how we should handle those field that which have empty value, cause some of our field is allow to be blank.

Thanks in advance
Reply With Quote
  #2 (permalink)  
Old 09-29-10, 04:20
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Foreign key constraints are by no means mandatory and can be implemented programmatically in your application. In other words prior to inserting the contents or updating you perform all the checks needed. If you use stored procedures local on the database then performance will be optimum for performing these checks. If you don't use stored procedures then each statement executed needs to be sent to the MySQL server, parsed and executed. This can be slow, especially if you have large numbers of records to process.

It also means that you need to use either the stored procedure or your application logic to add records making sure you have integrity.

If on the other hand you use foreign keys constraints then everything remains local in the database (performance is optimal) and a simple INSERT statement needs to be executed.

But keep in mind what foreign key constraints are there for. They are there to keep the integrity of the data in the database consistent for your application to run. Imagine you had two tables, an employee table and an accounting table. The accounting table keeps track of all employee expenses. Someone starts adding entries in this table but against and employee that does not exist in the employee table. Foreign key constraints would avoid this.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On