Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a form from where Information is entered into multiple database having same id, if by mistake wrong data is entered, I want to delete that from all tables.

How and what should I do to delete from all information related to that id from all tables.

share|improve this question
8  
And this, folks, is why we use foreign keys, referential integrity, and ON DELETE CASCADE. –  Ignacio Vazquez-Abrams Jul 3 '13 at 9:59
    
If you are using MySQL, use InnoDB Tables and referential constraints. It will automatically handle data consistency. –  Irfan DANISH Jul 3 '13 at 10:24
    
I haven't specified foreign key in database table whereas my id is being inserted by the I have written. cascade delete works on foreign key provided in database. –  Php developer Jul 4 '13 at 5:26
    
the person handling database has not used InnoDB tables and I can't change it as it is already on server. I have to do something with the codes. –  Php developer Jul 4 '13 at 5:32

1 Answer 1

You can do multiple table deletes:-

http://dev.mysql.com/doc/refman/5.0/en/delete.html

For example say you had 4 tables and want to delete all the records in 3 of them that relate to the 1st table:-

DELETE Table1, Table2, Table3
FROM Table0
INNER JOIN Table1
ON Table0.Id = Table1.ParentId
INNER JOIN Table2
ON Table0.Id = Table2.ParentId
INNER JOIN Table3
ON Table0.Id = Table3.ParentId
WHERE Table0.Id = 1

Of course you could also delete from the first table (Table0) as well.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.