Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2004
    Posts
    57

    Unanswered: 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

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    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
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •