Home > Database > Mysql Tutorial > MySQL Getting Started Tutorial 6 - Obtaining Database and Table Information

MySQL Getting Started Tutorial 6 - Obtaining Database and Table Information

黄舟
Release: 2017-02-23 11:41:42
Original
1321 people have browsed it

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  |
+------------+
Copy after login

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                 |
+---------------------+
Copy after login

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;
Copy after login
+---------+-------------+------+-----+---------+-------+
| 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    |       |
+---------+-------------+------+-----+---------+-------+
Copy after login

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 (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template