3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【mysql】int型に空文字を入れると・・・

Posted at

小ネタです

下記のようなテーブルに、空文字('')をINSERTするとどうなるでしょうか?
※名前とかは適当につけました。ご了承ください。

create table int_test(
id bigint(20) DEFAULT NULL,
test_id int(11) DEFAULT NULL,
name varchar(255) DEFAULT NULL);

私、最初はNULLが入ると思ってました。
だって、DEFAULT NULLだし。。。

ところが・・・

実際に空文字をINSERTしてみました。

mysql> insert into int_test (id, test_id, name)
     > values (1, 1, 'aaa'), ('', '', '');
Query OK, 2 rows affected, 2 warnings (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 2

mysql> select * from int_test;
+------+---------+------+
| id   | test_id | name |
+------+---------+------+
|    1 |       1 | aaa  |
|    0 |       0 |      |
+------+---------+------+
2 rows in set (0.00 sec)

なんと、0が入ってしまいました!
次から気をつけます。
バグを生んでしまったので懺悔のQiita投稿でした

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?