Mysql database information exploration

Mysql database information exploration

(1) Query the number of tables in the database

SELECT COUNT(*) TABLES, table_schema 
FROM information_schema.TABLES
GROUP BY table_schema;

(2) Query the number of tables in the specified database (db_one)

SELECT COUNT(*) TABLES, table_schema 
FROM information_schema.TABLES
WHERE table_schema = 'db_one';

(3) Query table structure

a. Use DESCRIBE or desc

DESCRIBE a_table_name;
desc a_table_name

example:
mysql> desc tb_class;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| Id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| clsNo   | varchar(3)  | NO   |     | NULL    |                |
| clsName | varchar(20) | NO   |     | NULL    |                |
| sp_Id   | int(11)     | YES  | MUL | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

MySQL Key值(PRI, UNI, MUL)的含义:
  PRI主键约束  UNI唯一约束  MUL可以重复

b. use explain

explain a_table_name

(4) Query the primary key field name

SELECT column_name 
FROM INFORMATION_SCHEMA.`KEY_COLUMN_USAGE` 
WHERE table_name='表名' AND constraint_name='PRIMARY'

(5) View the number of fields in a table

Please compare (2) the number of rows in the result of the query table structure desc.

SELECT count(*)
FROM information_schema.`COLUMNS`
WHERE TABLE_SCHEMA = '数据库名' AND TABLE_NAME = '表名'

(6) The amount of data saved in the statistical data table

(Maybe the table information has not been updated, resulting in a different amount of data than the direct count)

use information_schema;
SELECT TABLE_NAME, TABLE_ROWS
FROM TABLES 
WHERE TABLE_SCHEMA = '库名' and table_name = '表名'

(7) Check the table creation information

show create table 表名;

(8) View partition information

show partitions 表名

 

Guess you like

Origin blog.csdn.net/Cameback_Tang/article/details/108244081