DB - MySQL - 数据库表结构表字段信息
文字编写时间: 2022-05-08 17:53 (如下内容为本人工作中总结,只是现在才放到博客上,欢迎大家查阅)
1. MySql函数
http://www.baidu.com/index.php?tn=mswin_oem_dg
2. 查询数据库中所有表
SELECT * FROM information_schema.tables
WHERE table_schema='数据库名称';
3. 数据库所占空间
SELECT CONCAT(TRUNCATE(SUM(data_length)/1024/1024,2),'MB') AS data_size,
CONCAT ( TRUNCATE ( SUM(max_data_length)/1024/1024,2),'MB') AS max_data_size,
CONCAT(TRUNCATE(SUM(data_free)/1024/1024,2),'MB') AS data_free,
CONCAT(TRUNCATE(SUM(index_length)/1024/1024,2),'MB') AS index_size
FROM information_schema.tables WHERE TABLE_SCHEMA = '数据库名';
4. 查询表空间使用情况
SELECT '01',CONCAT(TRUNCATE(SUM(data_length)/1024/1024,2),'MB') AS data_size,
CONCAT (TRUNCATE(SUM(max_data_length)/1024/1024,2),'MB') AS max_data_size,
CONCAT(TRUNCATE(SUM(data_free)/1024/1024,2),'MB') AS data_free,
CONCAT(TRUNCATE(SUM(index_length)/1024/1024,2),'MB') AS index_size
FROM information_schema.tables WHERE TABLE_NAME = '表名';
5. 查询表字段并合并
SELECT GROUP_CONCAT(column_name) FROM information_schema.columns
WHERE table_name='表名';
6. 查看分区
查看分区对应的表:INFORMATION_SCHEMA. PARTITIONS
查看表具体分区数据量
SELECT PARTITION_NAME,TABLE_ROWS,a.index_length,a.data_length,a.* FROM INFORMATION_SCHEMA.PARTITIONS a
WHERE TABLE_NAME = '表名' ; (注意需要自行根据自己的业务分区字段来限定查询的分区内容)
7. 查询表字段、字段类型、字段注释 (如下SQL结构类似java实体对象信息。请各自根据实际需要进行调整sql)
SELECT
# GROUP_CONCAT(column_name)
CONCAT('//',a.column_comment),
'private ',
CASE
WHEN a.DATA_TYPE = 'VARCHAR2' THEN 'String '
WHEN a.DATA_TYPE = 'bigint' THEN 'long'
WHEN a.DATA_TYPE = 'datetime' THEN 'Date'
WHEN a.DATA_TYPE = 'int' THEN 'int'
ELSE
'String'
END AS java_type,
column_name, ';',a.*
FROM information_schema.columns a
WHERE table_name='表名';
8. 查询表和表字段
查询数据库中所有表名 (table_schema和table_type如果不确定可以先查询出所有再根据具体需要进行筛选查询)
select table_name from information_schema.tables
Where table_schema='库名' and table_type='表类型';
9. 查询指定数据库中指定表的所有字段名column_name
select column_name from information_schema.columns
Where table_schema='库名' and table_name='表名';
10. 查询表结构 (字段名、字段类型、字段注释说明,以及是否必填)
SELECT column_name, column_type, column_comment,
CASE
WHEN a.is_nullable='YES' THEN 'Y'
ELSE 'N'
END
FROM information_schema.columns a
WHERE table_name='表名';