What if you forget the name of a database or table, or what the structure of a given table is (for example, what its columns are called)? MySQL addresses this problem through several statements that provide information about the databases and tables it supports.
You have previously seen SHOW DATABASES, which
lists the databases managed by the server. To find out which
database is currently selected, use the
DATABASE() function:
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| menagerie |
+------------+
If you have not yet selected any database, the result is
NULL.
To find out what tables the default database contains (for example, when you are not sure about the name of a table), use this command:
mysql> SHOW TABLES;
+---------------------+
| Tables in menagerie |
+---------------------+
| event |
| pet |
+---------------------+
If you want to find out about the structure of a table, the
DESCRIBE command is useful; it displays
information about each of a table's columns:
mysql> DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
Field indicates the column name,
Type is the data type for the column,
NULL indicates whether the column can contain
NULL values, Key indicates
whether the column is indexed, and Default
specifies the column's default value.
If you have indexes on a table, SHOW INDEX FROM
produces information
about them.
tbl_name

User Comments
The following query might also be of interest in order to get some information about a particular table:
mysql> SHOW CREATE TABLE <table-name>;
where <table-name> is the name of the table.
The show create table <table-name>;
command shows the command you would use to recreate the table as it currently exists in the database.
The DESCRIBE pet; example shows an 'Extra' column but does not explain its purpose.
The "Extra" field records special information about columns.
If you have selected the "auto_increment" functionality for a column, for example, that would show up in the "Extra" field when doing a "describe".
You can also use DESC to describe a table instead of using DESCRIBE
While working with Visual Basic to create an interface with a MySQL database using MyOBDC, I was puzzeled by how to get a list of the names of the tables in a MySQL database. Typically when using an access database there is an object called TableDefs which has a property called Name. Not the case with MySQL. After several hours of head banging I realized that you simply run a query as in MyQueryStr = "Show Tables" and you'll get a recordset of a listing of the tables. One important point to make here is the name of the field that holds the names of the database fields. If you have a database called MyData, the name of the field holding the names of the tables when you run the database is "Tables_in_MyData" (note the underscore).
Add your own comment.