英文:
MySQL: varchar or nvarchar in utf8mb4?
问题
以下是您要求的翻译内容:
根据这个文档(https://dev.mysql.com/doc/refman/8.0/en/charset-national.html) 所述,在MySQL中,以下数据类型声明是等效的:
CHAR(10) CHARACTER SET utf8
NATIONAL CHARACTER(10)
NCHAR(10)
在手动指定字符集的情况下,是否会模糊 char 和 nchar 在关键功能方面的区别(比如保存Unicode字符)?
换句话说,CHAR(10) CHARACTER SET utf8
和 NCHAR(10) CHARACTER SET utf8
之间有什么区别?
可以理解为,只要MySQL字符集已设置为utf8mb4
,那么使用国家字符类型或非国家字符类型将是等效的(或至少不会有关)?
英文:
As this doc(https://dev.mysql.com/doc/refman/8.0/en/charset-national.html) said, these data type declarations are equivalent in MySQL:
CHAR(10) CHARACTER SET utf8
NATIONAL CHARACTER(10)
NCHAR(10)
Does manually specifying a charset blur the difference between char and nchar in key functionalities (such as saving Unicode characters)?
In other words, what is the difference between CHAR(10) CHARACTER SET utf8
and NCHAR(10) CHARACTER SET utf8
?
Can this be understood to mean that, as long as the MySQL character set is already set to utf8mb4
, then using either national or non-national column types will be equivalent (or at least not matter)?
答案1
得分: 1
在MySQL中,实际上没有真正的数据类型NCHAR或NVARCHAR。这些只是CHAR或VARCHAR的别名,带有系统定义的字符集。
示例:
CREATE TABLE test (
col1 CHAR(10) CHARACTER SET utf8,
col2 NATIONAL CHARACTER(10),
col3 NCHAR(10)
);
查看表创建时MySQL如何替换CHAR类型。
此外,由于MySQL 8.0已弃用utf8mb3,建议将NCHAR和NVARCHAR更改为使用utf8mb4。这个更改在2015年提出:链接。
英文:
There is literally no difference.
In fact, there's no real data type NCHAR or NVARCHAR in MySQL. These are just aliases for CHAR or VARCHAR with a system-defined character set.
Demo:
mysql> CREATE TABLE test (
col1 CHAR(10) CHARACTER SET utf8,
col2 NATIONAL CHARACTER(10),
col3 NCHAR(10)
);
mysql> SHOW CREATE TABLE test\G
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`col1` char(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
`col2` char(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL,
`col3` char(10) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
See how MySQL substituted the CHAR type as the table was created?
As a side issue, since MySQL 8.0 has deprecated utf8mb3, NCHAR and NVARCHAR should be changed to use utf8mb4 instead. This change was requested in 2015 https://bugs.mysql.com/bug.php?id=76529
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论