为什么TINYINT(1)作为布尔函数,但INT(1)不? [英] Why does TINYINT(1) function as a boolean but INT(1) does not?

查看:296
本文介绍了为什么TINYINT(1)作为布尔函数,但INT(1)不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 TINYINT(1)工作为布尔值?我理解的官方文档,(1)应该意味着它的显示宽度为1,所以如果我存储 56 在它,我认为它应该打印 5 。但由于某种原因,它总是打印 1 0



另一种情况是如果我在 INT(1)中存储 56 $ c> 56 (至少根据SQLFiddle)。这里发生了什么?

解决方案

MySQL整数类型的括号中的(1)与值的范围无关由数据类型接受,或如何存储它。它只用于显示。



另请参阅我的回答 MySQL中的类型:BigInt(20)vs Int(20)等



TINYINT与TINYINT TINYINT(2)或TINYINT(64)。它是一个8位有符号整数数据类型,它接受从-128到127的任何8位整数值。

  mysql> ;创建表b(i tinyint(1)); 

mysql>插入b值(42);

mysql> select * from b;
+ ------ +
| i |
+ ------ +
| 42 |
+ ------ +



为方便起见,MySQL支持BOOL别名,它立即被TINYINT(1)替换。

  mysql>创建表b2(i bool); 

mysql> show create table b2;

CREATE TABLE`b2`(
`i` tinyint(1)DEFAULT NULL
)ENGINE = InnoDB DEFAULT CHARSET = latin1
正如我所说,使用(1)几乎没有意义,它只是一个约定,所以如果你看到TINYINT(1),它是合理的假设列是用作布尔值。



如果你希望列只接受 0或1,你可以使用BIT(1):

  mysql>创建表b3(i位(1)); 

mysql>插入b3值(0),(1);
查询OK,2行受影响(0.00秒)
记录:2重复:0警告:0

mysql> insert into b3 values(-1);
ERROR 1406(22001):第1行的列'i'的数据太长

mysql> insert into b3 values(2);
ERROR 1406(22001):第1行的列'i'的数据太长

这不会保存任何空间相比,TINYINT虽然,因为给定列的存储四舍五入到最近的字节。



PS:尽管从@ samdy1答复,TINYINT不存储字符串 '0''1' em>整数 0 1 ,以及从-128到127的其他整数。不需要在SQL中引用整数,我经常困惑为什么这么多的开发人员。


Why does TINYINT(1) work as a boolean? The way I understood the official docs, the (1) should mean it has a display width of 1, so if I store 56 in it, I thought it should print 5. But for some reason it always prints either 1 or 0.

And another case is if I store 56 in an INT(1), then it prints 56 (at least according to SQLFiddle). What's going on here?

解决方案

The (1) in parentheses for a MySQL integer type has nothing to do with the range of values accepted by the data type, or how it is stored. It's only for display.

See also my answer to Types in MySQL: BigInt(20) vs Int(20) etc.

TINYINT is no different from TINYINT(1) or TINYINT(2) or TINYINT(64). It's an 8-bit signed integer data type, and it accepts any 8-bit integer value from -128 to 127.

mysql> create table b (i tinyint(1));

mysql> insert into b values (42);

mysql> select * from b;
+------+
| i    |
+------+
|   42 |
+------+

For convenience, MySQL supports an alias for BOOL, which is replaced immediately by TINYINT(1).

mysql> create table b2 (i bool);

mysql> show create table b2;

CREATE TABLE `b2` (
  `i` tinyint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

As I said, the use of (1) means almost nothing, it's only a convention so that if you see TINYINT(1) it's reasonable to assume the column is intended to be used as a boolean. But nothing in MySQL prevents you from storing other integer values in it.

If you want a column to accept only 0 or 1, you can use BIT(1):

mysql> create table b3 (i bit(1));

mysql> insert into b3 values (0), (1);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> insert into b3 values (-1);
ERROR 1406 (22001): Data too long for column 'i' at row 1

mysql> insert into b3 values (2);
ERROR 1406 (22001): Data too long for column 'i' at row 1

This doesn't save any space compared to TINYINT though, because the storage for a given column rounds up to the nearest byte.

PS: Despite answer from @samdy1, TINYINT does not store strings '0' or '1' at all, it stores integers 0 or 1, as well as other integers from -128 to 127. There is no need to quote integers in SQL, and I am often puzzled why so many developers do.

这篇关于为什么TINYINT(1)作为布尔函数,但INT(1)不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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