Skip to content

Instantly share code, notes, and snippets.

@akovalyov
Last active December 29, 2015 13:29
Show Gist options
  • Save akovalyov/7677179 to your computer and use it in GitHub Desktop.
Save akovalyov/7677179 to your computer and use it in GitHub Desktop.
get all constraints from mysql
-- helps to debug strange errors like General error: 1005 Can't create table '$some_db.#sql-186d_71' (errno: 121)
-- usually it happens whem you manually rename a table and schema is generated via a tool
-- need to execute also
-- ALTER TABLE `$table`
-- DROP FOREIGN KEY `old_key`,
-- ADD CONSTRAINT `new_key` FOREIGN KEY (field) REFERENCES $table2(id);
SELECT
constraint_name,
table_name
FROM
information_schema.table_constraints
WHERE
constraint_type = 'FOREIGN KEY'
AND table_schema = DATABASE()
ORDER BY
constraint_name;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment