Note that there are some explanatory texts on larger screens.

plurals
  1. POPerformant Way to List Foreign Keys for a MySQL Table?
    primarykey
    data
    text
    <p>Is there a <strong>performant</strong> way to fetch the list of foreign keys assigned to a MySQL table?</p> <p>Querying the information schema with </p> <pre><code>SELECT `column_name`, `referenced_table_schema` AS foreign_db, `referenced_table_name` AS foreign_table, `referenced_column_name` AS foreign_column FROM `information_schema`.`KEY_COLUMN_USAGE` WHERE `constraint_schema` = SCHEMA() AND `table_name` = 'your-table-name-here' AND `referenced_column_name` IS NOT NULL ORDER BY `column_name`; </code></pre> <p>works, but is painfully slow on the versions of MySQL I've tried it with. A bit of research turned up <a href="http://bugs.mysql.com/bug.php?id=19588" rel="nofollow">this bug</a>, which seems to indicate it's an ongoing issue without a clear solution. The solutions which are hinted at require reconfiguring or recompiling mysql with a patch, which doesn't work for the project I'm working on.</p> <p>I realize it's possible to issue the following</p> <pre><code>SHOW CREATE TABLE table_name; </code></pre> <p>and get a string representation of a CREATE TABLE statement, which will include the foreign key constraints. However, parsing this string seems like it would be fragile, and I don't have a large corpus of CREATE TABLE statements to test against. (if there's a standard bit of parsing code for this out there, I'd love some links)</p> <p>I also realize I can list the indexes with the following</p> <pre><code>SHOW CREATE TABLE table_name; </code></pre> <p>The list of indexes will include the foreign keys, but there doesn't appear to be a way to determine which of the indexes are foreign keys, and which are "regular" MySQL indexes. Again, some cross referencing with the SHOW CREATE table information could help here, but that brings us back to fragile string parsing.</p> <p>Any help, or even links to other smart discussions on the issue, would be appreciated. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload