4

How do you delete a row from two separate tables? I thought it would be possible to do this using an inner join

DELETE a.*, b.* FROM Holiday INNER JOIN Accommodation b on a.LocationID = b.LocationID

Here i try to delete by matching the primary key location in the first table to the location id in the second table. I get an SQL Exception "sqlException near a"

Im doing this in SQLITE, java

enter image description here

3
  • Not sure of the exact syntax for multi-table delete but regardless you've forgotten your correlation name of a for Holiday table. FROM Holiday INNER JOIN should be FROM Holiday a INNER JOIN Commented Feb 10, 2013 at 20:37
  • There is no multi-table delete in sqlite. Commented Feb 10, 2013 at 20:55
  • oh what how do i overcome this issue Commented Feb 10, 2013 at 20:58

1 Answer 1

7

In SQLite, one DELETE command deletes from only one table.

Your query, as written, doesn't actually restrict the records to be deleted, so if you really want to delete all records, you would use this:

DELETE FROM Holiday;
DELETE FROM Accommodation;

If you want to delete one record in the master table and all corresponding records in the child table, you just filter by that key value:

DELETE FROM Holiday       WHERE LocationID = 1;
DELETE FROM Accommodation WHERE LocationID = 1;
Sign up to request clarification or add additional context in comments.

2 Comments

What if you want the user in a program to pick that key value, without you specifying it
Then you let the user pick the value, then put that value into the query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.