MySQL query database table name, table comment, field name, field type, field comment

This type of query is very interesting, querying information about tables and fields in a database. It can be summarized by permutations 4species query.

1. Query all table names and table comments in the database

SELECT
	TABLE_NAME AS 表名,
	TABLE_COMMENT AS 表注释 
FROM
	INFORMATION_SCHEMA.TABLES 
WHERE
	TABLE_SCHEMA = 'dms_app_dev';

Insert picture description here

2. Query all field names, data types, and field comments in the dictionary table under the database

SELECT
	COLUMN_NAME AS 字段名,
	DATA_TYPE AS 数据类型,
	COLUMN_COMMENT AS 字段注释 
FROM
	INFORMATION_SCHEMA.COLUMNS 
WHERE
	TABLE_SCHEMA = 'dms_app_dev' 
	AND TABLE_NAME = 'dicts';

Insert picture description here

3. Query all field names, data types, and field comments under all tables in the database

SELECT
	COLUMN_NAME AS 字段名,
	DATA_TYPE AS 数据类型,
	COLUMN_COMMENT AS 字段注释 
FROM
	INFORMATION_SCHEMA.COLUMNS 
WHERE
	TABLE_SCHEMA = 'dms_app_dev';

Insert picture description here

4. Query all table names, table comments, and all field names, data types, and field comments in the database

SELECT
	t.TABLE_NAME AS 表名,
	t.TABLE_COMMENT AS 表注释,
	c.COLUMN_NAME AS 字段名,
	c.COLUMN_TYPE AS 数据类型,
	c.COLUMN_COMMENT AS 字段注释 
FROM
	INFORMATION_SCHEMA.TABLES AS t,
	INFORMATION_SCHEMA.COLUMNS AS c 
WHERE
	c.TABLE_NAME = t.TABLE_NAME 
	AND t.TABLE_SCHEMA = 'dms_app_dev';

Insert picture description here

In fact, the principle is very simple, when we create databases, tables, fields, and MySQLput the details in our data storage information_schemasystem database, so that we can SQLbe a simple query, are interested can look.

Insert picture description here

Guess you like

Origin blog.csdn.net/yilovexing/article/details/107068569