Is there a way, when you have a list of records, to check if each of these records have foreign key references before you attempt to delete any of these records?
As an example, if I have a list of borrowers and a list of books, you should not be able to delete a borrower from the system if he still has books on loan. (My actual system is much more complicated than that however - a lot more tables.)
I would like to remove the delete option from any borrowers that have books on loan (in that example).
If I try to delete a record with foreign key references, I get an error to the effect of:
Database access failed: Cannot delete or update a parent row: a foreign key constraint fails (
dbname
.tablename
, CONSTRAINTfkfieldid
FOREIGN KEY (fieldid
) REFERENCEStablename
(fieldid
))
One solution is to write a query to check if each record, in a list of records, has any foreign key references in any of the possible tables it could be referenced.
However, if I wish to display a list of 100 records from a table in my content management system, and I have to run 100 sub-queries in order to display that list, it is obviously very inefficient!
End users become confused when they try to delete a record but they can't because that data is 'in use' elsewhere, so I would rather remove the option to delete to avoid confusion.
Any ideas on what would be the best thing to do? Thanks.