Converting MySQL Database Character Set and Collation to UTF-8
MySQL databases can be set up with various character sets and collations, which determine how text data is stored and compared. Converting an entire MySQL database to UTF-8 encoding and Unicode collation is essential for handling international characters and ensuring data integrity.
Using ALTER DATABASE and ALTER TABLE Commands
To convert the character set and collation of an entire MySQL database, use the following commands:
ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Alternative for MySQL 5.5.2 or Older
If you are still using MySQL version 5.5.2 or older, which does not support 4-byte UTF-8, use the following commands:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Note:
The above is the detailed content of How to Convert MySQL Database Character Set and Collation to UTF-8?. For more information, please follow other related articles on the PHP Chinese website!