3
4

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 5 years have passed since last update.

MySQL5.6でINSERT/UPDATE時のエラーを無視する

Last updated at Posted at 2016-03-31

ずっと5.5環境で開発していて、5.6に移したら、

ERROR 1406 (22001): Data too long for column 'name' at row 1

などと、オーバーフローを知らせるエラーが。
調べてみると5.6から、Strict Modeの設定がされていて、チェックが文字通り厳格になったようである。

回避方法

その1:my.cnfをいじる

/etc/my.cnfを見てみると、

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

という記述がある。これを、

sql_mode=''

とすると無効になるらしい。MySQLの再起動を忘れずに。

その2:SQL文にIGNOREを入れる

条件が許せばこちらのほうが手軽か。

INSERT IGNORE INTO members(name) VALUES('hogehogehoge');

などとする。UPDATE IGNOREというのも使える。単に長さエラーだけでなく、その他の制約も無視できる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?