[Exclusively for beginners] MySQL queries all table names and table structures of the database and their comments

1. First understand INFORMATION_SCHEMA
1. In MySQL, INFORMATION_SCHEMA is regarded as a database, or to be precise, it is an information database. It holds information about all other databases maintained by the MySQL server. Such as database name, database tables, data types and access rights of table columns, etc. In INFORMATION_SCHEMA, there are several read-only tables. They are actually views, not base tables, so you won't be able to see any files related to them.

2. TABLES table: Provides information about tables in the database (including views). It describes in detail which schema a certain table belongs to, table type, table engine, creation time and other information. The result of show tables from schemaname is taken from this table.

3. COLUMNS table: Provides column information in the table. Detailed description of all columns of a table and information about each column. The result of show columns from schemaname.tablename is taken from this table.
 

ViewftpAll table names, table data volumes, table comments, field names, field types, and defaults starting with oemp in the database Values, field comments, etc.; if you check the entire database, delete them all after ftp.

            string sql = $@"SELECT TABLE_NAME as TableName, 
                     column_name AS DbColumnName,
                     CASE WHEN  left(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)-1)='' THEN COLUMN_TYPE ELSE  left(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)-1) END AS DataType,
                     CAST(SUBSTRING(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)+1,LOCATE(')',COLUMN_TYPE)-LOCATE('(',COLUMN_TYPE)-1) AS signed) AS Length,
                     column_default  AS  `DefaultValue`,
                     column_comment  AS  `ColumnComment`,
                     CASE WHEN COLUMN_KEY = 'PRI' THEN true ELSE false END AS `IsPrimaryKey`,
                     CASE WHEN EXTRA='auto_increment' THEN true ELSE false END as IsIdentity,
                     CASE WHEN is_nullable = 'YES' THEN true ELSE false END AS `IsNullable`
                     FROM Information_schema.columns where TABLE_NAME='{tableName}' and  TABLE_SCHEMA=(select database()) ORDER BY TABLE_NAME";

SELECT  
    T1.TABLE_COMMENT 表注释,
    T1.TABLE_ROWS 表数据量,
    T2.TABLE_NAME 表名,
    T2.COLUMN_NAME 字段名,  
    T2.COLUMN_TYPE 数据类型,  
    T2.DATA_TYPE 字段类型,  
    T2.CHARACTER_MAXIMUM_LENGTH 长度,  
    T2.IS_NULLABLE 是否为空,  
    T2.COLUMN_DEFAULT 默认值,  
    T2.COLUMN_COMMENT 字段备注   
FROM 
    INFORMATION_SCHEMA.TABLES T1
LEFT JOIN
    INFORMATION_SCHEMA.COLUMNS T2
ON
    T1.TABLE_NAME = T2.TABLE_NAME
WHERE  
    T1.TABLE_SCHEMA ='ftp'
AND 
    T1.TABLE_NAME LIKE 'oemp%'
ORDER BY 
    T1.TABLE_NAME;

2. How to obtain all table names

The basic statement is

SELECT table_name FROM information_schema.tables

But this does not meet business needs, because it will return all table names, and the business needs to limit which database it is, and different businesses may use different table prefixes, so it is best to limit the table prefix and need to display Notes on the table, otherwise everyone would not know which business the table belongs to.

So, the complete SQL statement is as follows

SELECT
	TABLE_NAME,
	TABLE_COMMENT 
FROM
	information_schema.TABLES 
WHERE
	TABLE_SCHEMA = 'TABLE_SCHEMA' 
	AND TABLE_NAME LIKE 'x_%' 
	AND TABLE_NAME NOT LIKE 'xx_exp%' 
ORDER BY
	TABLE_NAME

Several parameters need to be configured, and they have been sorted by table name. TABLE_COMMENT is the table comment.

  1. TABLE_SCHEMA database name
  2. x_ table prefix

The running result is as shown below

1、查看Mysql 数据库 "ori_data"下所有表的表名、表注释及其数据量

SELECT 
TABLE_NAME 表名,TABLE_COMMENT 表注释,TABLE_ROWS 数据量
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'ori_data' 
ORDER BY TABLE_NAME;
SELECT* FROM OPENQUERY (MYSQLTEST ,'
SELECT
	TABLE_NAME as 表名
	 
FROM
	information_schema.TABLES 
WHERE
	TABLE_SCHEMA = ''msldbalitest'' 
	AND TABLE_NAME LIKE ''tp_%'' 
	AND TABLE_NAME NOT LIKE ''cms_exp%'' 
    ORDER BY TABLE_NAME desc

')

2. 查询数据库 ‘ori_data’ 下表 ‘accumulation’ 所有字段注释

SELECT 
COLUMN_NAME 字段名,column_comment 字段注释 
FROM INFORMATION_SCHEMA.Columns 
WHERE table_name='accumulation' AND table_schema='ori_data'

select COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT from information_schema.COLUMNS where table_name = '表名' and table_schema = '数据库名称';
SELECT* FROM OPENQUERY (MYSQLTEST ,'
SELECT 
COLUMN_NAME as 字段名,DATA_TYPE,column_comment as 字段注释 
FROM INFORMATION_SCHEMA.Columns 
WHERE table_name=''cms_goods'' AND table_schema=''msldbalitest''

')

3. 查询数据库 "ori_data" 下所有表的表名、表注释以及对应表字段注释

SELECT 
a.TABLE_NAME 表名,a.TABLE_COMMENT 表注释,b.COLUMN_NAME 表字段,b.COLUMN_TYPE 字段类型,b.COLUMN_COMMENT 字段注释
FROM information_schema.TABLES a,INFORMATION_SCHEMA.Columns b 
WHERE b.TABLE_NAME=a.TABLE_NAME AND a.TABLE_SCHEMA='ori_data'
SELECT* FROM OPENQUERY (MYSQLTEST ,'
SELECT 
a.TABLE_NAME as 表名,a.TABLE_COMMENT as 表注释,b.COLUMN_NAME as 表字段,b.COLUMN_TYPE as 字段类型,b.COLUMN_COMMENT as 字段注释
FROM information_schema.TABLES a,INFORMATION_SCHEMA.Columns b 
WHERE b.TABLE_NAME=a.TABLE_NAME AND a.TABLE_SCHEMA=''msldbalitest''

')

The information_schema database is the database that comes with the MySQL database. It stores all the information of the MySQL database, including data tables, data comments, data table indexes, database permissions, etc.

How does the Mysql database obtain all table names (excluding table structures) of a database? The Sql is as follows:

SELECT 
	table_name 
FROM 
	information_schema.tables 
WHERE table_schema = 'xxx' AND table_type = 'base table'

information_schema: Mysql's own database, which stores information related to various databases. Most of the tables are views
information_schema.tables: tables under this database
table_schema: a field under the tables table, database name
table_type: a field under the tables table, table type, base table is the base table, note: there are spaces
table_name: a field under the tables table, the name of the data table
 

View the fields and comments of the specified table

SELECT* FROM OPENQUERY (MYSQLTEST ,'
select
	a.ordinal_position,
	a.COLUMN_name,
	a.COLUMN_type,
	a.COLumn_comment,
	a.is_nullable,
	a.column_key
from
	information_schema.COLUMNS a
where
	TABLE_schema = ''msldbalitest''
	and TABLE_name = ''cms_admin_menu''

')

View all table names and comments in the data

SELECT* FROM OPENQUERY (MYSQLTEST ,'
select
	t.TABLE_NAME,
	t.TABLE_COMMENT
from
	information_schema.tables t
where
	t.TABLE_TYPE = ''BASE TABLE''
	and TABLE_schema = ''msldbalitest''


')

In mysql, the information_schema database stores information about all databases of the mysql server.
Including database name, database table, data type of table field, etc.
In short, if you want to know which libraries, which tables are in mysql, what fields are in the tables and their comments, you can get them from information_schema
 

COLUMNS table
The COLUMNS table in the information_schema library stores the field details of all MySQL tables.

Commonly used columns
TABLE_SCHEMA: database name
TABLE_NAME: data table name
COLUMN_NAME: data column name a>
DATA_TYPE: Data type, such as: varchar
COLUMN_TYPE: Data column type (including data length), such as: varchar(32)
COLUMN_COMMENT: Data column comments/description
 

            string sql = $@"SELECT TABLE_NAME as TableName, 
                     column_name AS DbColumnName,
                     CASE WHEN  left(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)-1)='' THEN COLUMN_TYPE ELSE  left(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)-1) END AS DataType,
                     CAST(SUBSTRING(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)+1,LOCATE(')',COLUMN_TYPE)-LOCATE('(',COLUMN_TYPE)-1) AS signed) AS Length,
                     column_default  AS  `DefaultValue`,
                     column_comment  AS  `ColumnComment`,
                     CASE WHEN COLUMN_KEY = 'PRI' THEN true ELSE false END AS `IsPrimaryKey`,
                     CASE WHEN EXTRA='auto_increment' THEN true ELSE false END as IsIdentity,
                     CASE WHEN is_nullable = 'YES' THEN true ELSE false END AS `IsNullable`
                     FROM Information_schema.columns where TABLE_NAME='{tableName}' and  TABLE_SCHEMA=(select database()) ORDER BY TABLE_NAME";

For tables created usingMySQL, whether table comments, indexes, field types, etc., will be saved to the built-in library table ofMySQL , you can use SQL to find out the desired table and field information.
Understanding the information_schema library can have unexpected effects in your work

-- database_name替换为库名,查出库中所有表的TABLE_NAME表名、TABLE_COMMENT表注释
SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema='database_name';

TABLES table

information_schemaTheTABLES table in the library stores the table information ofMySQL all tables.

Common columns
  • TABLE_SCHEMA: database name
  • TABLE_NAME: data table name
  • TABLE_COMMENT: Data table comments/description

Query all fields of a table

select column_name,data_type,column_comment,column_key,extra,character_maximum_length,is_nullable,column_default
from information_schema.columns 
where table_schema = 'seata' and table_name = 'users' ;

Assemble all columns of table

select GROUP_CONCAT("t.",column_name) total
from information_schema.columns 
where table_schema = 'seata' and table_name = 'users' and column_name not in ('id');

Guess you like

Origin blog.csdn.net/zgscwxd/article/details/134937189