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

2020-07-30 16:50:03 +08:00
 rebeccaMyKid

如果 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)

救救孩子

846 次点击
所在节点    问与答
1 条回复
rebeccaMyKid
2020-07-30 16:54:47 +08:00
这 markdown 的代码自动高亮怎么取消啊。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/694384

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX