How do I see all foreign keys to a table or column?

后端 未结 13 826
忘了有多久
忘了有多久 2020-11-22 09:14

In MySQL, how do I get a list of all foreign key constraints pointing to a particular table? a particular column? This is the same thing as this Oracle question, but for MyS

相关标签:
13条回答
  • 2020-11-22 09:38

    As an alternative to Node’s answer, if you use InnoDB and defined FK’s you could query the information_schema database e.g.:

    SELECT CONSTRAINT_NAME, TABLE_NAME, REFERENCED_TABLE_NAME
    FROM information_schema.REFERENTIAL_CONSTRAINTS
    WHERE CONSTRAINT_SCHEMA = '<schema>'
    AND TABLE_NAME = '<table>'
    

    for foreign keys from <table>, or

    SELECT CONSTRAINT_NAME, TABLE_NAME, REFERENCED_TABLE_NAME
    FROM information_schema.REFERENTIAL_CONSTRAINTS
    WHERE CONSTRAINT_SCHEMA = '<schema>'
    AND REFERENCED_TABLE_NAME = '<table>'
    

    for foreign keys to <table>

    You can also get the UPDATE_RULE and DELETE_RULE if you want them.

    0 讨论(0)
  • 2020-11-22 09:39

    Posting on an old answer to add some useful information.

    I had a similar problem, but I also wanted to see the CONSTRAINT_TYPE along with the REFERENCED table and column names. So,

    1. To see all FKs in your table:

      USE '<yourschema>';
      
      SELECT i.TABLE_NAME, i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME 
      FROM information_schema.TABLE_CONSTRAINTS i 
      LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME 
      WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' 
      AND i.TABLE_SCHEMA = DATABASE()
      AND i.TABLE_NAME = '<yourtable>';
      
    2. To see all the tables and FKs in your schema:

      USE '<yourschema>';
      
      SELECT i.TABLE_NAME, i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME 
      FROM information_schema.TABLE_CONSTRAINTS i 
      LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME 
      WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' 
      AND i.TABLE_SCHEMA = DATABASE();
      
    3. To see all the FKs in your database:

      SELECT i.TABLE_SCHEMA, i.TABLE_NAME, i.CONSTRAINT_TYPE, i.CONSTRAINT_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME 
      FROM information_schema.TABLE_CONSTRAINTS i 
      LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME 
      WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY';
      

    Remember!

    This is using the InnoDB storage engine. If you can't seem to get any foreign keys to show up after adding them it's probably because your tables are using MyISAM.

    To check:

    SELECT * TABLE_NAME, ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA = '<yourschema>';
    

    To fix, use this:

    ALTER TABLE `<yourtable>` ENGINE=InnoDB;
    
    0 讨论(0)
  • 2020-11-22 09:40

    This solution will not only display all relations but also the constraint name, which is required in some cases (e.g. drop contraint):

    select
        concat(table_name, '.', column_name) as 'foreign key',
        concat(referenced_table_name, '.', referenced_column_name) as 'references',
        constraint_name as 'constraint name'
    from
        information_schema.key_column_usage
    where
        referenced_table_name is not null;
    

    If you want to check tables in a specific database, at the end of the query add the schema name:

    select
        concat(table_name, '.', column_name) as 'foreign key',
        concat(referenced_table_name, '.', referenced_column_name) as 'references',
        constraint_name as 'constraint name'
    from
        information_schema.key_column_usage
    where
        referenced_table_name is not null
        and table_schema = 'database_name';
    

    Likewise, for a specific column name, add

    and table_name = 'table_name

    at the end of the query.

    Inspired by this post here

    0 讨论(0)
  • 2020-11-22 09:41

    Constraints in SQL are the rules defined for the data in a table. Constraints also limit the types of data that go into the table. If new data does not abide by these rules the action is aborted.

    select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE = 'FOREIGN KEY';
    

    You can view all constraints by using select * from information_schema.table_constraints;

    (This will produce a lot of table data).

    You can also use this for MySQL:

    show create table tableName;
    
    0 讨论(0)
  • 2020-11-22 09:42

    If you use InnoDB and defined FK's you could query the information_schema database e.g.:

    SELECT * FROM information_schema.TABLE_CONSTRAINTS 
    WHERE information_schema.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'FOREIGN KEY' 
    AND information_schema.TABLE_CONSTRAINTS.TABLE_SCHEMA = 'myschema'
    AND information_schema.TABLE_CONSTRAINTS.TABLE_NAME = 'mytable';
    
    0 讨论(0)
  • 2020-11-22 09:43

    I'm reluctant to add yet another answer, but I've had to beg, borrow and steal from the others to get what I want, which is a complete list of all the FK relationships on tables in a given schema, INCLUDING FKs to tables in other schemas. The two crucial recordsets are information_schema.KEY_COLUMN_USAGE and information_schema.referential_constraints. If an attribute you want is missing, just uncomment the KCU., RC. to see what's available

    SELECT DISTINCT KCU.TABLE_NAME, KCU.COLUMN_NAME, REFERENCED_TABLE_SCHEMA, KCU.REFERENCED_TABLE_NAME, KCU.REFERENCED_COLUMN_NAME, UPDATE_RULE, DELETE_RULE #, KCU.*, RC.*
    FROM information_schema.KEY_COLUMN_USAGE KCU
    INNER JOIN information_schema.referential_constraints RC ON KCU.CONSTRAINT_NAME = RC.CONSTRAINT_NAME
    WHERE TABLE_SCHEMA = (your schema name)
    AND KCU.REFERENCED_TABLE_NAME IS NOT NULL
    ORDER BY KCU.TABLE_NAME, KCU.COLUMN_NAME;
    
    0 讨论(0)
提交回复
热议问题