Magento has a built in adapter to MySQL which allows you to add and drop constraints and keys.
You will be leveraging
addConstraint(). A good example is
In the code examples assume we are modifying a module called Items. Let’s assume the Items module allows the user to add/edit Items each of which is associated to one or more Products.
You will be making changes in your module’s
mysql4-upgrade-X.X.X-X.X.X.php file (e.g.
In addition you should set the new version number in the module’s
<config> <modules> <[Namespace]_Items> <version>0.0.2</version> ...
See more in the Custom Module with Custom Database Table Wiki article.
Add an index to
addKey($tableName, $indexName, $fields, $indexType = 'index')
$fields can be an array for a multiple-field index and
$indexType can be one of
Add a foreign key constraint to
items_item.item_id. Both ON DELETE and ON UPDATE are CASCADE.
Depending on your need, you would probably add another foreign key referencing
catalog/products so than, when a Product is deleted, your Item’s relation to the Product would be deleted as well.