8

Say I have a table "products" and I would like to check if this table has any indexes, foreign keys etc

A "DESCRIBE products" would give some information.

Field        Type           Null    Key    Default   Extra
productCode  varchar(200)   NO      MUL    NULL  
description  varchar(500)   NO             NULL 

Mainly the key field in the case. But defenietly no references and what table is linked to who etc etc.

What is the best way to get such information via SQL about a table?

Thanks

Gabriel Spiteri
  • 4,896
  • 12
  • 43
  • 58
  • http://stackoverflow.com/questions/806989/mysql-how-to-i-find-all-tables-that-have-foreign-keys-that-reference-particular – Jacob May 19 '11 at 09:44

5 Answers5

12

You can use

SHOW CREATE TABLE products

which gives you the query for creating the table. The most informative.

Jai
  • 3,549
  • 3
  • 23
  • 31
7

SHOW INDEXES IN <tablename> will give all the indexes in that table.

Amareswar
  • 2,048
  • 1
  • 20
  • 36
0

I don't know about foreign keys, but SHOW INDEX FROM tablename; gives a lot of easily parsed information.

Emil Vikström
  • 90,431
  • 16
  • 141
  • 175
0

The best way is to use the INFORMATION_SCHEMA as documented here:

http://dev.mysql.com/doc/refman/5.5/en/information-schema.html

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
-1
$result = mysql_query("SHOW FIELDS FROM $DATABASE.$TABLE_NAME");

$i = 0;

while ($row = mysql_fetch_array($result)) {
  echo $row['Field'] . ' ' . $row['Type'];
}
linktoahref
  • 7,812
  • 3
  • 29
  • 51
krifur
  • 870
  • 4
  • 16
  • 36