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

544,979

Solution 1

For a Table:

SELECT 
  TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM
  INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
  REFERENCED_TABLE_SCHEMA = '<database>' AND
  REFERENCED_TABLE_NAME = '<table>';

For a Column:

SELECT 
  TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM
  INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
  REFERENCED_TABLE_SCHEMA = '<database>' AND
  REFERENCED_TABLE_NAME = '<table>' AND
  REFERENCED_COLUMN_NAME = '<column>';

Basically, we changed REFERENCED_TABLE_NAME with REFERENCED_COLUMN_NAME in the where clause.

Solution 2

EDIT: As pointed out in the comments, this is not the correct answer to the OPs question, but it is useful to know this command. This question showed up in Google for what I was looking for, and figured I'd leave this answer for the others to find.

SHOW CREATE TABLE `<yourtable>`;

I found this answer here: MySQL : show constraints on tables command

I needed this way because I wanted to see how the FK functioned, rather than just see if it existed or not.

Solution 3

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';

Solution 4

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;

Solution 5

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.

Share:
544,979
Christian Oudard
Author by

Christian Oudard

My programming interests include web development, databases, algorithms, AI, and optimization. My favorite language is Python.

Updated on April 16, 2022

Comments

  • Christian Oudard
    Christian Oudard about 2 years

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

  • Christian Oudard
    Christian Oudard over 15 years
    actually, that points the wrong direction. that query shows all the foreign keys pointing FROM 'mytable', not all foreign keys pointing TO 'mytable'.
  • moriesta
    moriesta almost 12 years
    this always gives me an empty set, while query proposed by Node below works fine
  • Vinko Vrsalovic
    Vinko Vrsalovic almost 12 years
    @Acute: Are you sure you are asking about the correct table? If Node's query work, then you are likely asking about the other direction (i.e., keys FROM mytable, not keys TO mytable.) This expecting you wrote '<table>' with the table name and without the '<' and '>'?
  • moriesta
    moriesta almost 12 years
    Seems like I misunderstood you query, because I was querying for keys referencing FROM the <table> :) (yes, I wrote table name instead of "<table>" XD)
  • Kohányi Róbert
    Kohányi Róbert almost 12 years
    This one works better in my case. I need to drop every foreign key constraint (and only those) from a table to be able to change the InnoDB engine MyISAM or NDB.
  • guigouz
    guigouz over 11 years
    Those queries run much faster (from 2 secs to 0.0015 secs) if you specify k.TABLE_SCHEMA = DATABASE() and k.TABLE_NAME = '<table>' on the WHERE clause, as documented here dev.mysql.com/doc/refman/5.5/en/…
  • ChrisV
    ChrisV over 10 years
    You can get foreign keys in both directions from the REFERENTIAL_CONSTRAINTS table – I have added another answer with the query.
  • Barmar
    Barmar about 10 years
    This shows all the constraints in <yourtable>, not all the constraints that point to <yourtable>.
  • user12345
    user12345 almost 10 years
    Unless you are sure your table name is unique, you'll probably want to restrict your query to a particular database as well. Change the where clause to this: where REFERENCED_TABLE_SCHEMA = 'mydatabase' and REFERENCED_TABLE_NAME = 'mytable'
  • Mark Amery
    Mark Amery over 9 years
    As @Barmar says, this is entirely wrong; it will show the foreign keys belonging to the specified table, but will not show foreign keys pointing TO the table, which is what the question asks for. No idea how this got 50 upvotes; I guess people ended up here when really they were looking for the answer to the opposite question, found their answer here anyway, and didn't bother reading the original question (or even its title) before upvoting.
  • Luke Madhanga
    Luke Madhanga over 9 years
    I personally prefer this answer as using the REFERENTIAL_CONSTRAINTS table gives you the update and cascade rule. +1
  • Deepak Ram
    Deepak Ram over 9 years
    This wont work in case of non root user even though the user have all permissions to the database
  • A_user
    A_user over 9 years
    Doing discovery about a table you shouldn't forget that foreign keys can be established BOTH ways!
  • Beau Bouchard
    Beau Bouchard about 9 years
    great answer. Do you have any solutions for MyISAM?
  • Andy
    Andy about 9 years
    MyISAM does not support foreign keys, unfortunately. dev.mysql.com/doc/refman/5.7/en/myisam-storage-engine.html
  • Jigar
    Jigar almost 9 years
    @MarkAmery : this is first result of display foreign keys mysql in google, may be that is why ;)
  • william.eyidi
    william.eyidi over 8 years
    thisi actually shows all the constraints that exist, phpmyadmin will only show you the one pointing to your table. seems good enough to avoid duplicates with an ORM tool
  • Charles Wood
    Charles Wood over 8 years
    This is what I was looking for, so thanks for posting it even though it didn't answer OP's question. Never hurts to know how to look in both directions in a relationship!
  • Matthew Read
    Matthew Read almost 7 years
    @DeepakRam This works perfectly for me with a privileged user that is not the root user.
  • omarjebari
    omarjebari over 6 years
    Agreed, i only ever get an empty set here! Andy's answer worked best for me.
  • Lealo
    Lealo over 6 years
    Why is it required so much? Why not make it as easy as how you select rows?
  • wheelerswebservices
    wheelerswebservices about 6 years
    This worked perfect for me. I have most privileges, but not root. You're a life saver.
  • Arvid
    Arvid over 5 years
    I added TABLE_SCHEMA to the list of columns (SELECT TABLE_SCHEMA, TABLE_NAME, …) which is useful in case of there are multiple databases that have tables relating to our table/column of interest.
  • DimiDak
    DimiDak about 5 years
    I love this kind of answers sir.
  • Andreas
    Andreas almost 5 years
    Thanks great solution! I needed the column name as well, to get it add: 'k.COLUMN_NAME'
  • DJDave
    DJDave almost 4 years
    I don't think the footnote "This query does assume that the constraints and all referenced and referencing tables are in the same schema." is necessarily true? I have just used KEY_COLUMN_USAGE view (very helpful) to see cross-schema constraints
  • SherylHohman
    SherylHohman over 3 years
    Please add an explanation for long term value and to turn it into a quality answer that will be more likely to be upvoted. Answers should be able to help future visitors learn something and quickly determine if elements from your solution would apply to their own coding issues.
  • AnrDaemon
    AnrDaemon about 2 years
    Knowing names of the foreign keys doesn't really help in any meaningful way, shape or form.
  • user110857
    user110857 almost 2 years
    This helped tremendously. Other answers require you to supply names. This allows you to find all the foreign keys generically. It's exactly what I was looking for.