LoginSignup
154
104

More than 5 years have passed since last update.

Mysqlのboolean型について調べてみた

Last updated at Posted at 2015-08-28

はじめに

Mysqlでbooleanという型を指定してテーブルが作れるので、
実際どういう扱いになっているのか調べてみました。

テストしたMysql

ver. 5.1.73

boolean型でテーブルを作ってみる

-- こんなのを作ってみる
create table `bool_check` (
  `bool` boolean
);

mysql> create table `bool_check` (
    ->   `bool` boolean
    -> )
    -> ;
Query OK, 0 rows affected (0.06 sec)

出来たテーブルはどうなっているのか

mysql> show create table bool
    -> ;
+-------+------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                               |
+-------+------------------------------------------------------------------------------------------------------------+
| bool  | CREATE TABLE `bool` (
  `is_activate` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

booleanというのは、内部的には`tinyint(1)として扱われるようです。
で、何も指定しなければデフォルトで0(false)が入る様になっているみたいです。

早速データを入れてみる

いくつかデータを入れてみます

mysql> insert into bool_check values (true), (false), (0), (1), (100), (-100), (128), (10000)
    -> ;
Query OK, 8 rows affected, 2 warnings (0.05 sec)
Records: 8  Duplicates: 0  Warnings: 2

 
100とか、-100とか、128, 10000がどう扱われるのだろうか。。

mysql> select * from bool_check;
+------+
| bool |
+------+
|    1 |
|    0 |
|    0 |
|    1 |
|  100 |
| -100 |
|  127 |
|  127 |
+------+
8 rows in set (0.00 sec)

やはり・・tinyint型なので、-127 ~ 127までは入るみたいですね。
で、それ以上の数字は丸められると。

これはアプリケーションの方から変な値が入らないようにチェックした方がいいかもしれないですな。

selectしてみる

booleanの取得には、= true(false) か、 is true(false) の2種類がある。

まずは = true/falseを試して見る

mysql> select * from bool_check where bool = true;
+------+
| bool |
+------+
|    1 |
|    1 |
+------+
2 rows in set (0.00 sec)

mysql> select * from bool_check where bool = false;
+------+
| bool |
+------+
|    0 |
|    0 |
+------+
2 rows in set (0.01 sec)

なるなる。
insertした時に、ture,1で入れたものはtrueで一致。
false,0で入れたものはfalseで一致。

ココらへんは予想どおりっぽくて良さ気ですね。

is true/falseを試してみる

mysql> select * from bool_check where bool IS true;
+------+
| bool |
+------+
|    1 |
|    1 |
|  100 |
| -100 |
|  127 |
|  127 |
+------+
6 rows in set (0.00 sec)

mysql> select * from bool_check where bool IS false;
+------+
| bool |
+------+
|    0 |
|    0 |
+------+
2 rows in set (0.01 sec)

こっちはちょっと違った結果になったみたいです。
どうやら、下記のように認識されている。

false = 0
true = 0以外

まとめ

mysqlでboolean型を使う時は、内部的にはtinyint(1)として扱われる。

DDLでもbooleanとしないで、tinyint(1)と明示的に書いてあげても良いのかもしれない。

真偽値のチェックのは = true/falseを使う

もしDBに0/1以外の値が入ってしまってた場合、
=true/falseを使うと、=trueを使った時に想定外の値が取得出来てしまう可能性がある。

なので、=true/falseを使っていればこちらが想定している通りの値が取得出来る。

154
104
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
154
104