Skip to content

Instantly share code, notes, and snippets.

@jfriv
Last active March 4, 2018 01:56
Show Gist options
  • Save jfriv/7ea2f83831a8f2c8739c91c14e0c7f71 to your computer and use it in GitHub Desktop.
Save jfriv/7ea2f83831a8f2c8739c91c14e0c7f71 to your computer and use it in GitHub Desktop.
MySQL build query to drop all foreign keys for a table
---------------------------------------------------------------------
---- build sql to drop all foreign keys for a table
---------------------------------------------------------------------
SELECT CONCAT('ALTER TABLE `',table_schema,'`.`',table_name,'` DROP FOREIGN KEY ',constraint_name,';')
FROM information_schema.table_constraints
WHERE constraint_type='FOREIGN KEY'
AND table_schema='{database}'
AND table_name IN ('{table}');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment