简体   繁体   English

MySQL将(num)添加到smallint列

[英]MySQL adding (num) to smallint column

When I create a table in MySQL specifying smallint as a column, but then use show create table or even mysqldump, MySQL has added (5) after the smallint definition, as below. 当我在MySQL中创建一个将smallint指定为列的表,然后使用show create table甚至mysqldump时,MySQL在smallint定义之后添加了(5),如下所示。

I'm guessing it doesn't really matter as far as the data is concerned, but can anyone explain why and if/how I can stop it doing this? 我想就数据而言,这实际上并不重要,但是任何人都可以解释为什么以及是否/如何阻止它这样做?

As an aside, I am attempting to change an existing database table to exactly match that of a new sql script. 顺便说一句,我试图更改现有的数据库表,使其与新的sql脚本的表完全匹配。 I could always alter the new sql script, but I'd prefer to alter the existing table if possible (think software install versus software upgrade). 我总是可以更改新的sql脚本,但是如果可能的话,我希望更改现有的表(考虑软件安装与软件升级)。

DROP TABLE IF EXISTS `test`;

CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `status` varchar(100) NOT NULL DEFAULT '',
  `port` smallint unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SHOW CREATE TABLE test;

CREATE TABLE `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `status` varchar(100) NOT NULL DEFAULT '',
  `port` smallint(5) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

No, you can't stop the SHOW CREATE TABLE from including the display width attribute for integer types. 不,您不能阻止SHOW CREATE TABLE包含整数类型的display width属性。

If a value for the display width is not included in the column declaration of an integer type, MySQL supplies a default value for it. 如果整数类型的列声明中未包含显示宽度的值,则MySQL将为其提供默认值。 A value of 5 is the default value for SMALLINT UNSIGNED . 5SMALLINT UNSIGNED的默认值。

The display width doesn't have any affect on the values that can be stored or retrieved. 显示宽度对可以存储或检索的值没有任何影响。 Client applications can make use of the value for formatting a resultset. 客户端应用程序可以利用该值来格式化结果集。

Reference: http://dev.mysql.com/doc/refman/5.6/en/numeric-type-attributes.html 参考: http : //dev.mysql.com/doc/refman/5.6/en/numeric-type-attributes.html

tMySQL is simply setting the (displayed) length of the column to match he data type (max value 65535, five digits). tMySQL只是设置(显示)列的长度以匹配数据类型(最大值65535,五位数字)。 To change this, you can write: 要更改此设置,您可以编写:

port smallint (3) unsigned NOT NULL DEFAULT '0', port smallint(3)无符号NOT NULL缺省'0',

if you like. 如果你喜欢。

Try this start adding values in your table. 尝试此操作开始在表中添加值。

<mysql> CREATE TABLE test(
->    ID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
->    Name VARCHAR(100) NOT NULL
-> );>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM