说明
MySQL下有一个特殊的数据库记录者所有的表,以及表的结构,那就是information_schema,我们可以查询这个数据库来找到所有数据库的列表,所有表的列表,以及某个表的所有列的详细信息。
需求说明1
找出某个数据库下的所有表,并修改字符集为:utf8mb4_general_ci
用到information_schema.tables 存储着全部数据库的表
-- 得到数据库是test的所有表的详细信息列表
SELECT * from information_schema.tables where table_schema ='test';
-- 打印出修改字符集的SQL,把结果复制一下,再执行
-- 会打印出类似:ALTER TABLE `test`.`t_test` COLLATE = utf8mb4_general_ci;
SELECT
concat( 'ALTER TABLE `test`.`', table_name, '` COLLATE = utf8mb4_general_ci;' )
FROM
information_schema.tables
WHERE
table_schema = 'test';
需求说明2
打印出某个表的数据结构,类似写出表的说明结构
用到information_schema.columns 存储着全部数据库的表的字段
SELECT
column_name 列名,
column_type 数据类型,
data_type 字段类型,
character_maximum_length 长度,
column_key 字段KEY类型,
extra 额外信息,
is_nullable 是否为空,
column_default 默认值,
column_comment 备注
FROM
information_schema.columns
WHERE
table_schema = 'test'
AND table_name = 't_account'
得到如下结果:

文章评论