Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get MySQL command line tool to show booleans stored as BIT sensibly by default

I got a problem with selecting boolean types stored as BIT with MySQL. I know that I can get bit values shown in a sensible with with custom queries like with SELECT CAST(1=1 AS SIGNED INTEGER) or with SELECT BOOLFIELD + 0 ...

However, is there any way to get our booleans shown in a sensible way with command line client with queries like SELECT * FROM TABLE ?

UPDATE : At the moment I see only space in the results Example:

mysql> SELECT distinct foo, foo + 0 from table
+------+-------+
| foo  | foo_0 |
+------+-------+
|      |     0 |  <-- Only space
|     |     1 |   <-- Space, one space less
+------+-------+

With some googling, I found some (maybe related) bugs from MySQL bug DB (http://bugs.mysql.com/bug.php?id=28422, http://bugs.mysql.com/bug.php?id=43670) but not answer or fix?

like image 630
Touko Avatar asked Sep 25 '13 08:09

Touko


People also ask

How are Booleans stored in MySQL?

MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.

What is the default value for Boolean data type in SQL?

The default value of Boolean is False .

How do you set a boolean to true in MySQL?

You can update boolean value using UPDATE command. If you use the BOOLEAN data type, MySQL internally convert it into tinyint(1). It can takes true or false literal in which true indicates 1 to tinyint(1) and false indicates 0 to tinyint(1).

How do I select a boolean in MySQL?

MySQL does not contain built-in Boolean or Bool data type. They provide a TINYINT data type instead of Boolean or Bool data types. MySQL considered value zero as false and non-zero value as true. If you want to use Boolean literals, use true or false that always evaluates to 0 and 1 value.


1 Answers

To store booleans, one really ought to use MySQL's BOOLEAN type (which is an alias for TINYINT(1), given that MySQL doesn't have real boolean types): 0 represents false and non-zero represents true.

Whilst it might feel like storing a boolean in a byte is more wasteful than in a BIT(1) column, one must remember that a few saved bits will translate into more bit operations for the CPU on data storage & retrieval; and I'm unsure whether most storage engines pad BIT columns to the next byte boundary anyway.

If you insist on using BIT type columns, you should be aware that they are returned as binary strings. The MySQL command line client (stupidly) attempts to render binary strings as textual (by applying its default character set), which is what causes the behaviour that you observe—there's no way to avoid this (other than to manipulate the field in the select list in order that it as returned as something other than a binary string, as you are already doing).

However, if you also insist on using SELECT * (which is bad practice, albeit somewhat more understandable from the command line client), you might consider defining a view in which the manipulation is performed and then SELECT from that. For example:

CREATE VIEW my_view AS SELECT foo + 0 AS foo, bar FROM my_table;

Then one could do:

SELECT * FROM my_view WHERE foo = 1 AND bar = 'wibble';
like image 164
eggyal Avatar answered Oct 20 '22 02:10

eggyal