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 1tinyint
: storage size is 1 byte, possible values are 0-255Which 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?
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.
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.
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).
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With