Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seek foreign key violations on whole database? (I'm currently using MySQL)

In databases where foreign key checking has been disabled in the past, how can one check for foreign key constraint violations?

like image 658
Gedean Dias Avatar asked Oct 05 '09 14:10

Gedean Dias


2 Answers

there is no built-in way to do this. the only thing i can think of would be to look at the TABLE_CONSTRAINTS and KEY_COLUMN_USAGE tables in the INFORMATION_SCHEMA database to manually check for rows that don't match.

like image 22
longneck Avatar answered Oct 14 '22 05:10

longneck


Basically, if you have no foreign key constraints, you can do this:

SELECT * FROM CHILD C WHERE C.PARENT_ID NOT IN (SELECT ID FROM PARENT);
like image 171
Mike Demenok Avatar answered Oct 14 '22 03:10

Mike Demenok