获取数据库表的表名、字段名、数据类型等信息
获取某个数据库表的字段名称
获取数据库(数据库名称)中表(表名)的列名称
select column_name
from information_schema.columns
where table_schema='数据库名称' and table_name='表名';
获取数据库表名
获取数据库(数据库名称)中表(表名)的表名称
SELECT table_name FROM information_schema.tables
WHERE table_schema='intelligentrecorder';
获取数据库表某个字段的属性
筛选出含有某个字段的数据库表,并展示该字段的属性
select table_name,COLUMN_NAME,DATA_TYPE,column_type,COLUMN_COMMENT
from information_schema.COLUMNS
where table_schema = '数据库名称' AND (COLUMN_NAME = "字段1" OR (COLUMN_NAME = "字段2"));
帮助文档
COLUMN_NAME 字段名
DATA_TYPE 简略数据类型
column_type 详细数据类型
COLUMN_COMMENT 注释
提供常用的information_schema.columns的值
- table_catalog :不管是table | view 这个列的值总是def
- table_schema :表 | 视图所在的数据库名
- table_name :表名 | 视图名
- column_name :列名
- column_default :列的默认值
- is_nullable :是否可以取空值
- data_type :列的数据类型
- character_maximum_length :列的最大长度(这列只有在数据类型为char | varchar 时才有意义)
- column_type :列类型这个类型比data_type列所指定的更加详细,如data_type 是int 而column_type 就有可以能是int(11)
- column_key :列上的索引类型 主键–>PRI | 唯一索引 -->UNI 一般索引 -->MUL
例如:
select table_catalog,table_schema,table_name,column_name,column_default,is_nullable,data_type,character_maximum_length,column_type,column_key
from information_schema.columns
where table_schema = '数据库名称' AND table_name = '表名' AND `column_name` = "字段名称";