相关文章推荐
魁梧的大蒜  ·  Data type summary | ...·  1 月前    · 
追风的牛肉面  ·  bcprov-jdk16-1.46.jar ...·  10 月前    · 
慷慨的葡萄酒  ·  vue.js - SVG and ...·  1 年前    · 
笑点低的猴子  ·  java.util.zip.ZipExcep ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

You should use Bit because it can only be the two values you care about. Thus, TinyInt is wasted space. Siyual Aug 21, 2014 at 14:23 @ElliottFrisch But I thought you said it was synonyms for TINYINT(1) . Why would it be more efficient? user1032531 Aug 21, 2014 at 14:30

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:

Currently, I am using TINYINT which I believe defaults to TINYINT(256) . Guess it is easy enough to change to TINYINT(1) . Exactly the same as BIT(1) ? user1032531 Aug 21, 2014 at 14:29 If you ever need to use a join on the table, don't use bit. You can run into bugs. I'm using mysql version 5.6, and when joining two tables with the same names for the bit column, the results become unpredictable. Even when you output the same column twice with a single query with a join, you can get a different value each time. Bart Oct 14, 2015 at 8:50 hate to break it to everyone, but, tinyint(1) will store between -128 and +127. the 1 is nothing more than display width. brian Apr 27, 2016 at 3:40

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .