1

I have the below tables

Tables Structure

I want to delete the data corresponding to the productId from both the tables. I have studied that JOIN can not be applied with DELETE in sqlite so i have tried the below query

DELETE FROM optionToValues WHERE optionToValues.optionId   IN
(SELECT optionToValues.optionId
FROM optionToValues 
JOIN productOptions on productOptions.optionId = optionToValues.optionId 
WHERE productOptions.product_id = 82)

But no delete operation is performed. Please suggest me something how we can achieve this. Any help is appreciated.

4
  • That query doesn't really make sense. You're looking for product_id within a set of optionIds. Maybe you just want DELETE FROM productOptions WHERE product_id = 82? If not, what exactly ARE you trying to accomplish? Commented Aug 12, 2013 at 18:02
  • Or are you trying to delete all the options for a certain product? Commented Aug 12, 2013 at 18:02
  • @323go i have to delete the productoptions and then that product also Commented Aug 12, 2013 at 18:09
  • 1
    "Delete data from 2 tables" isn't possible with a single statement. You can sort of accomplish this with foreign key actions (for example, to RESTRICT deletion where you'd get invalid relations, or CASCADE the deletion across tables): sqlite.org/foreignkeys.html#fk_actions Commented Aug 12, 2013 at 18:09

2 Answers 2

2

You'll want this --

DELETE 
  FROM optionToValues
 WHERE optionId IN ( SELECT optionId FROM productOptions WHERE product_id = 82 )

and

DELETE
  FROM productOptions
 WHERE product_id = 82
Sign up to request clarification or add additional context in comments.

Comments

1

It sounds like optionToValues.optionid should be a foreign key to productOptions.optionID using ON DELETE CASCADE. With that in place, when you delete an entry from productOptions, it's corresponding entries in optionToValues will be deleted for you.

Alternatively, you could define a trigger on productOptions which would delete the optionToValues rows.

Comments

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.