It’s been a while since I last updated and the truth is I just don’t have time to write proper blog posts. Keeping that in mind, but still wishing to keep this up-to-date. I’ve decided to post quick tips and snippets that I find useful. Here’s the first:
I’ve been doing some database work recently and I wanted to unify the character set and collation for all databases and tables. Changing the database collation was easy to do in bulk. The tables less so and I was not manually changing 13,014 tables, eventually I came up with this:
mysql --batch --skip-column-names --user=username --password=password --execute="select CONCAT('alter table ',TABLE_SCHEMA,'.',TABLE_NAME,' convert to character set utf8 collate utf8_general_ci;') from information_schema.TABLES WHERE TABLE_SCHEMA != 'information_schema' AND TABLE_SCHEMA != 'mysql' AND TABLE_SCHEMA != 'performance_schema';" | mysql --user=username --password=password
That should generate a bunch of statements that look like this:
alter table database_name.table_name convert to character set utf8 collate utf8_general_ci;
For every table in every database except the information_schema, mysql and performance_schema databases. I just pipe these right back in to MySQL but you can output them to a text file and run that later if you prefer. This snippet should also to be useful for any mass/multiple/bulk table edits or changes.