MySQL 中标志的 BIT(1) 或 TINYINT [英] BIT(1) or TINYINT for flags in MySQL
问题描述
我经常需要在表中存储一个标志,该标志可以是 1 或 0(真或假等).
我以前使用过 TINYINT.
我应该改用 BIT(1) 吗?为什么或为什么不?
如果您使用 mysql 版本高于 5.0.3 Bit
不再是 Tinyint
但是如果你创建一个 bit
列,它无论如何都会得到 1 Byte
.
所以使用 Bit(1)
或 Tinyint(1)
是相等的,如果你的表只有 1 个 Bit
列,你不会得到任何好处.
但如果你有更多的真/假列,我建议你使用 Bit
因为位列的每个值都放在同一个 1 Byte
中,直到它被填满.
如果你使用低于 5.0.3 的 mysql,那么使用 tinyint
或 bit
完全没问题.如果您查看有关 bool
类型的 mysql 文档,您会发现它是 tinyint
http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
<块引用>布尔值,布尔值
这些类型是 TINYINT(1) 的同义词.零值被认为是错误的.非零值被认为是真的:
<小时><块引用>
BIT 是 TINYINT(1) 的同义词.
I often have tables where I need to store a flag which can either be 1 or 0 (true or false, etc).
I've previously used TINYINT.
Should I instead use BIT(1)? Why or why not?
if you use a mysql version greater then 5.0.3 Bit
isn't anymore an alias for Tinyint
but if you create a bit
column it gets anyway 1 Byte
.
so use Bit(1)
or Tinyint(1)
is equal and you get no benefits if your table had only 1 Bit
column.
but if you had more true/false columns i suggest you to use Bit
as each value of the bit columns are placed in the same 1 Byte
until it is filled.
if u use mysql lower then 5.0.3 then use tinyint
or bit
is totally fine. if you look at the mysql documentation on bool
types you see that it is a alias for tinyint
http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
BOOL, BOOLEAN
These types are synonyms for TINYINT(1). A value of zero is considered false. Nonzero values are considered true:
BIT is a synonym for TINYINT(1).
这篇关于MySQL 中标志的 BIT(1) 或 TINYINT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!