LoginSignup
1

More than 3 years have passed since last update.

MySQLのintの最大値を超えた場合の挙動はどうなるか?

Posted at

MySQLのintの最大値はunsignedでない場合「2147483647」ですが
Unixtimestampを使用しているとこの数値は
2038-01-19 3:14:07 になります。
これが俗に言う2038年問題ですね。(あと20年後か…)

ちなみにこの数値を越えるとMySQL的にはどうなるのかな?と思ったので試してみました。

「int_test」の定義がint(11)にしてあります。

mysql> select * from user_int_test limit 1;
+----+---------+------------+
| id | user_id | int_test | 
+----+---------+------------+
| 1  |    10   | 0 | 
+----+---------+------------+
mysql> update user_int_test set int_test = 2147483647 where user_id =10;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select * from user_int_test limit 1;
+----+---------+------------+
| id | user_id | int_test | 
+----+---------+------------+
| 1  |    10   | 2147483647 | 
+----+---------+------------+

最大値は問題なく入りますね。
次は閾値を1だけ超えた「2147483648」で試してみます。

mysql> update user_int_test set int_test = 2147483648 where user_id =10;
Query OK, 0 rows affected, 1 warning (0.00 sec)

クエリ的にはエラーになることもなく正常に完了しました。
値は、、

mysql> select * from user_int_test limit 1;
+----+---------+------------+
| id | user_id | int_test | 
+----+---------+------------+
| 1  |    10   | 2147483647 | 
+----+---------+------------+

変わっていない!

ということでmysql側に自動で切り捨てられるようですね。
エラーになるべきではと言う考え方もあるかもしれませんが
その都度状況によって変わると思います。

余談ですが、私がずっと続けているパズドラは攻撃力が最近とくにインフレを起こしてきていますがデータ型は何で定義してあるのでしょうね。

ゲームやユーザの規模が桁違いのシステムなどはデータ型のMAX値についても特に意識して実装いきたいですね。

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
What you can do with signing up
1