I have a one to many relationship between two tables in mysql.
One called type with a primary of url
url
varchar(255)
And one called order with an index on url.
url | id_no
varchar(255) | int
The order table can have many values in it and there is already a foreign key set up so that if a url is deleted in the table type it deletes all associated keys with a matching url in order.
Orders can also be deleted manually meaning that a url could exist in type that doesn’t have a relationship to the table order.
Is there a way to set up a foreign key so that if a url in type does not have a relationship to the table order then it will delete itself from type?
Looking at the mysql manual I cannot figure it out, I am confused by CASCADE as it says it will delete all associated keys, but I only want to delete a url from the table type if and only if there is no relationship between the two?
Thanks for your help!
You can easily do that with a
DELETEtrigger on theordertable: Let it check, if there exist other rows with the same url, and if not delete it fromtype.Another way to do it is creating a maintenance script similar to
and run it on a regular or on-demand basis.