Saturday, July 14, 2007

Character Sets and Collations in MySQL

A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set.

The MySQL server can support multiple character sets. To list the available character sets, use the SHOW CHARACTER SET statement.
mysql> SHOW CHARACTER SET;
Any given character set always has at least one collation. It may have several collations. To list the collations for a character set, use the SHOW COLLATION statement.
mysql> SHOW COLLATION LIKE 'latin1%';

Every database has a database character set and a database collation. The CREATE DATABASE and ALTER DATABASE statements have optional clauses for specifying the database character set and collation:
CREATE DATABASE db_name
[[DEFAULT] CHARACTER SET charset_name]
[[DEFAULT] COLLATE collation_name]


ALTER DATABASE db_name
[[DEFAULT] CHARACTER SET charset_name]
[[DEFAULT] COLLATE collation_name]


Example:
CREATE DATABASE db_name CHARACTER SET latin1 COLLATE latin1_swedish_ci;

The database character set and collation are used as default values if the table character set and collation are not specified in CREATE TABLE statements. They have no other purpose.

In order to check teh character set and collation for the databases, tables and columns, you can go to the information_schema database, and check the table called SCHEMATA.
mysql> SELECT * FROM SCHEMATA;

No comments: