Home > Database > Mysql Tutorial > body text

Get information about MySQL databases and tables

王林
Release: 2023-08-25 23:25:13
forward
1261 people have browsed it

获取有关 MySQL 数据库和表的信息

Users may forget the name of a database or table, the structure of a table, or the names of columns. This problem can be solved using MySQL because it supports many statements that provide information about the databases and tables it supports.

The "SHOW DATABASES" query can be used to list all databases managed by MySQL. server. To see which database is currently in use, use the "DATABASE()" function.

Let us understand this query in the following sections -

Query

mysql> SELECT DATABASE();
Copy after login

Output

+---------------------+
| DATABASE()          |
+---------------------+
| databaseInUse       |
+---------------------+
Copy after login

If no database is selected, it will result in the output of "NULL ".

To see which tables the default database contains, you can use the following query -

Query
mysql> SHOW TABLES;
Copy after login

Output

+-----------------------------------+
| Tables_in_databaseInUse           |
+-----------------------------------+
| val1                              |
| val1                              |
+-----------------------------------+
Copy after login

The column names in the output generated by the above query is "Tables_in_databaseInUse" where databaseInUse is the name of the database being used/selected.

If the user wants to know more information about the table structure, he can use the "DESCRIBE" statement. It will display information about the columns of each table -

Query

mysql> DESCRIBE pet;
Copy after login

Output

+---------+-------------+-------+------+-----------+--------+
| Field   | Type        | Null  | Key  | Default   | Extra  |
+---------+-------------+-------+------+-----------+--------+
| name    | varchar(20) | YES   |      | NULL      |        |
| owner   | varchar(20) | YES   |      | NULL      |        |
+---------+-------------+-------+------+-----------+--------+
Copy after login

field represents the column name, 'Type' represents the data type of the column, ' NULL' indicates whether the column can contain NULL values, 'Key' indicates whether the column is indexed, and "default" specifies the default value of the column. "Extra" displays special information about the column. If the column was created with the "AUTO_INCRMENT" option, the value is "auto_increment", not empty.

The above is the detailed content of Get information about MySQL databases and tables. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!