Check for violated foreign key constraintsDeveloped In: SQL — Contributed by: Roland BoumanForeign key constraint checking in MySQL can be enabled or disabled through the dynamic system variable Foreign key constraint checking is sometimes temporarily disabled to make it easier (and faster) to load data into a couple of related tables. Disabling foreign key checking can also be used to emulate DEFERRED foreign key constraints. Re-enabling foreign key checking does not automatically validate the constraint. 'Bad' data may have entered the database during a period of time when foreign key constraint checking was disabled. This procedure checks for these violations and reports helpful information that may aid in restoring the database's referntial integrity. When a problem is found, a statement is suggested that retrieves the violating rows. The procedure accepts two parameters. The Sample usage:
use sakila;
mysql> call p_check_fk_constraint_violations('sakila','%');
+------------------------------------------------------------------------+
| message |
+------------------------------------------------------------------------+
| Checked 22 foreign keys, found 0 violated and a total of 0 violations. |
+------------------------------------------------------------------------+
1 row in set (0.51 sec)
mysql> set foreign_key_checks = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from sakila.film;
Query OK, 1000 rows affected (0.62 sec)
mysql> call p_check_fk_constraint_violations('sakila','%');
+------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------+
| message
|
+------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------+
| 5462 violations found for foreign key `fk_film_actor_film` on table `film_actor` in schema `sakila`.
Violating set can be retrieved with the following statement:
select *
from `sakila`.`film_actor` c
left join `sakila`.`film` p
on c.film_id=p.film_id
where p.film_id is null;
|
+------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------+
1 row in set (0.53 sec)
+------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------+
| message
|
+------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------+
| 1000 violations found for foreign key `fk_film_category_film` on table `film_category` in schema `sakila`.
Violating set can be retrieved with the following statement:
select *
from `sakila`.`film_category` c
left join `sakila`.`film` p
on c.film_id=p.film_id
where p.film_id is null;
|
+------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------+
1 row in set (0.53 sec)
+------------------------------------------------------------------------------------------------------------
--------------------------------------------------------+
| message
|
+------------------------------------------------------------------------------------------------------------
--------------------------------------------------------+
| 4581 violations found for foreign key `fk_inventory_film` on table `inventory` in schema `sakila`.
Violating set can be retrieved with the following statement:
select *
from `sakila`.`inventory` c
left join `sakila`.`film` p
on c.film_id=p.film_id
where p.film_id is null;
|
+------------------------------------------------------------------------------------------------------------
--------------------------------------------------------+
1 row in set (0.53 sec)
+----------------------------------------------------------------------------+
| message |
+----------------------------------------------------------------------------+
| Checked 22 foreign keys, found 3 violated and a total of 11043 violations. |
+----------------------------------------------------------------------------+
SQL
Thanks exactly what i was looking for> 6S In order to know the foreign key relationships:
|
VotesWatches0 members are watching this tool
You must be logged in to track this tool.
Provide Feedback
You must be logged in to comment
|
nice code, hopefully i found it before i write a similar tool myself. you save me a lot a time, thx