Home arrow PHP arrow Deleting Database Records with an Abstraction and Foreign Key Constraints

Deleting Database Records with an Abstraction and Foreign Key Constraints

In this fifth article of a six-part series, you will learn how to use foreign key constraints in MySQL to produce the deletion in cascade of rows in a child table when the related rows of the corresponding parent are deleted as well. We'll use a simple MySQL abstraction class to do the trick.

TABLE OF CONTENTS:
  1. Deleting Database Records with an Abstraction and Foreign Key Constraints
  2. Review: updating database rows in cascade through a MySQL abstraction class
  3. Enabling support for cascading row deletions
  4. The MySQL abstraction class in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
December 21, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Maintaining the integrity of a database in MySQL or other RDBMS isnít always a simple task; this is notoriously evident where the responsibility to carry out this process is delegated to the application that interacts with the database. In those situations, establishing and maintaining relationships between the tables of a database is  often performed by a combination of abstraction and ORM libraries (short for Object Relational Mapping), but the implementation of the logic required to put these two pieces to work together used to be complex.

Fortunately, most of the Relational Database Management Systems being used nowadays, including MySQL naturally, provide support for defining foreign key constraints in a straightforward fashion. This handy feature permits developers to handle and maintain relationships between tables at the database level, thus allowing them to write smaller and simpler programs using any server-side programming language.

In the previous installments of this series of articles, I demonstrated this concept specifically when working with PHP 5. In the course of those tutorials I defined two basic InnoDB tables in MySQL, which comprised the data layer of a sample blogger program that stored some simple blog posts along with the comments that were made on them.

The rudimentary nature of the program served to illustrate that, thanks to the functionality offered by foreign key constraints, updating a specified blog entry and its related comments was as easy as performing a simple UPDATE statement via an abstraction PHP 5 class, and nothing else.

The functionality provided by foreign key constraints doesnít stop here. Itís feasible to take advantage of them for deleting database rows, in a way pretty similar to performing updates. However, in this case Iím going to demonstrate how to achieve this by using the MySQL abstraction class that was coded in the previous tutorial.

Are you ready to learn how to leverage the power of foreign key constraints with MySQL and PHP 5  working together? Then start reading now!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: