Results 1 to 14 of 14

Thread: MySQL Delete with JOINS

  1. #1

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    MySQL Delete with JOINS

    I have 4 joined tables:
    tbl_users (primary key user_id)
    tbl_user_subject_registration (foreign key user_id)
    tbl_user_results (primary key result_id, foreign key user_id)
    tbl_user_question_register (foreign key result_id)

    I would like to delete all records in all tables related to a user_id supplied. Ideally this should be within one Delete statement. I've tried various things but always get a syntax error. Can anyone point out where I'm going wrong. Latest attempt below.

    Code:
    DELETE  	tbl_users.*, tbl_user_subject_registration.*, tbl_user_results.*, tbl_user_question_register.*
    FROM		tbl_users 
    INNER JOIN	tbl_user_subject_registration 
    ON		tbl_users.user_id = tbl_user_subject_registration.user_id 
    INNER JOIN	tbl_user_results 
    ON		tbl_users.User_id = tbl_user_results.user_id 
    INNER JOIN	tbl_user_question_register 
    ON		tbl_user_results.result_id = tbl_user_question_register.result_id 
    WHERE 		tbl_users.user_id = ?
    Cheers

    DJ
    Last edited by dj4uk; Jul 14th, 2005 at 03:35 AM.

    If I have been helpful please rate my post. If I haven't tell me!

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: MySQL Delete with JOINS

    What DB are you using. Most will let you cascade deletes on a join by join basis. If so set the relevant relationship to cascade on delet and you'll only need to delete the tblUsers record, the rest will be deleted automatically.

    I know I've had problems getting deletes and updates to work on joined datasets with various databases. I'm actually not sure it's even valid sql (though I could be wrong)

  3. #3

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: MySQL Delete with JOINS

    It's a MySQL database.

    Article I was following: http://www.electrictoolbox.com/artic...-table-delete/

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: MySQL Delete with JOINS

    I've *never* had a DBMS of any kind allow a delete via joins....

    -after looking at the article-
    Wow... interesting.

    -- And what kind of syntax error do you get?

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: MySQL Delete with JOINS

    Why wouldn't you just "cascade" delete?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: MySQL Delete with JOINS

    Well I can't see any difference between what you're doing and what's in that article. As TG asked - what's the error?

    I found this link that shows you how to enforce referential indegrity in mySQL that might help. Page 2 shows how to cascade deletes:-

  7. #7

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: MySQL Delete with JOINS

    The exact error is:

    Microsoft OLE DB Provider for ODBC Drivers error '80040e14'

    [MySQL][ODBC 3.51 Driver][mysqld-3.23.55-nt]You have an error in your SQL syntax near 'tbl_users.*, tbl_user_subject_registration.*, tbl_user_results.*, tbl_user_quest' at line 1

    /admin/users.asp, line 25

    - Useful NOT!

    I've inherited the database so there are no relationships enforced and ideally I'd rather leave well alone unless absolutely necessary.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  8. #8

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: MySQL Delete with JOINS

    I'm beginning to think it's version problem - the database is MySQL 3.23.55.

    Delete with joins perhaps not supported this far back?

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  9. #9
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: MySQL Delete with JOINS

    Taken from the article you originally linked to:-
    Using a join to delete records in MySQL is only possible with version 4.0 or higher.
    I don't think your going to be able to do it on your version I'm afraid

  10. #10

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: MySQL Delete with JOINS

    Missed that cheers! I'll have a look at another method.

    Thanks everyone for suggestions.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  11. #11

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: MySQL Delete with JOINS

    Quote Originally Posted by dee-u
    Why wouldn't you just "cascade" delete?
    Is this possible with the version of MySQL I use?

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  12. #12

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: MySQL Delete with JOINS

    I've found the following: http://dev.mysql.com/doc/mysql/en/in...nstraints.html.

    It would seem that although foreign key constraits are supported, ON DELETE CASCADE is not

    Also I'd have to change all the table types to InnoDB.

    I think I'm going to have to solve this in the script rather than being able to do it all in SQL - pity I'd rather the database did it.

    Thanks to everyone.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  13. #13
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: MySQL Delete with JOINS *UNRESOLVED* Version limitations

    That page says that on delete cascade is supported from MySQL 3.23.50 onwards but only if your using innoDB. If your using 3.23.55 you should be OK to use it.

    I took a quick look at the innoDB stuff because I'd never heard of it and it looks like it's basically a layer that will add all the relational stuff for you. I know you're not keen to tinker too heavily with a db you've inherited but you might find this is a worthwhile exercise. You can alter the tables ratherthan recreating them and once you've set up the relationships properly then the db will do a lot of your work for you in future.

  14. #14

    Thread Starter
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: MySQL Delete with JOINS *UNRESOLVED* Version limitations

    Oh yes - you are right - I must have misread yesterday.

    How would I go about converting MyISAM tables to InnoDB?

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width