Reputation:
is there a way to delete a relation only if it exists in the table? If not, Whats the best approach when deleting a relation in a table:
Thanks
Upvotes: 0
Views: 303
Reputation: 25371
DELETE FROM table WHERE id=30
will delete a row if it exists and will do nothing if it is not. No need for catching errors.
Upvotes: 4
Reputation: 357
DELETE deletes a row only if it's exist. It doesn't throw an error if there is nothing to delete.
Upvotes: 1
Reputation: 7841
I think you can do a subselect and choose which record you want to delete from that subselect, something like this
DELETE FROM target WHERE target_table.id IN (
SELECT target_table.id INNER JOIN relation_table ON relation_table.target_table_id = target_table.id
)
where the subselect, using INNER JOIN
will return records with relations =)
hope it helps! =)
Upvotes: 0