旭东 带你提升工作中的B格,解决需求


日常工作中,遇到类似下面的应用场景:

删除数据库test下所有前缀为don的表;

将数据库test下面所有存储引擎为MyISAM的表改为InnoDB.

 

5.0之后,提供一个新的数据库information_schema,用来记录MySQL中元数据信息。元数据指的是数据的数据,比如表明、列名、列类型、索引名等表的各种属性

它是一个虚拟数据库,物理上并不存在相关的目录和文件;库里show tables显示的各种也并不是实际存在的物理表,而全部是视图

 

删除test库下面所有前缀以don开头的表

MariaDB [test]> select concat('droptable test.',table_name,';') from information_schema.tables wheretable_schema='test' and table_name like 'don%';

+-------------------------------------------+

| concat('drop table test.',table_name,';')|

+-------------------------------------------+

| drop table test.dong;                     |

| drop table test.dong_ha;                  |

| drop table test.dong_xi;                  |

+-------------------------------------------+

3 rows in set (0.00 sec)

test库下所有myisam引擎更改为innodb引擎

MariaDB [test]> select concat('altertable test.',table_name,' engine=innodb;') from information_schema.tables wheretable_schema='test' and engine='myisam';

+----------------------------------------------------------+

| concat('alter table test.',table_name,'engine=innodb;') |

+----------------------------------------------------------+

| alter table test.dong_wwengine=innodb;                  |

+----------------------------------------------------------+

1 row in set (0.00 sec)

常用的视图

SCHEMATA:提供了当前mysql实例中所有的数据库信息,showdatabases的结果取之此表

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

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

STATISTICS:该表提供了关于索引的信息。Show indexfrom schemaname.tablename的结果取之此表。