Tinyint vs Bit? [英] Tinyint vs Bit?

查看:90
本文介绍了Tinyint vs Bit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想在这里引发一场宗教战争,但是关于如何在数据库中表示布尔值似乎有两种思路.有人说bit是合适的数据类型,而其他人则说tinyint更好.

I don't want to touch-off a religious war here, but there seem to be two schools of thoughts in how to represent boolean values in a database. Some say bit is the appropriate data type, while others argue tinyint is better.

我知道的唯一区别是:

  • bit:存储大小为1位,可能的值为0或1
  • tinyint:存储大小为1个字节,可能的值为0-255
  • bit: storage size is 1 bit, possible values are 0 or 1
  • tinyint: storage size is 1 byte, possible values are 0-255

当您需要表示布尔值时,哪种数据类型更好? tinyint是否值得额外的开销以防万一,您需要将值设置为> 1?

Which data type is better when you need to represent boolean values? Is tinyint worth the extra overhead "just in case" you need to values > 1?

推荐答案

向表中添加位列时,它将在每个记录中占据整个字节,而不仅仅是单个位.当您添加第二个位列时,它将存储在同一字节中.第九位列将需要第二个字节的存储.具有1位列的表将不会获得任何存储好处.

When you add a bit column to your table it will occupy a whole byte in each record, not just a single bit. When you add a second bit column it will be stored in the same byte. The ninth bit column will require a second byte of storage. Tables with 1 bit column will not gain any storage benefit.

Tiintint和bit都可以使用,我已经成功使用了它们,并且没有强烈的偏好.

Tinyint and bit can both be made to work, I have used both successfully and have no strong preference.

这篇关于Tinyint vs Bit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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