2

I want to change the character-set of all tables present in my DB. To change character set of one table I can execute it as:

"alter table mydb.mytable1 CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_Ci;"

But i Have many tables in my 'my db' database. List of Tables we can read from information_schema table. But i am not sure how to form the Exact query so that with one query I can change character set of all the tables at once.

Pratik
  • 109
  • 1
  • 5
  • 14

2 Answers2

1
-- https://stackoverflow.com/a/39465494/819417
SELECT concat('ALTER TABLE ', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;')
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'your_db_name'

Then copy/paste all results and use a db tool or scripting language to run them one by one.

To fix future tables as you add them, run this as well:

ALTER DATABASE your_db_name CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124
0

Alter Database:

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Note: Use "utf8_unicode_ci" instead of "utf8_general_ci"

So if you can't do this with Alter DB then you have to run this for every table individually

Manwal
  • 23,450
  • 12
  • 63
  • 93