3

I have the following tables, with these keys in my database:

bookings session_id

sessions session_id course_id

courses course_id

I want to create a query to delete all date relating to a single course (course_id). For example if I wanted delete course_id=10, I would want any sessions with course_id=10 to be deleted, in addition any bookings associated with any of these sessions need to be deleted too.

Is this possible? what is the best way to approach it? (I'm writing this in PHP.)

Any help much appreciated!

1
  • does the sessions table reference itself with the session_id key? Commented Feb 28, 2010 at 19:13

2 Answers 2

4

MySQL supports multi-table deletes:

DELETE FROM BOOKINGS 
 USING BOOKINGS JOIN SESSIONS JOIN COURSES
 WHERE BOOKINGS.session_id = SESSIONS.session_id
   AND SESSIONS.course_id = COURSES.course_id
   AND COURSES.course_id = ?

Another alternative would be to use stored procedure, and process the deletions in proper order:

  1. BOOKINGS

    DELETE FROM BOOKINGS
     WHERE EXISTS(SELECT NULL
                    FROM SESSIONS s 
                   WHERE s.session_id = session_id
                     AND s.course_id = ?)
    
  2. SESSIONS

    DELETE FROM SESSIONS
     WHERE EXISTS(SELECT NULL
                    FROM COURSES c
                   WHERE c.course_id = course_id
                     AND c.course_id = ?)
    
  3. COURSES

    DELETE FROM COURSES
     WHERE course_id = ?
    
Sign up to request clarification or add additional context in comments.

14 Comments

does my proposed solution have any drawbacks apart from depending on using the InnoDB engine? Also, it's _id for the keys.
@Adriano Varoli Piazza: DELETE ON CASCADE isn't a recommended practice, because someone else wouldn't necessarily know it is in place & end up wiping out data they shouldn't have. While it seems like more work, being explicit about what is being done is the best approach.
Hi There, Thanks for the response, I am attempting to use the first multi delete method: DELETE FROM bookings, sessions, courses USING bookings JOIN sessions JOIN courses WHERE bookings.session_id=sessions.session_id AND sessions.course_id=courses.course_id AND courses.course_id="13" This doesn't seem to be working - is my syntax correct? -Thanks!
If you don't use transactions, don't use a sequence of related actions.
@bart: Yes, which is why I mentioned "stored procedure".
|
3

I think the best way would be to configure the tables adding proper foreign keys (you'll have to use InnoDB for this to actually work in mysql) and setting the behavior of the FKs to 'ON DELETE CASCADE'. This way, when you delete something from the courses table, the related bookings and sessions will be deleted automatically.

Some linksies:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.