1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| 拼接语句 配合concat()函数拼接语句或命令
1.模仿以下语句,进行数据库的分库分表备份。 mysqldump -uroot -p123 world(库名) city(表名) >/databak/world_city.sql
SELECT concat("mysqldump -uroot -p123 ",table_schema," ",table_name," >/databak/",table_schema,"_",table_name,".sql") FROM information_schema.tables WHERE table_schema NOT IN ('mysql','sys','information_schema','performance_schema') into outfile '/tmp/bak.sh' ;
2.模仿以下语句,进行批量生成,对world库下所有表进行操作。 alter table world.city discard tablespace;
SELECT concat("alter table",table_schema," ",table_name,"discard tablespace") FROM information_schema.tables WHERE table_schema='world';
a. 查询一下业务数据库中,非InnoDB的表 SELECT table_schema , table_name ,engine FROM information_schema.tables WHERE table_schema NOT IN ('mysql','sys','information_schema','performance_schema') AND ENGINE <>'innodb';
b. 将非InnoDB表批量替换为InnoDB SELECT concat("alter table ",table_schema,".",table_name," engine=innodb;") FROM information_schema.tables WHERE table_schema NOT IN ('mysql','sys','information_schema','performance_schema','world') AND ENGINE <>'innodb' into outfile '/tmp/alter.sql' ;
|