使用显式默认字符集创建 mysql 表,如果我不这样做怎么办?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2136126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:05:42  来源:igfitidea点击:

Creating mysql table with explicit default character set, what if I don't?

mysqlcharacter-encodingdefault

提问by DanInDC

In mysql 5.x Whats the difference if I do something like this:

在 mysql 5.x 中,如果我这样做有什么区别:

CREATE TABLE aTable (
    id                       BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    aNumber          bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;

with this:

有了这个:

CREATE TABLE aTable (
   id                       BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
   aNumber            bigint(20) DEFAULT NULL
) ENGINE=InnoDB CHARACTER SET=utf8;

Notice I am not specifying the character set as default in the first one. I couldn't find anything in the mysql docs.

请注意,我没有在第一个中将字符集指定为默认值。我在 mysql 文档中找不到任何内容。

回答by martin clayton

The word DEFAULTis optional there - so the two are equivalent, i.e. they set the default character set for the table.

这个词DEFAULT在那里是可选的——所以两者是等价的,即它们为表设置默认字符集。

See the MySQL documentation for CREATE TABLE. Here's the relevant bit:

请参阅CREATE TABLE的 MySQL 文档。这是相关的部分:

table_option:
    ENGINE [=] engine_name
  ... other options ...
  | [DEFAULT] CHARACTER SET [=] charset_name
  ... more options ...

You can confirm this using the SHOW CREATE TABLEcommand.

您可以使用SHOW CREATE TABLE命令确认这一点。

回答by code_burgar

There are 4 levels of default settings in MySQL: server, database, table, and column. Using lower level defaults, you can override higher lever defaults.

MySQL 中有 4 个级别的默认设置:服务器、数据库、表和列。使用较低级别的默认值,您可以覆盖较高级别的默认值。

If you alter a table that has default charset set to something other than what the database has set, the table default will override the db default.

如果您将默认字符集设置为不同于数据库设置的表,则表默认值将覆盖数据库默认值。