LoginSignup
8
8

More than 5 years have passed since last update.

mysql本家ドキュメントから文字コードについて学ぶ

Posted at

はじめに

mysqlのDocumentation読んでますか?私はどうも読みづらくて、ずっと敬遠してました。しかし、何だかんだでオリジナルのドキュメントを読むのが一番早いですよね。今回、文字コードについて色々調べてみて、本家のドキュメントの解説が一番分かりやすかったので、大事だと思われる部分をかいつまんでみました。特に断りが無い場合は、本家ドキュメントの引用とそのカジュアルな意訳です。

出来るだけ読みやすく正確に翻訳したつもりですが、もし分かりづらかったり意味が間違っていたら、ご指摘頂ければ有難いです。特に専門用語の日本語訳はスタンダードなものがあると思いますが、それを調べるような手間はかけていません。今まで慣れ親しんだ用語を使っています。

なお、見出しに書かれている文字はオリジナル文章のセクション番号です。

参考にしたページ
http://dev.mysql.com/doc/refman/5.6/en/charset-general.html
http://dev.mysql.com/doc/refman/5.6/en/charset-mysql.html

10.1.1 character-set

Suppose that we have an alphabet with four letters: “A”, “B”, “a”, “b”. We give each letter a number: “A” = 0, “B” = 1, “a” = 2, “b” = 3. The letter “A” is a symbol, the number 0 is the encoding for “A”, and the combination of all four letters and their encodings is a character set.

いま"A","B","a","b"の四文字があるとします。このそれぞれの文字に番号を与えましょう。

A=0, B=1,a=2,b=3

ここで"A"はシンボル(symbol)で、数字の"0"はAのエンコーディングです。そしてこの全ての文字とエンコーディングの組み合わせがcharacter-setです。

10.1.1 collation

Suppose that we want to compare two string values, “A” and “B”. The simplest way to do this is to look at the encodings: 0 for “A” and 1 for “B”. Because 0 is less than 1, we say “A” is less than “B”. What we've just done is apply a collation to our character set. The collation is a set of rules (only one rule in this case): compare the encodings.

さて、この二つの文字"A"と"B"を比較するとしましょう。もっとも単純な方法は、このエンコーディングを比較する事です。Aは0,Bは1でしたね。0は1より小さいので、AはBより小さいと言えます。ここで行ったのが照合(collation)です。この場合は一つのルール(エンコーディングの比較)ですが、collationはこのようなルールのセットです。

10.1.2 character-setとcollationの見方

 mysql> SHOW CHARACTER SET;
+----------+-----------------------------+---------------------+--------+
| Charset  | Description                 | Default collation   | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |
| dec8     | DEC West European           | dec8_swedish_ci     |      1 |
| cp850    | DOS West European           | cp850_general_ci    |      1 |
| hp8      | HP West European            | hp8_english_ci      |      1 |
| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |
mysql> SHOW COLLATION LIKE 'latin1%';
+---------------------+---------+----+---------+----------+---------+
| Collation           | Charset | Id | Default | Compiled | Sortlen |
+---------------------+---------+----+---------+----------+---------+
| latin1_german1_ci   | latin1  |  5 |         |          |       0 |
| latin1_swedish_ci   | latin1  |  8 | Yes     | Yes      |       1 |
| latin1_danish_ci    | latin1  | 15 |         |          |       0 |
| latin1_german2_ci   | latin1  | 31 |         | Yes      |       2 |
| latin1_bin          | latin1  | 47 |         | Yes      |       1 |
| latin1_general_ci   | latin1  | 48 |         |          |       0 |
| latin1_general_cs   | latin1  | 49 |         |          |       0 |
| latin1_spanish_ci   | latin1  | 94 |         |          |       0 |
+---------------------+---------+----+---------+----------+---------+

解説
ここで分かるのは、一つのCharacter-setに対して複数のCollationがあることです。また、それぞれのCharacter-setにデフォルトのCollationも定義されています。つまり、文字エンコーディングと、その比較を分離しています。つまり文字化けだけを気にするのなら、collationは深く考えなくてもいいという事ですね。

10.1.3 character-setとcollationの設定

There are default settings for character sets and collations at four levels: server, database, table, and column. The description in the following sections may appear complex, but it has been found in practice that multiple-level defaulting leads to natural and obvious results.

Character-setとCollationのデフォルト設定には4レベルあります。サーバー、データベース、テーブル、そしてカラムです。このような定義は複雑化もしれませんが、今までの実例から、この方法が最も自然で確実なことが分かっています。

10.1.3.1 サーバー

MySQL Server has a server character set and a server collation. These can be set at server startup on the command line or in an option file and changed at runtime.

MysqlではサーバーCharacter-setとサーバーCollationを設定できます。それらはサーバーのスタートアップ、コマンドライン、もしくはオプションファイルで設定できます。

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

サーバーCharacter-setとサーバーCollationは、CREATE DATABASEのデフォルトして利用されます。それ以外の用途はありません。

10.1.3.2 データベース

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:

全てのデータベースは、データベースCharacter-setとデータベースCollationを持っています。CREATE DATABASEとALTER DATABASEでそれらを指定する事も可能です。

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]

The database character set and collation are used as default values for table definitions if the table character set and collation are not specified in CREATE TABLE statements. The database character set also is used by LOAD DATA INFILE. The character set and collation have no other purposes.

The character set and collation for the default database can be determined from the values of the character_set_database and collation_database system variables. The server sets these variables whenever the default database changes. If there is no default database, the variables have the same value as the corresponding server-level system variables, character_set_server and collation_server.

データベースCharacter-setとデータベースCollationはCREATE TABLE時のデフォルト定義として利用されます。それ以外の用途はありません。

10.1.3.3 テーブル

The table character set and collation are used as default values for column definitions if the column character set and collation are not specified in individual column definitions.

テーブルCharacter-setoとCollationはカラム定義のデフォルト値として使われます。

解説
本当にこんなまどろっこしい事をやる必要があるのか疑問が生じてきました、、、。

10.1.3.4 カラム

Every “character” column (that is, a column of type CHAR, VARCHAR, or TEXT) has a column character set and a column collation. Column definition syntax for CREATE TABLE and ALTER TABLE has optional clauses for specifying the column character set and collation:

全ての文字カラム(char,varchar,もしくはTEXT)はカラムcharacter-setとカラムcollationを持っています。CREATE TABLEとALTER TABLEで次のように指定出来ます。

CREATE TABLE t1
(
    col1 VARCHAR(5)
      CHARACTER SET latin1
      COLLATE latin1_german1_ci
);

ALTER TABLE t1 MODIFY
    col1 VARCHAR(5)
      CHARACTER SET latin1
      COLLATE latin1_swedish_ci;

解説
この後は同じような事をしつこく説明していてカジュアルでないので、かなり省略します。

CREATE TABLE t1
(
    col1 CHAR(10) CHARACTER SET utf8
) CHARACTER SET latin1 COLLATE latin1_bin;

The character set is specified for the column, but the collation is not. The column has character set utf8 and the default collation for utf8, which is utf8_general_ci. To see the default collation for each character set, use the SHOW COLLATION statement.

Character setがカラムに設定されていますが、collationはセットされていません。カラムのcharacter setはutf8が利用されて、collationにはutf8のデフォルト、すなわちutf8_general_ciが利用されます。それぞれのcharacterに対するデフォルトcollationを見るには、SHOW COLLATIONステートメントを使って下さい。

終わりに

いかがでしたか?個人的には今まで曖昧に理解していた事がすっきりしました。正直、原文は細かすぎて読んでて疲れますが、非常に詳しく説明してくれてるとも言えます。あと、大体が同じような構成で書かれているので、一部を理解すれば、他の部分も読みやすくなるのではと期待しています。

これ以外にも色々調べたのですが、それについては別途書きたいと思います。

8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8