I was patching CiviCRM earlier on when I needed to flash the existing database with a new snapshot. The snapshot SQL performs a progressive sweep of the database, using sequential:


DROP IF EXISTS <table name>
CREATE...
INSERT...

The problem is that some tables contain explicit FOREIGN KEY references to others. When the referred to tables are dropped before the referring tables, that violates the foreign key constraint.

The solution is to ignore the constraints during the drop operation using:


SET FOREIGN_KEY_CHECKS = 0;
...drops...
SET FOREIGN_KEY_CHECKS = 1;

Found a great post on this.