tinyint(size),varchar(size):"size"解释 [英] tinyint(size), varchar(size): "size" explaination

查看:124
本文介绍了tinyint(size),varchar(size):"size"解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道tinyint(1)和tinyint(2)具有相同的存储空间范围.

I understand that tinyint(1) and tinyint(2) have the same range of storage space.

唯一的区别是显示宽度会有所不同.这是否意味着tinyint(1)将存储所有类型的整数,但仅正确显示从0到9的范围?而且tinyint(2)只能正确显示0到99?

The only difference would be that the display width is differentiated. Does this mean that tinyint(1) will store all types of integers but only display the range from 0 to 9 correctly? And tinyint(2) displays only 0 to 99 correctly?

并且提到最大大小为tinyint(255).也有其他类似varchar(500)类型的宽度.除非存储论文,否则在什么情况下文本和整数都需要这么大的显示宽度?像密码加密之类的东西?

And it is mentioned that max size is tinyint(255). Also there are others like varchar(500) type of width. Unless an essay is being stored, under what circumstances would you need such a large display width for both text and integers? Like password encryption or something?

如果问题中所写的任何上述信息有误,请纠正我.

Do correct me if any of the above information written in the question is wrong.

添加一个问题:显示宽度仅适用于char,varchar等,不适用于整数类型.

Add on question: The display width only works for char, varchar etc but not for integer types.

但是,为什么要麻烦输入"M",因为对于整数来说,除了零填充之外,它对存储它没有任何影响?

However, why bother putting "M" since for integers it will not make a difference in storing it other than zerofill?

推荐答案

您的困惑解决方案是-

在Mysql中,int(6)并不意味着它们最多可以存储6位数字.不超过999999.

In Mysql , int(6) doesn't mean they can store up to 6 digit. not more than 999999.

但是CHAR(6) 这意味着一个字符列,最大长度为6个字符,例如:

However CHAR(6) This means a character column, with a maximum length of 6 characters, such as these words:

房屋

如果我尝试将"special"一词存储在该列中,则MySQL会将值切为"specia",因为原始单词有7个字符.

If I tried storing the word "special" in that column, MySQL would chop the value to be "specia", since the original word has 7 characters.

无论如何,所有整数列都具有允许的预设值范围.括号中的数字仅表示显示宽度.

Anyway, integer columns all have a preset range of values allowed. The number in parenthesis only indicates the display width.

这可能仍然令人困惑,所以让我进一步解释...

This is probably still confusing, so let me explain further…

显示宽度是1到255之间的一个数字.如果希望所有整数值都类似地出现",则可以设置显示宽度:

The display width is a number from 1 to 255. You can set the display width if you want all of your integer values to "appear" similarly:

TINYINT [(M)] [未签名] [零填充]

TINYINT[(M)] [UNSIGNED] [ZEROFILL]

M表示整数类型的最大显示宽度.最大显示宽度为255.显示宽度与类型可以包含的值的范围无关.对于浮点和定点类型,M是可以存储的总位数.

M indicates the maximum display width for integer types. The maximum display width is 255. Display width is unrelated to the range of values a type can contain, . For floating-point and fixed-point types, M is the total number of digits that can be stored.

如果为数字列指定ZEROFILL,MySQL会自动将UNSIGNED属性添加到该列.

If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.

所以您的问题的答案是

create table foo
(
col2 tinyint(2) unsigned zerofill, col4 tinyint(4) unsigned zerofill, col5notzerofill        tinyint(5)  
)engine=innodb;

insert into foo values (1,2,3),(11,12,14),(123,123,156),(1234,1234,12756);
select * from foo;

OUTPUT :-
+------+------+-----------------+
| col2 | col4 | col5notzerofill |
+------+------+-----------------+
|   01 | 0002 |               3 |
|   11 | 0012 |              14 |
|  123 | 0123 |             127 |
|  255 | 0255 |             127 |
+------+------+-----------------+

*因此请理解,在Mysql中,显示宽度仅适用于填零.

*So Understand, In Mysql display width only work with zerofill.

*如果您不使用填零功能,则无效.

*if you use do not use zerofill, it is not effective.

如果使用zerofill,则mysql将检查display widht,如果不分配,则mysql将自动为该列分配display-width. *

这篇关于tinyint(size),varchar(size):"size"解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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