推荐阅读: 使用SQL查询所有数据库名和表名
查询语句
- 查询所有数据库
show databases;
- 查询指定数据库中所有表名
select table_name from information_schema.tables where table_schema='database_name'
- 查询指定表中的所有字段名
select column_name from information_schema.columns where table_schema='database_name' and table_name='table_name';
- 查询指定表中的所有字段名和字段类型
select column_name,data_type from information_schema.columns where table_schema='database_name' and table_name='table_name';
- 检查 mysql 数据库是否存在
SELECT information_schema.SCHEMATA.SCHEMA_NAME FROM information_schema.SCHEMATA where SCHEMA_NAME='database_name'; 或 CREATE DATABASE IF NOT EXISTS data_name;
- 查询指定数据库下,是否存在某张表
SELECT DISTINCT t.table_name, n.SCHEMA_NAME FROM information_schema.TABLES t, information_schema.SCHEMATA n WHERE t.table_name = 'tableName' AND n.SCHEMA_NAME = 'databaseName';
information_schema.tables字段说明
table_catalog
: 数据表登记目录table_schema
: 数据表所属的数据库名table_name
: 表名称table_type
: 表类型[system view|base table]engine
: 使用的数据库引擎[MyISAM|CSV|InnoDB]version
: 版本,默认值10row_format
: 行格式[Compact|Dynamic|Fixed]table_rows
: 表里所存多少行数据avg_row_length
: 平均行长度data_length
: 数据长度max_data_length
: 最大数据长度index_length
: 索引长度data_free
: 空间碎片auto_increment
: 做自增主键的自动增量当前值create_time
: 表的创建时间update_time
: 表的更新时间check_time
: 表的检查时间table_collation
: 表的字符校验编码集checksum
: 校验和create_options
: 创建选项table_comment
: 表的注释、备注