Using PHP, when I'm about to delete a row from a table I want to able to determine if it's PK value is used by any other table
I have a table where it's PK may be FK in one or several tables. At first hand I don't know how many tables are related.
What could be the most efficient and shortest way to achieve what I'm looking for?
My first approach is to get which tables are related to, doing something like:
select table_name, column_name from information_schema.key_column_usage where
table_schema = 'my_schema' and referenced_table_name = 'my_table' and
referenced_column_name = 'id_field'
Then loop through that list and for each table do something like:
select count(*) as total from table_name_from_loop where id_field = my_value
So if I any of those count()
is greater than 0, then that row is in use.
Any suggestion on how to perform this task? I prefer some kind of check instead of trying to delete and catch some error
EDIT 201309091540: Anyone?