MySQL 中的 True/False 与 0/1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10852337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:36:25  来源:igfitidea点击:

True/False vs 0/1 in MySQL

mysql

提问by SomeKittens

Which is fasterin a MySQL database? Booleans, or using zero and one to represent boolean values? My frontend just has a yes/no radio button.

在 MySQL 数据库中哪个更快?布尔值,还是使用零和一来表示布尔值?我的前端只有一个是/否单选按钮。

采纳答案by Marcus Adams

Some "front ends", with the "Use Booleans" option enabled, will treat all TINYINT(1) columns as Boolean, and vice versa.

某些启用了“使用布尔值”选项的“前端”会将所有 TINYINT(1) 列视为布尔值,反之亦然。

This allows you to, in the application, use TRUE and FALSE rather than 1 and 0.

这允许您在应用程序中使用 TRUE 和 FALSE 而不是 1 和 0。

This doesn't affect the database at all, since it's implemented in the application.

这根本不会影响数据库,因为它是在应用程序中实现的。

There is not really a BOOLEANtype in MySQL. BOOLEAN is just a synonym for TINYINT(1), and TRUE and FALSE are synonyms for 1 and 0.

BOOLEANMySQL 中并没有真正的类型。BOOLEAN 只是 TINYINT(1) 的同义词,TRUE 和 FALSE 是 1 和 0 的同义词。

If the conversion is done in the compiler, there will be no difference in performance in the application. Otherwise, the difference still won't be noticeable.

如果在编译器中进行转换,则应用程序的性能不会有任何差异。否则,差异仍然不会很明显。

You should use whichever method allows you to code more efficiently, though not using the feature may reduce dependency on that particular "front end" vendor.

您应该使用可以让您更有效地编码的任何方法,尽管不使用该功能可能会减少对特定“前端”供应商的依赖。

回答by Laurence

In MySQL TRUEand FALSEare synonymsfor TINYINT(1).

在MySQLTRUEFALSE同义词TINYINT(1)

So therefore its basically the same thing, but MySQL is converting to 0/1 - so just use a TINYINTif that's easier for you

因此,它基本上是一样的,但是 MySQL 正在转换为 0/1 - 所以只要使用一个TINYINT对你来说更容易的

P.S.
The performanceis likely to be so minuscule (if at all), that if you need to ask on StackOverflow, then it won't affect your database :)

PS
表现很可能是如此微不足道,(如果有的话),如果你要问在计算器上,那么它会不会影响你的数据库:)

回答by ZZ-bb

Bitis also an option if tinyintisn't to your liking. A few links:

Bit如果tinyint您不喜欢,这也是一个选择。几个链接:

Not surprisingly, more info about numeric types is available in the manual.

毫不奇怪,手册中提供有关数字类型的更多信息。

One more link: http://blog.mclaughlinsoftware.com/2010/02/26/mysql-boolean-data-type/

还有一个链接:http: //blog.mclaughlinsoftware.com/2010/02/26/mysql-boolean-data-type/

And a quote from the comment section of the article above:

以及上面文章评论部分的引用:

  • TINYINT(1) isn't a synonym for bit(1).
  • TINYINT(1) can store -9 to 9.
  • TINYINT(1) UNSIGNED: 0-9
  • BIT(1): 0, 1. (Bit, literally).
  • TINYINT(1) 不是 bit(1) 的同义词。
  • TINYINT(1) 可以存储 -9 到 9。
  • TINYINT(1) 无符号:0-9
  • BIT(1): 0, 1.(位,字面意思)。


Edit: This edit (and answer) is only remotely related to the original question...

编辑:此编辑(和答案)仅与原始问题有很大关系...

Additional quotes by Justin Rovang and the author maclochlainn (comment section of the linked article).

Justin Rovang 和作者 maclochlainn 的其他引用(链接文章的评论部分)。

Excuse me, seems I've fallen victim to substr-ism: TINYINT(1): -128-+127 TINYINT(1) UNSIGNED: 0-255 (Justin Rovang 25 Aug 11 at 4:32 pm)

True enough, but the post was about what PHPMyAdmin listed as a Boolean, and there it only uses 0 or 1 from the entire wide range of 256 possibilities. (maclochlainn 25 Aug 11 at 11:35 pm)

对不起,我似乎已经成为 substr-ism 的受害者:TINYINT(1):-128-+127 TINYINT(1) UNSIGNED:0-255(Justin Rovang 11 年 8 月 25 日下午 4:32)

确实如此,但该帖子是关于 PHPMyAdmin 列为布尔值的内容,在那里它只使用了 256 种可能性的整个范围内的 0 或 1。(maclochlainn 11 年 8 月 25 日晚上 11:35)

回答by Eduárd Moldován

If you are into performance, then it is worth using ENUM type. It will probably be faster on big tables, due to the better index performance.

如果您对性能感兴趣,那么值得使用 ENUM 类型。由于更好的索引性能,它在大表上可能会更快。

The way of using it (source: http://dev.mysql.com/doc/refman/5.5/en/enum.html):

使用方式(来源:http: //dev.mysql.com/doc/refman/5.5/en/enum.html):

CREATE TABLE shirts (
    name VARCHAR(40),
    size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
);

But, I always say that explaining the query like this:

但是,我总是说这样解释查询:

EXPLAIN SELECT * FROM shirts WHERE size='medium';

will tell you lots of information about your query and help on building a better table structure. For this end, it is usefull to let phpmyadmin Propose a table table structure - but this is more a long time optimisation possibility, when the table is already filled with lots of data.

将告诉您有关查询的大量信息,并有助于构建更好的表结构。为此,让 phpmyadmin 提出一个表表结构是很有用的——但这是一个更长时间的优化可能性,当表已经填满了大量数据时。