tags:

views:

82

answers:

4

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?

A: 

MySQL used to not honor foreign keys. It still doesn't, unless you take measures.

Out of sight, out of mind, right?

Ignacio Vazquez-Abrams
+1  A: 

I'm not sure how common it is, but I feel that you should express the conditions of the object model fully regardless of whether the underlying database fully supports them.

If you're generally writing ANSI SQL, then if you go ahead and add the foreign key constraints, then when your database supports them, you use an engine that supports them, or you move to another database that supports them, then you'll get them for "free" and not have to go back and attempt to find all the relations.

So, I would put the foreign keys in SQL anyway, but that's me and again may not be common.

RC
No, that's me too. Unless I'm writing a throwaway schema for a SO question.
Ignacio Vazquez-Abrams
thankssounds like a good practice
Sam
A: 

In MySQL only InnoDB even supports Foreign Keys, and only after MySQL 5.1.

Edit: InnoDB will be the default storage engine in MySQL 5.5

Edit-Ignore: Referential Integrety will be a "new feature" in 6.1 according to their roadmap: http://en.wikipedia.org/wiki/MySQL#Future_releases

mynameiscoffey
That wiki info is really outdated. MySQL 6 is shelved, and MySQL 5.5 will support InnoDB as the default storage engine.
Bill Karwin
@Bill Karwin, PHP 6 scrapped, MySQL 6 scrapped, there's something up with those 6's.
I have edited the wikipedia paragraph you linked to.
Bill Karwin
Thanks Bill, I have also edited my comment to reflect the new (correct) article :)
mynameiscoffey
+4  A: 

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.

Bill Karwin
thank you, that makes sense
Sam
+1 - make sure you understand the downsides of using innoDB over MyISAM though. Do a Google search for innoDB vs MyISAM, there are many articles discussing the advantages and disadvantages of both. So it really depends on what you are using the DB for whether you really want/need referential integrity over some of the advantages that MyISAM brings. One which i just discovered last week is that innoDB doesn't support full-text searches (yet), but MyISAM does :(
Jacob
My rule of thumb when it comes to InnoDB vs. MyISAM is; if you need to do full text search, use MyISAM. Full text search seems to be the only thing MyISAM has a big advantage over InnoDB in terms of speed
Thomas Winsnes