I have a database where I need to drop some foreign keys, but I don\'t know beforehand whether the foreign keys still exist.
I\'ve found some stored procedures (http
You need to connect to the Information scheme and you can find all the information about the primary key and foreign keys in this table
select
concat(table_name, '.', column_name) as 'foreign key',
concat(referenced_table_name, '.', referenced_column_name) as 'references'
from
information_schema.key_column_usage
where
referenced_table_name is not null;
HELP: see this link list-foreign-keys-in-mysql
You need to connect to the Information scheme and you can find all the information about the primary key and foreign keys in this table
SELECT * FROM information_schema.TABLE_CONSTRAINTS T;
you need to be a ROOT
user to access the information_schema
.
USING this table you can find the table, db and whether it has foreign key.
Hope this helps if you dont wanna use IF EXIST
and Stored Procedure. But I am Sure you can use IF EXIST
can be used for non stored procedure queries....
Why don't You use the table "INFORMATION_SCHEMA" to this?
SELECT *
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'