Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-01
3
Medium Priority
?
1,618 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 800 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

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