Mysql查询所有表和字段信息的方法

吾爱主题 阅读:143 2023-04-16 17:46:00 评论:0

1 MySQL中information_schema是什么

  • information_schema数据库是MySQL自带的,它提供了访问数据库元数据的方式。
  • 元数据:元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等。有些时候用于表述该信息的其他术语包括“数据字典”和“系统目录”。
  • 在MySQL中,把information_schema看作是一个数据库,确切说是信息数据库。其中保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权限等。
  • information_schema数据库表说明:

  schemata表:提供了当前mysql实例中所有数据库的信息。是show databases的结果取之此表。

  tables表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。是show tables from schemaname的结果取之此表。

  columns表:提供了表中的列信息。详细表述了某张表的所有列以及每个列的信息。是show columns from schemaname.tablename的结果取之此表。

  statistics表:提供了关于表索引的信息。是show index from schemaname.tablename的结果取之此表。

  user_privileges(用户权限表)表:给出了关于用户权限的信息。该信息源自mysql.user授权表。是非标准表。

  schema_privileges(方案权限表)表:给出了关于方案(数据库)权限的信息。该信息来自mysql.db授权表。是非标准表。

  table_privileges(表权限)表:给出了关于表权限的信息。该信息源自mysql.tables_priv授权表。是非标准表。

  column_privileges(列权限)表:给出了关于列权限的信息。该信息源自mysql.columns_priv授权表。是非标准表。

  character_sets(字符集)表:提供了mysql实例可用字符集的信息。是show character set结果集取之此表。

  collations表:提供了关于各字符集的对照信息。

  collation_character_set_applicability表:指明了可用于校对的字符集。这些列等效于show collation的前两个显示字段。

  table_constraints表:描述了存在约束的表。以及表的约束类型。

  key_column_usage表:描述了具有约束的键列。

  routines表:提供了关于存储子程序(存储程序和函数)的信息。此时,routines表不包含自定义函数(UDF)。名为“mysql.proc name”的列指明了对应于information_schema.routines表的mysql.proc表列。

  views表:给出了关于数据库中的视图的信息。需要有show views权限,否则无法查看视图信息。

  triggers表:提供了关于触发程序的信息。必须有super权限才能查看该表。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 select * from information_schema.schemata; select * from information_schema.tables; select * from information_schema.columns; select * from information_schema. statistics ; select * from information_schema.user_privileges; select * from information_schema.schema_privileges; select * from information_schema.table_privileges; select * from information_schema.column_privileges; select * from information_schema.character_sets; select * from information_schema.collations; select * from information_schema.collation_character_set_applicability; select * from information_schema.table_constraints; select * from information_schema.key_column_usage; select * from information_schema.routines; select * from information_schema.views; select * from information_schema.triggers;

2 根据库名获取所有表的信息

【information_schema.`TABLES`】

?
1 select * from information_schema. tables where table_schema = '库名' ;

3 根据库名获取所有的字段信息

【information_schema.`COLUMNS`】

?
1 select * from information_schema. columns

4 根据库名获取所有的表和表字段的基本信息

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 select c.table_schema             as '库名' ,         t.table_name               as '表名' ,         t.table_comment            as '表注释' ,         c.column_name              as '列名' ,         c.column_comment           as '列注释' ,         c.ordinal_position         as '列的排列顺序' ,         c.column_default           as '默认值' ,         c.is_nullable              as '是否为空' ,         c.data_type                as '数据类型' ,         c.character_maximum_length as '字符最大长度' ,         c.numeric_precision        as '数值精度(最大位数)' ,         c.numeric_scale            as '小数精度' ,         c.column_type              as 列类型,         c.column_key 'KEY' ,         c.extra                    as '额外说明'    from information_schema. tables t    left join information_schema. columns c      on t.table_name = c.table_name     and t.table_schema = c.table_schema   where t.table_schema = 'dvmdm'   order by c.table_name, c.ordinal_position;

5 查询某个字段在多个数据库表中的分布

?
1 2 3 4 5 6 7 8 9 select group_concat(table_schema separator ',' ) as '库名' ,         group_concat(table_name separator ',' ) as '表名' ,         column_name as '列名' ,         group_concat(column_type separator ',' ) as '列类型' ,         group_concat(column_comment separator ',' ) as '注释'    from information_schema. columns   where table_schema in ( '库1' , '库2' , '库3' )   group by column_name   order by group_concat(table_schema separator ',' ), column_name

到此这篇关于Mysql查询所有表和字段信息的方法的文章就介绍到这了,更多相关mysql查询所有表和字段信息内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/daytoy105/p/17321087.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容