Home > Database > Mysql Tutorial > body text

MySQL字符集继承关系验证

WBOY
Release: 2016-06-07 16:50:40
Original
940 people have browsed it

(1)创建数据库时不指定字符集会继承服务器字符集 Server characterset: utf8 Db characterset: latin1 mysqlgt; show g

(1)创建数据库时不指定字符集会继承服务器字符集
 Server characterset:  utf8
 Db    characterset:  latin1

mysql> show global variables like 'character_set_database';
 +------------------------+--------+
 | Variable_name          | Value  |
 +------------------------+--------+
 | character_set_database | latin1 |
 +------------------------+--------+
 1 row in set (0.00 sec)
 

mysql> create database mytest2;
 Query OK, 1 row affected (0.02 sec)
 

mysql> show create database mytest2;
 +----------+------------------------------------------------------------------+
 | Database | Create Database                                                  |
 +----------+------------------------------------------------------------------+
 | mytest2  | CREATE DATABASE `mytest2` /*!40100 DEFAULT CHARACTER SET utf8 */ |
 +----------+------------------------------------------------------------------+
 1 row in set (0.00 sec)
 

可见,创建数据库时,若没有指定字符集,那么会继承服务器的字符集,不受character_set_database值的影响。

--------------------------------------分割线 --------------------------------------

Ubuntu 14.04下安装MySQL

《MySQL权威指南(原书第2版)》清晰中文扫描版 PDF

Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL

Ubuntu 14.04下搭建MySQL主从服务器

Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群

Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb

MySQL-5.5.38通用二进制安装

--------------------------------------分割线 -------------------------------------- 

(2)表创建不指定字符集时会继承数据库字符集
 mysql> create database mytest1 charset=latin1;
 Query OK, 1 row affected (0.04 sec)
 

mysql> show create database mytest1;
 +----------+--------------------------------------------------------------------+
 | Database | Create Database                                                    |
 +----------+--------------------------------------------------------------------+
 | mytest1  | CREATE DATABASE `mytest1` /*!40100 DEFAULT CHARACTER SET latin1 */ |  ## 这个可以看到,数据库的字符集为latin1
 +----------+--------------------------------------------------------------------+
 1 row in set (0.00 sec)
 

mysql> use mytest1
 Database changed
 mysql> create table t(id int,name char(20));  ## 创建表t时没有指定字符集
 Query OK, 0 rows affected (0.13 sec)
 

mysql> show create table t\G
 *************************** 1. row ***************************
        Table: t
 Create Table: CREATE TABLE `t` (
  `id` int(11) DEFAULT NULL,
  `name` char(20) DEFAULT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1
 1 row in set (0.00 sec)
 

可见,创建表时没有指定字符集,,那么会继承数据库的字符集。
 

 

(3)创建表时,字段不指定字符集,则默认继承表的字符集
 mysql> show create database mytest1;
 +----------+--------------------------------------------------------------------+
 | Database | Create Database                                                    |
 +----------+--------------------------------------------------------------------+
 | mytest1  | CREATE DATABASE `mytest1` /*!40100 DEFAULT CHARACTER SET latin1 */ |
 +----------+--------------------------------------------------------------------+
 1 row in set (0.00 sec)
 

mysql> create table t3(id int,name char(20) character set utf8,text char(30));
 Query OK, 0 rows affected (0.15 sec)
 

mysql> show create table t3\G
 *************************** 1. row ***************************
        Table: t3
 Create Table: CREATE TABLE `t3` (
  `id` int(11) DEFAULT NULL,
  `name` char(20) CHARACTER SET utf8 DEFAULT NULL,
  `text` char(30) DEFAULT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1
 1 row in set (0.00 sec)
 

可见,如果表创建时,字段不指定字符集,字段的字符集就会继承表的字符集。

本文永久更新链接地址:

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!