Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
cascading delete PHP/mySQL
Old 07-21-2011, 03:29 PM cascading delete PHP/mySQL
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
Hi

I want to run a cascading delete that deletes say a restaurant ( id=2) from my restaurant table but cascade delete all the dishes that are related to that restaurant.

Could some one please showe me how i might do this.

The key for restaurant table id id
The key for dish table is restaurant_id

Any help would really be appreciated. Bit stuck on this.

Cheers
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
 
Register now for full access!
Old 07-21-2011, 03:44 PM Re: cascading delete PHP/mySQL
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Are you using foreign keys? If you're using innodb you can create a foreign key and set it to cascade, for example you could add the following to your dish table definition:
Code:
FOREIGN KEY (restaraunt_id) REFERENCES restaurant(id) ON DELETE CASCADE
Now if a restaurant is deleted, any corresponding dishes will be deleted as well.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 07-21-2011 at 04:00 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-21-2011, 03:48 PM Re: cascading delete PHP/mySQL
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
No, No foriegn Keys
Just two tables linked by restaurant.id and dish.restaurant_id
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 07-21-2011, 03:50 PM Re: cascading delete PHP/mySQL
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
Your

"FOREIGN KEY (restaraunt_id) REFERENCES restaurant(id) ON DELETE CASCADE"

comment would actually be defined in the DB on the create YES?

This hasnt been done by person who created DB so again no... Just two tables.

Looking to create best SQL i can from what have
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 07-21-2011, 03:52 PM Re: cascading delete PHP/mySQL
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
To Add

I was looking at how i could do with a trigger?
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 07-21-2011, 03:57 PM Re: cascading delete PHP/mySQL
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I think you can set a foreign key in the dish table that refers to the restaurant table, with a cascading delete attached to it. However, I also think you have to use the InnoDB storage engine (dont think MyISAM supports foreign keys).

If your concern in the first place is that you don't wanna run two seperate queries in case the first one succeed and the second one fails, another alternative is to use transactions (also supported by InnoDB but not MyISAM). Transactions ensure that either all queries are successfully run, or none is. If one fails, everything is "rolled back" to their state before the transaction.

EDIT: Oh, I was too slow. I opened this tab 20 minutes ago and didn't post until now, seems I wasn't quick enough :P
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.

Last edited by lizciz; 07-21-2011 at 03:59 PM..
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 07-21-2011, 04:00 PM Re: cascading delete PHP/mySQL
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
OK I understand
thanks for you input.
In truth, i think i am going to have to go the the person and see why he has built the Db the way he has and see if he can update things.

Cheers again.

Steve
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 07-21-2011, 04:03 PM Re: cascading delete PHP/mySQL
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
By the way, what is your original problem? Can't you just run two queries?
delete from dish where restaurant_id=2
delete from restaurant where id=2
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 07-21-2011, 04:13 PM Re: cascading delete PHP/mySQL
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
Yes thats how i am doing at moment.
2 completely separate queries.

It was just suggested to me i should be able to cascade the delete. ( by employer )

On looking into this, i realized that it build with myisam engine not InnoDB so this just got me thinking if i could do in a more efficient way.

I found some info on triggers but didnt quite understand it. It was on the back en of that i questioned how i could delete more efficiently.

CREATE TRIGGER restaurant_cascade_delete

I suppose my question still stands how i can use a trigger to delete
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 07-21-2011, 04:16 PM Re: cascading delete PHP/mySQL
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by lizciz View Post
dont think MyISAM supports foreign keys
This is true. You must be using the innodb storage engine to use foreign keys.

If you're already using innodb or there isn't a strong reason why you can't switch (relying on myisam full text search would be one) my advice would be to redesign the table to use foreign keys.

I'm moving this thread to the DB forum since the problem isn't strictly related to PHP.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-21-2011, 04:20 PM Re: cascading delete PHP/mySQL
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
This might be of some help if you want to create a trigger:
http://stackoverflow.com/questions/5...storage-engine
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-21-2011, 04:23 PM Re: cascading delete PHP/mySQL
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
Ah yes thats quite helpful.
Thanks
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Reply     « Reply to cascading delete PHP/mySQL
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.44356 seconds with 12 queries