Sep
5
2009
5
2009
List foreign keys in mysql
One interesting way of listing foreign keys is like this :
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;
which is found here
and gives an output like this :
+-----------------------+-------------+ | foreign key | references | +-----------------------+-------------+ | orders.client_id | clients.id | | line_items.order_id | orders.id | | line_items.product_id | products.id | +-----------------------+-------------+
Another way would be to :
SHOW CREATE TABLE tablename;
which displays the sql query to create the table and has the foreign key constraints too.
Apart from this phpmyadmin too shows the foreign keys on the top of a table structure.
References :
1. Mysql Docs
Popularity: 17% [?]
Tags: mysql, phpmyadmin
Related Posts
Subscribe
Recent Posts
- Compile wxwebconnect on Ubuntu 11.04 64 bit
- Disqus Comments Importer Script in PHP
- Beginners’ guide to socket programming with winsock
- Handle multiple socket connections with fd_set and select on Linux
- Beginners guide to socket programming in C on Linux
- Gui whois client in python with wxpython
- Whois client code in C with Linux sockets
- str_replace for C
- Easy to use C/C++ IDE for Ubuntu Linux
- Get local ip in C on linux
Binarytides
Tags
apache
applications
box2d
bsnl
c
chrome
cron
css
database
dns
firefox
flash
freelance
game programming
gd
graphs
hacking
htaccess
html
html5
imagemagick
java
javascript
libpcap
linux
mod rewrite
moneybookers
mootools
mvc
mysql
networking
payment
paypal
php
phpmyadmin
python
ruby
security
Sockets
software
swing
ubuntu
winpcap
winsock
xdebug
An article by Binary Tides





The show create table command is great.. too bad describe [table] doesn’t provide more information about keys and such.
Thank you, I’ve tried the first solution and it worked perfectly.
Thanks a lot!!! Super!!! Finally someone posted a real solution.
It doesn’t work. Because the referenced_table_name IS NULL. Is this a MySQL bug ?
Can your provide the schema definition ?