Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tinyint vs Bit?

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.

The only differences I'm aware of are these:

  • bit: storage size is 1 bit, possible values are 0 or 1
  • tinyint: storage size is 1 byte, possible values are 0-255

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?

like image 330
Seibar Avatar asked Jan 28 '09 18:01

Seibar


People also ask

What is the difference between Tinyint and bit?

A TINYINT is an 8-bit integer value, a BIT field can store between 1 bit, BIT(1), and 64 bits, BIT(64). For a boolean values, BIT(1) is pretty common. what's the difference between a TINYINT and a BIT(8) ? TINYINT can be signed or unsigned and relate to negative numbers.

What is difference between bit and boolean?

A boolean is a true-or-false quantity, but a bit is actually an integer, just like char or int, but only one bit wide. When converting to these types, you can get different results. In the boolean world, 0 is false and anything else is true.

Is boolean a bit?

Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False. Boolean variables display as either: True or False (when Print is used), or. #TRUE# or #FALSE# (when Write # is used).

Is Tinyint a boolean?

There is no difference between TINYINT(1) and Boolean. The keyword Bool or Boolean internally converts into TINYINT(1) or we can say Bool or Boolean are synonymous with TINYINT(1).


1 Answers

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.

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

like image 156
ScottS Avatar answered Oct 04 '22 10:10

ScottS