MySql: Tinyint (2) vs tinyint(1) - 有什么区别?

2021-11-20 00:00:00 mysql sqldatatypes

我知道 mysql 中的布尔值是 tinyint (1).

I knew boolean in mysql as tinyint (1).

今天我看到一个表定义了一个整数,比如 tinyint(2),还有其他的比如 int(4), int(6) ...

Today I see a table with defined an integer like tinyint(2), and also others like int(4), int(6) ...

integer 和 tinyint 类型的字段中的大小是什么意思?

What does the size means in field of type integer and tinyint ?

推荐答案

表示显示宽度

无论您使用 tinyint(1) 还是 tinyint(2),都没有任何区别.

Whether you use tinyint(1) or tinyint(2), it does not make any difference.

我总是使用 tinyint(1) 和 int(11),我使用了几个 mysql 客户端(navicat、sequel pro).

I always use tinyint(1) and int(11), I used several mysql clients (navicat, sequel pro).

它根本没有任何意义!我跑了一个测试,上面的客户端甚至命令行客户端似乎都忽略了这一点.

It does not mean anything AT ALL! I ran a test, all above clients or even the command-line client seems to ignore this.

但是,如果您使用 ZEROFILL 选项,显示宽度 是最重要的,例如您的表格有以下 2 列:

But, display width is most important if you are using ZEROFILL option, for example your table has following 2 columns:

A tinyint(2) zerofill

A tinyint(2) zerofill

B tinyint(4) zerofill

B tinyint(4) zerofill

两列的值都是 1,A 列的输出将是 010001 对于 B,如下面的截图所示:)

both columns has the value of 1, output for column A would be 01 and 0001 for B, as seen in screenshot below :)

相关文章