1MySQL中information_schema是什么
information_schema数据库是MySQL自带的,它提供了访问数据库元数据的方式。
元数据:元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等。有些时候用于表述该信息的其他术语包括“数据字典”和“系统目录”。
在MySQL中,把information_schema看作是一个数据库,确切说是信息数据库。其中保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权限等。
information_schema数据库表说明:
schemata表:提供了当前mysql实例中所有数据库的信息。是showdatabases的结果取之此表。
tables表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。是showtablesfromschemaname的结果取之此表。
columns表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。是showcolumnsfromschemaname.tablename的结果取之此表。
statistics表:提供了关于表索引的信息。是showindexfromschemaname.tablename的结果取之此表。
user_privileges(用户权限表)表:给出了关于用户权限的信息。该信息源自mysql.user授权表。是非标准表。
schema_privileges(方案权限表)表:给出了关于方案(数据库)权限的信息。该信息来自mysql.db授权表。是非标准表。
table_privileges(表权限)表:给出了关于表权限的信息。该信息源自mysql.tables_priv授权表。是非标准表。
column_privileges(列权限)表:给出了关于列权限的信息。该信息源自mysql.columns_priv授权表。是非标准表。
character_sets(字符集)表:提供了mysql实例可用字符集的信息。是showcharacterset结果集取之此表。
collations表:提供了关于各字符集的对照信息。
collation_character_set_applicability表:指明了可用于校对的字符集。这些列等效于showcollation的前两个显示字段。
table_constraints表:描述了存在约束的表。以及表的约束类型。
key_column_usage表:描述了具有约束的键列。
routines表:提供了关于存储子程序(存储程序和函数)的信息。此时,routines表不包含自定义函数(UDF)。名为“mysql.procname”的列指明了对应于information_schema.routines表的mysql.proc表列。
views表:给出了关于数据库中的视图的信息。需要有showviews权限,否则无法查看视图信息。
triggers表:提供了关于触发程序的信息。必须有super权限才能查看该表。
select*frominformation_schema.schemata;
select*frominformation_schema.tables;
select*frominformation_schema.columns;
select*frominformation_schema.statistics;
select*frominformation_schema.user_privileges;
select*frominformation_schema.schema_privileges;
select*frominformation_schema.table_privileges;
select*frominformation_schema.column_privileges;
select*frominformation_schema.character_sets;
select*frominformation_schema.collations;
select*frominformation_schema.collation_character_set_applicability;
select*frominformation_schema.table_constraints;
select*frominformation_schema.key_column_usage;
select*frominformation_schema.routines;
select*frominformation_schema.views;
select*frominformation_schema.triggers;
2根据库名获取所有表的信息
【information_schema.`TABLES`】
select*frominformation_schema.tableswheretable_schema='库名';
3根据库名获取所有的字段信息
【information_schema.`COLUMNS`】
select*frominformation_schema.columns
4根据库名获取所有的表和表字段的基本信息2
selectc.table_schemaas'库名',
t.table_nameas'表名',
t.table_commentas'表注释',
c.column_nameas'列名',
c.column_commentas'列注释',
c.ordinal_positionas'列的排列顺序',
c.column_defaultas'默认值',
c.is_nullableas'是否为空',
c.data_typeas'数据类型',
c.character_maximum_lengthas'字符最大长度',
c.numeric_precisionas'数值精度(最大位数)',
c.numeric_scaleas'小数精度',
c.column_typeas列类型,
c.column_key'KEY',
c.extraas'额外说明'
frominformation_schema.tablest
leftjoininformation_schema.columnsc
ont.table_name=c.table_name
andt.table_schema=c.table_schema
wheret.table_schema='dvmdm'
orderbyc.table_name,c.ordinal_position;
5查询某个字段在多个数据库表中的分布2
selectgroup_concat(table_schemaseparator',')as'库名',
group_concat(table_nameseparator',')as'表名',
column_nameas'列名',
group_concat(column_typeseparator',')as'列类型',
group_concat(column_commentseparator',')as'注释'
frominformation_schema.columns
wheretable_schemain('库1','库2','库3')
groupbycolumn_name
orderbygroup_concat(table_schemaseparator','),column_name
|