What if you forget the name of the database or table, or what the structure of a given table is (for example, what its columns are called)? MySQL Solve this problem with a few statements that provide information about the database and its supporting tables.
You have 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 selected any database, the result is NULL.
To find out what tables the current database contains (for example, when you are not sure of the name of a table), use this command:
mysql> SHOW TABLES; +---------------------+ | Tables in menagerie | +---------------------+ | event | | pet | +---------------------+
If you want to know the structure of a table, you can Use the DESCRIBE command; it displays information about each column in the 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 displays the column name, Type is the data type of the column, Null indicates whether the column can contain NULL values, Key shows whether the column is indexed and Default Specifies the default value for the column.
If the table has an index, SHOW INDEX FROM tbl_name generates information about the index.
The above is the content of MySQL introductory tutorial 6 - Obtaining database and table information. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!