2

I have three tables:

estate_field_data
estate_field_types
estate_fields

And the only input i have from the browser is estate_field_types.ID = 3

estate_fields contains a definition for estate_field_types.ID => estate_fields.FieldType

But estate_field_data does not have a definition for the estate_field_types.ID, but instead a estate_field_data.FieldID => estate_fields.ID

How can I delete from all three rows with this single value?

Thank you! :-)

2 Answers 2

3

You have to do it as 3 separate statements (unless you've set up cascading deletes on your foreign key references). I'm assuming there's a PK in estate_fields (I've chosen to call it estate_field_id) which estate_field_data references (otherwise, I'm unsure how we identify what to delete from that table)

delete from estate_field_data where estate_field_id in (select estate_field_id from estate_fields where FieldType = 3)

delete from estate_fields where FieldType = 3

delete from estate_field_types where ID = 3
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly! It was the "IN"-clause that was missing! :-) Thank you!
-1

Try with joins as well.... Damien ... Should we suggest OP for Joins usage? May be quicker in OP case?

Delete K From estate_field_data K
Inner Join estate_fields F on F.estate_field_id = K.estate_field_id
Where F.estate_field_id = 3

6 Comments

Should the estate_field_types then be deleted in a second statement?
rest you can follow Damien's suggestion.... But I want to suggest Joins in place of In statement.. Will be quicker in deletion....
@meep - you'd still need to do 3 separate deletes - there's no way for a delete (or insert/update) to affect more than one table (directly - such a statement against a table with triggers or cascading deletes on foreign keys may end up affecting multiple tables)
I wouldn't expect any speed difference. Both queries would need to access the estate_field_id of estate_fields, with a filter criteria against FieldType, and both queries need to filter estate_field_data based on estate_field_id - the same indexes would assist both queries (or the lack of indexes would hinder both queries)
I'd stick with Damien. But nice to learn various ways of doing this.
|

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.