HomePHP Performing Record Updates with Foreign Key Constraints in MySQL
Performing Record Updates with Foreign Key Constraints in MySQL
In this fourth installment of a six-part series, I explain how to use foreign key constraints to update rows of two InnoDB tables via a basic abstraction class built into PHP 5. This example will help you start utilizing foreign key constraints with a particular server-side scripting language.
Being the most popular open source RDBMS available nowadays, it’s not breaking news to say that MySQL supports multiple storage engines, including the default, highly-fast MyISAM type, which you may have used hundreds of times before for developing your own database-driven web applications.
However, there are times when it’s necessary to look at other MySQL storage engines. Sometimes a particular project needs to implement additional features, like handling foreign key constraints. In a case like this, InnoDB tables will fit this requirement very efficiently, even though they may be a bit slower in terms of performance than their MyISAM counterparts.
Undeniably, one of the major advantages to using foreign key constraints with InnoDB tables is that this capability allows you to handle and maintain relationships between several tables at the database level, instead of delegating this task to certain modules or libraries of the application responsible for interacting with the tables in question.
Naturally, if you’ve been a patient follower of this series of articles and already went through all of the articles that precede this one, then you have a solid background in working with foreign key constraints within IndoDB tables. In those tutorials I explained how to trigger cascading updates and deletions on the records of a child table, when performing the same operations on rows of its related parent table.
Those basic tables initially comprised the data layer of a fictional blog application, but as you might have guessed, foreign key constraints can be used in a variety of programs with great results. In addition, it’s valid to recall that all of the examples developed so far were coded by utilizing raw SQL, without tying them to a particular programming language.
Taking into account that MySQL is very often strongly coupled with PHP, in this fourth part of the series I’m going to demonstrate how to work with foreign key constraints within a pair of InnoDB tables that will be accessed with PHP 5. Doing so should give you an accurate idea of how to use this feature in a more realistic scenario.
Now it’s time to leave the preliminaries behind and learn how to leverage the power of foreign key constraints with PHP 5. Let’s get started!