最好在MySQL中使用位或布尔值?

时间:2021-07-10 07:23:40

1) In my table I have a column that set privileges of the users.

在我的表中,我有一个列,它设置用户的权限。

I want have a flag 1/0 (yes/not,true/false and so on..) for my privileges (for example: if the user is admin or mod...).

我想要1/0 (yes/not,true/false等)作为我的特权(例如:如果用户是admin或mod…)。

I searched a lot, I'm still confused about the differences about boolean and bit in terms of resources requests to the DBMS. Which is better?

我搜索了很多,我仍然搞不清关于布尔值和比特值在资源请求到DBMS方面的区别。哪个更好?

I also found a lot of question of pasted years, so I'd like to have a fresh answer, in case that something it's changed/improved.

我也发现了很多关于粘贴年份的问题,所以我想有一个新的答案,以防它有所改变/改进。

2) Another question...

2)另一个问题……

I tryied to use both of these types and I saw that with boolean, it's easy to check if the value is true or false, but I haven't figured out how to see the value of a variable BIT. I'm my database coloumn I put values 1 or 0, but with a echo of the bit variable, nothing is shown.

我倾向于使用这两种类型,我在布尔函数中看到,它很容易检查值是真还是假,但我还没弄清楚如何查看变量位的值。我是我的数据库coloumn我将值设为1或0,但是有了位变量的回波后,什么都没有显示。

So, how can I see the value of a bit (I need to use only 1 or 0).

那么,如何才能看到位的值(我只需要使用1或0)。

Thank you in advice!

谢谢你的建议!

1 个解决方案

#1


3  

Use TINYINT(1). It's what's most commonly used for boolean values. Bear in mind though that it allows for values beyond 1 and 0 so for the sake of consistency I'd suggest using the keywords TRUE and FALSE when inserting data in it as they reflect 1 and 0.

使用非常小的整数(1)。它是最常用的布尔值。请记住,它允许超过1和0的值,因此为了一致性起见,我建议在插入数据时使用TRUE和FALSE,因为它们反映了1和0。

BOOL and BOOLEAN are just synonyms for TINYINT(1). BIT on the other hand used to be a synonym for TINYINT(1) before version 5.0.3.

BOOL和BOOLEAN就是TINYINT(1)的同义词。另一方面,BIT在5.0.3版之前曾是TINYINT(1)的同义词。

#1


3  

Use TINYINT(1). It's what's most commonly used for boolean values. Bear in mind though that it allows for values beyond 1 and 0 so for the sake of consistency I'd suggest using the keywords TRUE and FALSE when inserting data in it as they reflect 1 and 0.

使用非常小的整数(1)。它是最常用的布尔值。请记住,它允许超过1和0的值,因此为了一致性起见,我建议在插入数据时使用TRUE和FALSE,因为它们反映了1和0。

BOOL and BOOLEAN are just synonyms for TINYINT(1). BIT on the other hand used to be a synonym for TINYINT(1) before version 5.0.3.

BOOL和BOOLEAN就是TINYINT(1)的同义词。另一方面,BIT在5.0.3版之前曾是TINYINT(1)的同义词。