V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
rebeccaMyKid
V2EX  ›  问与答

数据库和表的 charset 分别是起什么作用的?为什么我能 insert 中文到 charset 是 latin1 的库和表?

  •  
  •   rebeccaMyKid · 2020-07-30 16:50:03 +08:00 · 846 次点击
    这是一个创建于 1575 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如果 charset 不是对 insert 进行限制的,那是用在什么地方的?

    测试过程如下:

    mysql server 版本:
    +-------------------------+---------------------+
    | Variable_name           | Value               |
    +-------------------------+---------------------+
    | protocol_version        | 10                  |
    | version                 | 5.1.73              |
    | version_comment         | Source distribution |
    | version_compile_machine | x86_64              |
    | version_compile_os      | redhat-linux-gnu    |
    +-------------------------+---------------------+
    
    
    连接信息
    
    mysql> SHOW SESSION VARIABLES LIKE 'character\_set\_%';
    +--------------------------+--------+
    | 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   |
    +--------------------------+--------+
    7 rows in set (0.00 sec)
    
    mysql> SHOW SESSION VARIABLES LIKE 'collation\_%';
    +----------------------+-------------------+
    | Variable_name        | Value             |
    +----------------------+-------------------+
    | collation_connection | latin1_swedish_ci |
    | collation_database   | latin1_swedish_ci |
    | collation_server     | latin1_swedish_ci |
    +----------------------+-------------------+
    3 rows in set (0.00 sec)
    
    测试的数据库和表信息:
    
    mysql> show create database sunny_test;
    +------------+-----------------------------------------------------------------------+
    | Database   | Create Database                                                       |
    +------------+-----------------------------------------------------------------------+
    | sunny_test | CREATE DATABASE `sunny_test` /*!40100 DEFAULT CHARACTER SET latin1 */ |
    +------------+-----------------------------------------------------------------------+
    
    mysql> show create table pet;
    +-------+------------------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table                                                                                                                 |
    +-------+------------------------------------------------------------------------------------------------------------------------------+
    | pet   | CREATE TABLE `pet` (
      `name` varchar(20) NOT NULL DEFAULT '',
      PRIMARY KEY (`name`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
    +-------+------------------------------------------------------------------------------------------------------------------------------+
     
    mysql> insert into pet values ("你好");
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from pet;
    +--------+
    | name   |
    +--------+
    | 你好 |
    +--------+
    1 row in set (0.00 sec)
    
    
    mysql> insert into pet values ("♥");
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from pet;
    +--------+
    | name   |
    +--------+
    | ♥    |
    | 你好 |
    +--------+
    2 rows in set (0.00 sec)
    

    救救孩子

    1 条回复    2020-07-30 16:54:47 +08:00
    rebeccaMyKid
        1
    rebeccaMyKid  
    OP
       2020-07-30 16:54:47 +08:00
    这 markdown 的代码自动高亮怎么取消啊。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   965 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 21:32 · PVG 05:32 · LAX 13:32 · JFK 16:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.