• mysql 字符集研究


     一、创建一个测试数据库 及一个测试用的表。均使用默认的编码方式。

       show variables like 'char%':

    mysql> show variables like 'char%';
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | latin1                     |
    | character_set_connection | latin1                     |
    | character_set_database   | latin1                     |
    | character_set_filesystem | binary                     |
    | character_set_results    | latin1                     |
    | character_set_server     | latin1                     |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
    8 rows in set (0.00 sec)

        create database dbtest

      create table utf8Latin1Table(column_utf8 varchar(30),column_latin1 varchar(30));

      查看库及表的编码方式:均为 latin1 

    mysql> show create database dbtest;
    +----------+-------------------------------------------------------------------+
    | Database | Create Database                                                   |
    +----------+-------------------------------------------------------------------+
    | dbtest   | CREATE DATABASE `dbtest` /*!40100 DEFAULT CHARACTER SET latin1 */ |
    +----------+-------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> show create table test;
    +-------+---------------------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table                                                                                                                    |
    +-------+---------------------------------------------------------------------------------------------------------------------------------+
    | test  | CREATE TABLE `test` (
      `id` varchar(10) DEFAULT NULL,
      `name` varchar(30) DEFAULT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
    +-------+---------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)

    查看表中字段的编码:

    mysql> show full columns from utf8Latin1Table;
    +---------------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
    | Field         | Type        | Collation         | Null | Key | Default | Extra | Privileges                      | Comment |
    +---------------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
    | column_utf8   | varchar(30) | latin1_swedish_ci | YES  |     | NULL    |       | select,insert,update,references |         |
    | column_latin1 | varchar(30) | latin1_swedish_ci | YES  |     | NULL    |       | select,insert,update,references |         |
    +---------------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
    2 rows in set (0.00 sec)
    

     修改column_utf8 的编码方式 :由latin1 ------ utf8 形式

      alter table utf8Latin1Table change column_utf8 column_utf8 varchar(30) character set utf8; 

     修改完字段的字符集后: 

    mysql> show full columns from utf8Latin1Table;
    +---------------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
    | Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
    +---------------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+
    | column_utf8 | varchar(30) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
    | column_latin1 | varchar(30) | latin1_swedish_ci | YES | | NULL | | select,insert,update,references | |
    +---------------+-------------+-------------------+------+-----+---------+-------+---------------------------------+---------+

    二、 向数据库中存入测试数据。(数据库连接工具是 UTF8 编码方式)

       测试 数据为 带重音的 e :  é

      é 在 latin1中的编码为:E9   在utf8字符集中编码为:C3A9

    mysql> insert into utf8Latin1Table values('é','é');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from utf8Latin1Table;
    +-------------+---------------+
    | column_utf8 | column_latin1 |
    +-------------+---------------+
    | é          | é            |
    +-------------+---------------+
    1 row in set (0.00 sec)

    查看存储的二进制内容:

    mysql> select hex(column_utf8),column_utf8,hex(column_latin1),column_latin1  from utf8Latin1Table;
    +------------------+-------------+--------------------+---------------+
    | hex(column_utf8) | column_utf8 | hex(column_latin1) | column_latin1 |
    +------------------+-------------+--------------------+---------------+
    | C383C2A9         | é          | C3A9               | é            |
    +------------------+-------------+--------------------+---------------+
    1 row in set (0.00 sec)

    虽然存入的是一样的字符,但是存入的二进制信息不同。

    三、分析Insert过程 、select 过程(未写)

    涉及应用程序段字符编码(数据库查询工具,网页端等等),character_set_client ,character_set_connection,character_set_results ,及数据库编码(列编码)

        insert 过程:

     

  • 相关阅读:
    HP LoadRunner11.0下载地址(官网地址)
    出现500错误[code=CANT_CONNECT_LOOPBACK] Cannot connect due to potential loopback problems的解决方法
    一个数据库的所见即所得的好工具
    强制释放windows被占用的端口
    测试管理工具QC第二篇QC安装步骤(史上最详细的图解过程)第二篇server2003的环境设置
    NAT连接虚拟机和主机的通信(静态IP配置完整图解,测试通过可用)附vmware tools的安装(未完待续)第一篇
    QC插件大集合
    winmail搭建自己的邮件服务器第二篇(详细图解,测试通过)
    QTP基本脚本设计(第一部分)
    winmail搭建自己的邮件服务器第一篇(附详细图解测试通过可用)
  • 原文地址:https://www.cnblogs.com/igoogleyou/p/mysqlcharset.html
Copyright © 2020-2023  润新知