my_table
contains the enabled
field which is defined as: enabled BIT NOT NULL DEFAULT 0
.
This table has multiple rows with enabled = b'0'
, and multiple rows with enabled = b'1'
.
However, both this:
SELECT * from my_table WHERE enabled = b'0';
and this:
SELECT * from my_table WHERE enabled = b'1';
show blank in the enabled
column:
+----+---------+ | id | enabled | +----+---------+ | 1 | | | 2 | | +----+---------+
Why is that? How could I see the value of the enabled
field?
$ mysql --version mysql Ver 14.14 Distrib 5.1.63, for debian-linux-gnu (x86_64) using readline 6.1
A simple method to toggle a bit column (MySQL, SQL Server, and others) As you can see, the XOR operator returns “0” if the two arguments match and “1” if they don't. This makes it easy to flip the bit in a boolean column without having to first check it's existing value.
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).
The BIT data type is used to store bit values. A type of BIT( M ) enables storage of M -bit values. M can range from 1 to 64. To specify bit values, b' value ' notation can be used. value is a binary value written using zeros and ones.
BIT is a data type used in MySQL that allows us to store bit values. The bit value comes in a range of 1-64. It will store values only in 0 and 1. If we store a bit value like 2, it will return an error message.
The reason why you can't see it in terminal is because bit values are non printable characters.
Lets insert following values:
INSERT INTO `my_table` (`ID`, `enabled`) VALUES (1,b'1'),(2,b'0');
Then select them to file:
mysql> SELECT * FROM my_table INTO OUTFILE '/tmp/my_table.txt' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
First lets view our /tmp/my_table.txt
file as plain text:
"1"," "
"2"," "
and then in hex view:
22 31 22 2C 22 01 22 0A 22 32 22 2C 22 00 22 0A
To be able to see those values you can simply CAST
them in SELECT
:
SELECT id, CAST(enabled AS UNSIGNED) AS enabled FROM my_table
And that will produce the following output:
+----+---------+ | id | enabled | +----+---------+ | 1 | 1 | | 2 | 0 | +----+---------+ 2 rows in set (0.00 sec)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With