Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a common practice for PHP/MySQL apps to have Foreign Keys declared?

Tags:

php

mysql

I am working on an open source PHP/MySQL application

I have looked at phpBB, Wordpress, and other apps to see if they specified Foreign Keys or not (to ensure referential integrity)

I cannot find that they have

Is it a common practice in these types of applications to specify Foreign Keys in the MySQL database structure?

like image 955
samJL Avatar asked Jun 29 '10 02:06

samJL


People also ask

Are foreign keys mandatory?

Note that foreign keys are not mandatory, and a table may have no foreign keys. Conversely, every column in a table may have a foreign key constraint.

When should you not use foreign keys?

There's one good reason not to use them: If you don't understand their role or how to use them. In the wrong situations, foreign key constraints can lead to waterfall replication of accidents. If somebody removes the wrong record, undoing it can become a mammoth task.

Do I need foreign key constraints?

Using a foreign key constraint helps us ensure that only data that can be linked to the referenced table can be entered into our database.

Can we create database without foreign key?

There is no reason not to use Foreign Key Constraints, and to use a relational database. You have two jobs as a SQL DBA: Make schema with DDL. Access schema with DML.


1 Answers

Past versions of MySQL use the MyISAM storage engine by default, which does not support foreign key constraints. Unless you explicitly declare tables to use the InnoDB storage engine, or change the default storage engine server-wide, no foreign keys appear, and it's no surprise that software developers who design for MySQL don't bother to use foreign key constraints.

MySQL 5.5 is currently in beta and finally InnoDB will be the default storage engine. So foreign key constraints will be supported out of the box.

Yes, foreign keys are recommended. These constraints help to ensure your data always satisfies referential integrity. The alternative is that your database gradually fills with "crumbs" or rows that refer to a parent row that is no longer in the database. These can lead to strange results from queries and wasted space, inefficient queries, and you end up doing cleanup chores manually that would be unnecessary if you just had the database enforce cleanliness for you.


Re comment form @Jacob: Good points, but be sure to read recent articles comparing InnoDB vs. MyISAM Years ago, MyISAM was considered the "fast storage engine" and InnoDB was considered the storage engine you'd reluctantly have to use if you couldn't do without transactions.

But InnoDB has improved dramatically in the past few years, and in most cases today InnoDB performs faster than MyISAM.

Aside from MyISAM still supporting fulltext indexing as you mention, there are fewer and fewer reasons to use MyISAM. When I need fulltext indexing, I either maintain a MyISAM table as a mirror of my primary storage in InnoDB, or else I use Apache Solr.

like image 188
Bill Karwin Avatar answered Sep 23 '22 02:09

Bill Karwin