MySQL的每个数据库目录中有一个文件db.opt,该文件主要 用来存储当前数据库的默认字符集和字符校验规则。
eg.default-character-set=latin1
default-collation=latin1_swedish_ci
该文件中存储的是创建数据库时默认的字符集和字符集校验规则,则该数据库在以后创建表时如果没有指定字符集和校验规则,则该表的这两个属性将去自这两个表。
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
转载于:https://blog.51cto.com/samyubw/282975