Query to find foreign keys

前端 未结 3 1729
梦毁少年i
梦毁少年i 2021-01-05 00:17

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

相关标签:
3条回答
  • 2021-01-05 01:01

    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

    0 讨论(0)
  • 2021-01-05 01:06

    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....

    0 讨论(0)
  • 2021-01-05 01:08

    Why don't You use the table "INFORMATION_SCHEMA" to this?

    SELECT *
    FROM information_schema.TABLE_CONSTRAINTS
    WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
    
    0 讨论(0)
提交回复
热议问题