Solved

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

Posted on 2008-10-01
3
1,604 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now