What if you forget the name of a database or table, or what is the structure of a given table (for example, what is its column name? MySQL solves this problem by providing several statements about the database and its supported tables. You have seen SHOWDATABASES, which lists the databases managed by the server. To find the DATABASE selected, use the DATABASE manual.
What if you forget the name of a database or table, or what is the structure of a given table (for example, what is its column name? MySQL solves this problem by providing several statements about the database and its supported tables.
You have seen show databases, which lists the DATABASES managed by the server. To find the DATABASE selected, use the DATABASE () function:
Mysql> select database ();
+ ------------ +
| DATABASE () |
+ ------------ +
| Menagerie |
+ ------------ +
If you have not selected any database, the result is blank.
To find out what table the current database contains (for example, when you cannot determine the name of a table), run the following command:
Mysql> show tables;
+ --------------------- +
| Tables in menagerie |
+ --------------------- +
| Event |
| Pet |
+ --------------------- +
If you want to know the structure of a table, the DESCRIBE command is very useful; it displays information about each column of a table:
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 indicates the column data Type. Null indicates whether the column can contain NULL values. Key indicates whether the column is indexed and Default indicates the Default value of the column.
If you have an INDEX on a table, show index from tbl_name generates information about the indexes.