How to Quickly Change Database, Table, and Column Collations
Changing the collation can be crucial to ensure proper data handling and storage. The question relates to altering the collation of an existing database, table, and column from latin1_general_ci to utf8mb4_general_ci using PhpMyAdmin.
Database Collation Alteration
To modify the default collation of a database, use the following command:
ALTER DATABASE <database_name> CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
However, this only sets the new default for newly created tables.
Table Collation Alteration
To change the collation of a specific table, utilize the following command:
ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Column Collation Alteration
For modifying the collation of a single column, use the following command:
ALTER TABLE <table_name> MODIFY <column_name> VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Note:
It's recommended to change the collation at the table level, as it automatically updates the collation of all columns within the table. Specific column alterations are only necessary in exceptional cases.
The above is the detailed content of How to Efficiently Change Database, Table, and Column Collations in MySQL?. For more information, please follow other related articles on the PHP Chinese website!