括号中的数字到底是什么意思? [英] What does the number in parenthesis really mean?

查看:176
本文介绍了括号中的数字到底是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直以为括号中的数字代表字段长度?但是,我了解并非总是如此.也许这是一个mysql问题?有人告诉我,如果我将一个字段设置为9个字符长,我可以添加一个超过9个字符的值,但是只会保存前9个字符.

I always thought that the number in the parenthesis represented the field lenght? However, I understand that is not always the case. Maybe it's a mysql issue? Someone told me if I set a field to 9 characters long, I can add a value that's more than 9 characters but only the first 9 will be saved.

示例...

create table "person" ( id INT, age INT(2));

如果是这种情况,我不应该选择TINYINT之类的东西代替INT作为年龄吗?

If that's the case, shouldn't I select something like TINYINT instead of INT for age?

推荐答案

INT(2)将生成一个

MySQL支持扩展,可以有选择地在类型的基本关键字之后的括号中指定整数数据类型的显示宽度.例如,INT(4)指定显示宽度为四位数的INT.应用程序可以使用此可选的显示宽度来显示整数值,该整数值的宽度小于为列指定的宽度,方法是用空格左键填充它们. (也就是说,此宽度存在于结果集返回的元数据中.是否使用该宽度取决于应用程序.)

MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)

显示宽度不限制可以存储在列中的值的范围.也不会阻止宽于列显示宽度的值正确显示.例如,指定为SMALLINT(3)的列通常具有-32768到32767的SMALLINT范围,并且使用三位以上的数字完整显示三位数字所允许的范围之外的值.

The display width does not constrain the range of values that can be stored in the column. Nor does it prevent values wider than the column display width from being displayed correctly. For example, a column specified as SMALLINT(3) has the usual SMALLINT range of -32768 to 32767, and values outside the range permitted by three digits are displayed in full using more than three digits.

这不会不会影响可以存储在字段中的可能值的范围;也不是用于存储它的字节数.除非使用ZEROFILL,否则似乎只是建议如何显示该值(请参见链接页面).

this does not affect the range of possible values that can be stored in the field; neither is it the number of bytes used to store it. It seems to be only a recommendation for applications how to show the value, unless ZEROFILL is used (see the linked page).

未签名的TINYINT(0 ... 255)也可能会这样做,除非 cryopreservation 在应用程序的生命周期中向前迈了一大步.

A unsigned TINYINT (0...255) would probably do as well, unless cryopreservation takes a big step forward during the lifetime of your application.

这篇关于括号中的数字到底是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆