MySQL 5.0 字符集(1)--服务器端的字符集

自4.1以后,MySQL增强了对字符集的支持。

名词解释:
—————————————————–
字符集(character set)是一套符号和编码。
而校对(collation)是在该字符集下,用于比较字符的一个规则。
—————————————————–

对于数据库,有四种级别的编码和整理设定:
1. server
2. database
3. table
4. column

  SQL标准只定义了server和database级别的字符集,MySQL在此基础上,增加了对table和column字符集的支持。下面是相应的语法:

1. server
————–
–character-set-server
–collation-server

编译时指定服务器编码
shell> ./configure –with-charset=latin1

shell> ./configure –with-charset=latin1 \
–with-collation=latin1_german1_ci

2. 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]

eg: CREATE DATABASE db_name CHARACTER SET latin1 COLLATE latin1_swedish_ci;

3. table
————–
CREATE TABLE tbl_name (column_list)
[[DEFAULT] CHARACTER SET charset_name] [COLLATE collation_name]]

ALTER TABLE tbl_name
[[DEFAULT] CHARACTER SET charset_name] [COLLATE collation_name]

eg: CREATE TABLE t1 ( … ) CHARACTER SET latin1 COLLATE latin1_danish_ci;

* The table character set and collation are MySQL extensions; there are no such things in standard SQL.

4. column (only available for CHAR, VARCHAR and TEXT)
————–
col_name {CHAR | VARCHAR | TEXT} (col_length)
[CHARACTER SET charset_name] [COLLATE collation_name]

eg:

CREATE TABLE Table1
(
column1 VARCHAR(5) CHARACTER SET latin1 COLLATE latin1_german1_ci
);

发表评论

邮箱地址不会被公开。 必填项已用*标注