Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I cascade delete across tables?

Status
Not open for further replies.

adam0101

Programmer
Jun 25, 2002
1,952
US
Can someone tell me how to cascade delete? I have two tables (tblCategories and tblServices) related on the CategoryID field. How do I create the tables so when a record is deleted from the tblCategories table, all related records from the tblServices table will be deleted?
Code:
                      +-----------+
+-------------+       |tblServices|
|tblCategories|       +-----------+
+-------------|       |ServiceID  |
|CategoryID   |<<--->>|CategoryID |
|Category     |       |Service    |
+-------------+       +-----------+
 
An update to my previous post: I tried using the following sql code to create a cascading relationship. It creates the tables with no errors, but won't cascade! What am I doing wrong?

CREATE TABLE tblCategories (CategoryID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (CategoryID), Title varchar(50), Help varchar(255)) TYPE=INNODB;

CREATE TABLE tblServices (ServiceID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (ServiceID), CategoryID INT NOT NULL, FOREIGN KEY (CategoryID) REFERENCES tblCategories(CategoryID) ON DELETE CASCADE ON UPDATE CASCADE, Title varchar(50), Number varchar(50)) TYPE=INNODB;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top