admin管理员组

文章数量:1638917

安装mysql后,会自动创建好几个数据库,其中就包括information_schema。今天就来聊聊这个数据库的作用,以及我们可以利用这个库做什么。

其实从名字应该就可以看出作用了,这个库主要存储schema信息(元数据)。

什么叫元数据?

数据库中有很多数据,比如某一行某一列的数据是150,那这个150指的是height(身高)呢,还是weight(体重)呢?

这里的height和weight就是描述数据的数据,也就是mysql中的属性名(或者说列名)。

除此之外,数据库名、表名、列名以及其他的定义信息(如字符编码、主键标识)都可以称之为元数据。

我们可以重点看下schemata表、tables表、columns表

schemata

查一下这个表有什么

mysql> use information_schema;
Database changed

mysql> select * from schemata;
+--------------+--------------------+----------------------------+------------------------+----------+
| CATALOG_NAME | SCHEMA_NAME        | DEFAULT_CHARACTER_SET_NAME | DEFAULT_COLLATION_NAME | SQL_PATH |
+--------------+--------------------+----------------------------+------------------------+----------+
| def          | information_schema | utf8                       | utf8_general_ci        | NULL     |
| def          | muke               | utf8mb4                    | utf8mb4_general_ci     | NULL     |
| def          | mysql              | utf8mb4                    | utf8mb4_general_ci     | NULL     |
| def          | performance_schema | utf8                       | utf8_general_ci        | NULL     |
| def          | sys                | utf8                       | utf8_general_ci        | NULL     |
| def          | test               | utf8mb4                    | utf8mb4_general_ci     | NULL     |
+--------------+--------------------+----------------------------+------------------------+----------+
6 rows in set (0.02 sec)

其实和 show databases的结果是一样的,多了一些编码信息。

tables

tables表用来描述表信息,字段就比较多了。

挑几个比较熟悉的说说

table_name            表名

table_schema        表所在的数据库名

table_type              两种取值:BASE TABLE   和   VIEW。 由此可见这个information_schema存的不仅仅是基础表,也有可能是视图。

engine                   表所用的引擎

create_time           创建时间

table_comment      表备注

查一下我之前创建的数据库(test)看看

mysql> select table_name, table_type, engine, create_time, table_comment from tables where table_schema = 'test';
+--------------+------------+--------+---------------------+---------------+
| table_name   | table_type | engine | create_time         | table_comment |
+--------------+------------+--------+---------------------+---------------+
| tb_teacher   | BASE TABLE | InnoDB | 2021-03-24 13:58:53 | 教师表        |
| tb_user_info | BASE TABLE | InnoDB | 2021-03-24 13:58:38 | 用户表        |
+--------------+------------+--------+---------------------+---------------+
2 rows in set (0.05 sec)

columns表

columns表描述每个列的信息

table_schema      所属数据库

table_name          所属表的名字

column_name      列名

ordinal_position    在所属表里的顺序

is_nullable            是否可为null

data_type             数据类型  

column_key         可能的取值有: 空(可重复的普通列)、PRI(表示该列是主键的组成部分)、UNI(唯一索引的第一列,或者说前导列)

extra                     可能的取值有: 空 、auto_increment(自增)

column_comment  表备注

测试一下:

mysql> select table_schema, table_name, column_name, ordinal_position, is_nullable, data_type, column_key, extra, column_comment from columns where table_name = 'tb_user_info';
+--------------+--------------+-------------+------------------+-------------+-----------+------------+----------------+----------------+
| table_schema | table_name   | column_name | ordinal_position | is_nullable | data_type | column_key | extra          | column_comment |
+--------------+--------------+-------------+------------------+-------------+-----------+------------+----------------+----------------+
| test         | tb_user_info | id          |                1 | NO          | int       | PRI        | auto_increment | 主键自增       |
| test         | tb_user_info | name        |                2 | YES         | varchar   |            |                | 姓名           |
| test         | tb_user_info | age         |                3 | YES         | int       |            |                | 年龄           |
| test         | tb_user_info | gender      |                4 | YES         | tinyint   |            |                | 性别           |
| test         | tb_user_info | create_by   |                5 | YES         | int       |            |                | 创建人         |
| test         | tb_user_info | update_by   |                6 | YES         | int       |            |                | 更新人         |
| test         | tb_user_info | create_time |                7 | YES         | datetime  |            |                | 创建时间       |
| test         | tb_user_info | update_time |                8 | YES         | datetime  |            |                | 更新时间       |
| test         | tb_user_info | deleted     |                9 | YES         | tinyint   |            |                | 回收站标志     |
+--------------+--------------+-------------+------------------+-------------+-----------+------------+----------------+----------------+
9 rows in set (0.03 sec)

我们可以通过information_schema来做些什么?

1、可以做可视化工具

比如navicat中的这些数据库名、表名、列名都是从这个库读取的。

2、可以做一些代码生成工具

连接数据库后,读取information_schema库中的schema信息,做一些类型转换、字符串处理,再借助模板引擎(比如 freemarker)即可逆向生成JavaBean、Controller、Service等相关的代码。

本文标签: 数据库是用来mysqlinformationschema