MySQL源码:
/* Set table default charset, if not set
SYNOPSIS
set_table_default_charset()
create_info Table create information
DESCRIPTION
If the table character set was not given explicitely,
let’s fetch the database default character set and
apply it to the table. */
static void set_table_default_charset(THD *thd,
HA_CREATE_INFO *create_info, char *db)
{
if (!create_info->default_table_charset)
{
HA_CREATE_INFO db_info;
load_db_opt_by_name(thd, db, &db_info);
create_info->default_table_charset= db_info.default_table_charset;
}
创建数据库时指定字符集和字符集校验规则:
create database if not exists test default [charset|character set ] utf8 default collate utf8_general_ci;
修改数据库的字符集和字符集校验规则:
alter database test default [charset|character set ] latin1 default collate latin1_swedish_ci;
创建数据库create database
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] …
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
也可以通过alter database修改
ALTER {DATABASE | SCHEMA} [db_name]
alter_specification …
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name