Solved

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

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 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