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

空扰寡人 提交于 2019-12-20 09:49:28

问题


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

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.


回答1:


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

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

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

You can confirm this using the SHOW CREATE TABLE command.




回答2:


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

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.



来源:https://stackoverflow.com/questions/2136126/creating-mysql-table-with-explicit-default-character-set-what-if-i-dont

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!