matrix_ — 2012-02-12T03:42:32-05:00 — #1
I have table structures like so..
CREATE TABLE members (
memberid int(25) NOT NULL auto_increment,
firstname varchar(55) NOT NULL default '',
lastname varchar(55) NOT NULL default '',
PRIMARY KEY (memberid)
CREATE TABLE reports (
reportid int(25) NOT NULL auto_increment,
memberid int(5) NOT NULL default '0',
PRIMARY KEY (reportid)
CREATE TABLE photos (
photoid int(25) NOT NULL auto_increment,
reportid int(5) NOT NULL default '0',
photoname varchar(55) NOT NULL default '',
PRIMARY KEY (photoid)
I want to delete all member, reports and photos from the specific member..
The member record and report records are easy but am a bit lost on best way to pick up on the photos that correspond to each report being deleted.. Should I be using a query and join to grab the corresponding photos for unlink?
r937 — 2012-02-12T04:15:34-05:00 — #2
the easiest way to handle the deletions is to let the database do it for you, using the ON DELETE CASCADE option of foreign keys
that way, you delete the member, and the member's related reports and photos are deleted automatically
if you don't use foreign keys, you would use a multi-table delete, which is simply a DELETE statement where the rows of the related tables are properly joined -- see http://dev.mysql.com/doc/refman/5.0/en/delete.html for the syntax
matrix_ — 2012-02-16T03:11:39-05:00 — #3
Not really getting my head around this.. trying to delete a member, all the members reports and unlink all the images linked to the reports..
Also how would I structure the tables to use foreign keys in the future?
r937 — 2012-02-16T04:37:08-05:00 — #4
delete a member, and all his reports, and unlink the photos? you want to leave the photos behind instead of deleting them?
that will require custom php code in your application
as for structuring the tables to use foreign keys, any competent sql tutorial can teach you that
matrix_ — 2012-02-16T15:11:22-05:00 — #5
Ok I have managed to sort it uisng 2 sql joins.. The first to collect all the photos id's and unlink the photos.. the 2nd to select all records in the 3 tables and delete them.. What a mission that was..
I will have a look for tutroials that may help.. Do you know of any easy to follow ones off hand?