Solved

MySQL "on delete cascade" doesn't appear to work.

Posted on 2008-10-01
3
1,614 Views
Last Modified: 2012-05-05
After I set up a test database:

CREATE TABLE hit (
  id int(10) unsigned NOT NULL auto_increment,
  visitorId int(10) unsigned NOT NULL,
  PRIMARY KEY  (id),
  KEY visitorId (visitorId)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

INSERT INTO hit (id, visitorId) VALUES
(1, 1),
(2, 2),
(3, 3);

CREATE TABLE visitor (
  id int(10) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (id)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

INSERT INTO visitor (id) VALUES
(1),
(2),
(3);

ALTER TABLE hit ADD FOREIGN KEY (visitorId) REFERENCES visitor(id) ON DELETE CASCADE;

I get:

mysql> select * from hit;
+----+-----------+
| id | visitorId |
+----+-----------+
|  1 |         1 |
|  2 |         2 |
|  3 |         3 |
+----+-----------+
3 rows in set (0.00 sec)

mysql> select * from visitor;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.00 sec)

When I:

mysql> delete from hit where id=2;
Query OK, 1 row affected (0.00 sec)

mysql> select * from hit;
+----+-----------+
| id | visitorId |
+----+-----------+
|  1 |         1 |
|  3 |         3 |
+----+-----------+
2 rows in set (0.00 sec)

mysql> select * from visitor;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.00 sec)

The delete doesn't cascade to the visitor table.  Why not?
0
Comment
Question by:arantius
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
philipjonathan earned 200 total points
ID: 22620908
Nope, it's the other way round.
Try deleting a row from visitor, then all rows in hit whose visitorId references the deleted visitor row will be deleted too.
0
 
LVL 18

Author Comment

by:arantius
ID: 22624176
I see.  So, if it's not obvious, I'm recording hits to web pages.  Each hit references multiple separate values like the URL, the visitor that made it, and so forth.  I periodically throw away old hit records (in this case, I'm only interested in about the past week at any time), and I was hoping to use cascade to automatically discard e.g. visitors that don't have any hit records left.

Is there a straightforward way to do this?
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 22624339
Well, I don't think we can use on delete cascade here. Probably what you can do is to periodically check the visitor table and delete records which don't have child records.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question