I’m involved is a project to migrate a project from Oracle to MySQL. I Have a script that i’m running from the MySQL shell command, called CreateTables.sql that looks like this internally:
source table\DropForeignKeys.sql
source tables\Site.sql
source tables\Language.sql
source tables\Country.sql
source tables\Locale.sql
source tables\Tag.sql
mysql --user=root --password --database=junkdb -vv < CreateTables.sql
What I’m after is a way to make the execution for the first script DropForeignKeys.sql conditional based on if the db has any tables of not. Alternatively it would be nice if there were a way to drop constraint if not exists but such a construct does not exists in MySQL to my knowledge.
So my question is how do I make the dropping of foreign key constraints conditional at script level or constraint level, so that I can have a reliable re-playable script?
Conditional logic (IF/ELSE) is only supported in functions and stored procedures – you’d have to use something that resembles:
Rather than reference INFORMATION_SCHEMA.COLUMNS, you could reference INFORMATION_SCHEMA.KEY_COLUMN_USAGE.
Depending on your needs:
…will disable and re-enable the keys attached to that table without needing to know each one. You can disable and enable keys on a database level using
SET foreign_key_checks = 0;to disable, andSET foreign_key_checks = 1;to enable them.Oracle doesn’t either, but constraints aren’t really something you want to alter blindly without knowing details.
Yes, dropping or disabling the constraints will allow you to drop the table but be aware – in order to re-enable the fk check you’ll need the data in the parent to match the existing data in the child tables.