![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have two tables that I join together for SELECT queries. One lists 3 columns (id, type, and expiration) where 'id' and 'type' are UNIQUE, then the other lists about 30 columns of data, where 'id' is a PRIMARY. The 'id' for the two tables will always match up. My question is, what is the proper syntax to DELETE from both tables at once, based on the 'id' column? Currently, I run 2 queries, but it would be better if I could do it in one: DELETE FROM table1 WHERE id=12345; DELETE FROM table2 WHERE id=12345; |
#3
| |||
| |||
|
|
I have two tables that I join together for SELECT queries. One lists 3 columns (id, type, and expiration) where 'id' and 'type' are UNIQUE, then the other lists about 30 columns of data, where 'id' is a PRIMARY. The 'id' for the two tables will always match up. My question is, what is the proper syntax to DELETE from both tables at once, based on the 'id' column? Currently, I run 2 queries, but it would be better if I could do it in one: DELETE FROM table1 WHERE id=12345; DELETE FROM table2 WHERE id=12345; |
#4
| ||||||||
| ||||||||
|
|
"Jason C" <jwcarl... (AT) gmail (DOT) com> wrote in message news:22879852.161.1328142923702.JavaMail.geo-discussion-forums (AT) vbxy22 (DOT) .. I have two tables that I join together for SELECT queries. One lists 3 columns (id, type, and expiration) where 'id' and 'type' are UNIQUE, then the other lists about 30 columns of data, where 'id' is a PRIMARY. The 'id' for the two tables will always match up. My question is, what is the proper syntax to DELETE from both tables at once, based on the 'id' column? Currently, I run 2 queries, but it would be better if I could do it in one: DELETE FROM table1 WHERE id=12345; DELETE FROM table2 WHERE id=12345; Whilst I can see the elegance of doing it all in one query (see Denis' post), it wouldn't surprise me if running your current two separate deletes is slightly faster - if your tables are very large then it might be worth benchmarking it, but with proper indexes then for most applications I'm sure both would be fast enough. What I would say is that if you delete via two separate deletes then its worth wrapping it up inside a transaction, to avoid the risk of ending up with the record only having been deleted from one table and not the other. -- *Brian Cryer *http://www.cryer.co.uk/brian |
|
id | +----+ 1 | 2 | 3 | +----+ |
|
id | +----+ 1 | 2 | 3 | +----+ |
|
id | parent_id | +------+-----------+ 1 | 1 | 2 | 2 | 3 | 3 | +------+-----------+ |
|
id | id | parent_id | +----+------+-----------+ 1 | 1 | 1 | 2 | 2 | 2 | 3 | 3 | 3 | +----+------+-----------+ |
|
id | id | parent_id | +----+------+-----------+ 2 | 2 | 2 | 3 | 3 | 3 | +----+------+-----------+ |
|
id | +----+ 2 | 3 | +----+ |
|
id | parent_id | +------+-----------+ 2 | 2 | 3 | 3 | +------+--------+ |
![]() |
| Thread Tools | |
| Display Modes | |
| |