What is the difference between BIT and TINYINT in MySQL?
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.
BIT(1) or TINYINT for flags in MySQL
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 TINYINT(1) versus BIT(1)
A TINYINT will always have a size of one (1) byte. And accept values between -128 and 127 (if signed).
The number you put in the brackets is for display purposes.
A BIT(1) on the other hand only take one bit in storage but needs to be aligned to whole bytes, meaning that if you only have one BIT(1) column, one byte is used, but if you have multiple they can be placed in the same byte.
MySQL: BOOLEAN (aka tinyint(1)) vs BIT
tinyint(1) is an integer type with a defined display width of 1. The BIT data type represents bit-field values which can have from 1 to 64 bits.
The storage size of tinyint is always 1 byte while the storage size of BIT(n) is approximately INT((n+7)/8) bytes
You can write to a BIT field using a special notation e.g. b'1111', don't think you can use this with INT/TINYINT fields
What is the difference between bit(1) and tinyint(1) in mysql
- BIT(m)
A bit-field type. M
indicates the number of bits per value, from 1 to 64. The default is 1 if M is omitted.
- TINYINT(m)
A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.
See Numeric Type Overview
SQL Server 2008: tinyint vs bit
The problem in 2000 was a bit couldn't be part of an index. Which is logical since a bit only has two states. It is a very poor field to have on an index. In 2005 you can create covered indexes, which allows you to specify extra pieces of data to store with the index. This allows you to avoid a bookmark lookup.
I would go with the bit.
Is BIT field faster than int field in SQL Server?
Officially bit will be fastest, especially if you don't allow nulls. In practice it may not matter, even at large usages. But if the value will only be 0 or 1, why not use a bit? Sounds like the the best way to ensure that the value won't get filled with invalid stuff, like 2 or -1.
Related Topics
Returning Month Name in SQL Server Query
SQL Get All Records Older Than 30 Days
Oracle: How to Get Percent of Total by a Query
What's the Escape Sequence for Hyphen (-) in Postgresql
How to Persist a Variable Across a Go
Table Scan and Index Scan in SQL
How to Copy Indexes from One Table to Another in SQL Server
Case in Statement with Multiple Values
Convert Access Transform/Pivot Query to SQL Server
Extract Day of Week from Date Field in Postgresql Assuming Weeks Start on Monday
How to Add a Not Null Column Without Default Value
How to Find Records That Are Not Joined
Why Can't I Use "Create Schema" in a Begin/End Block in SQL Management Studio
String or Binary Data Would Be Truncated. the Statement Has Been Terminated
Postgresql Copy/Transfer Data from One Database to Another
Do We Need to Specify "Not Null" for Primary Key? Oracle/Sql