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

STRICT_TRANS_TABLESでのゼロ日付確認。

Posted at

db tech showcaseでもチラッと話しに出ていたんですが、sql_mode = STRICT_TRANS_TABLES の場合に5.6と5.7.5以降で異なるという問題が解決されたということらしい。
全く同じ挙動に戻すのか、デフォルト値を変えるのか、互換性オプションを付けるのかはわかりませんが、5.6.25と5.7.7DMRで確認。
sql_modeは

+--------------------------------------------+
| @@sql_mode                                 |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+

でそろえました。

Timestamp型

以下の3つを確認しました。

CREATE TABLE `mytable` ( 
`id` int(10) unsigned NOT NULL, 
`field2` tinyint(3) unsigned NOT NULL, 
`last_change` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
);
CREATE TABLE `mytable` ( 
`id` int(10) unsigned NOT NULL, 
`field2` tinyint(3) unsigned NOT NULL, 
`last_change` timestamp NOT NULL DEFAULT '1970-01-01 09:00:00'
);
CREATE TABLE `mytable` ( 
`id` int(10) unsigned NOT NULL, 
`field2` tinyint(3) unsigned NOT NULL, 
`last_change` timestamp NOT NULL DEFAULT '1970-01-01 09:00:01'
);
結果
5.6.25 5.7.7DMR
'0000-00-00 00:00:00' OK NG
'1970-01-01 09:00:00' NG NG
'1970-01-01 09:00:01' OK OK
 

Date型

以下の3つを確認しました。
CREATE TABLE `mytable2` ( 
`id` int(10) unsigned NOT NULL, 
`field2` tinyint(3) unsigned NOT NULL, 
`last_change_dt` date NOT NULL DEFAULT '0000-00-00'
);
CREATE TABLE `mytable2` ( 
`id` int(10) unsigned NOT NULL, 
`field2` tinyint(3) unsigned NOT NULL, 
`last_change_dt` date NOT NULL DEFAULT '0000-00-01'
);
CREATE TABLE `mytable2` ( 
`id` int(10) unsigned NOT NULL, 
`field2` tinyint(3) unsigned NOT NULL, 
`last_change_dt` date NOT NULL DEFAULT '0000-01-01'
);
結果
5.6.25 5.7.7DMR
'0000-00-00' OK NG
'0000-00-01' OK NG
'0000-01-01' OK OK
完全なるゼロ日付は、MySQL独自だけど0月1日を通すのか。 jdbcURLのクエリに"zeroDateTimeBehavior=convertToNull"を付けても、おそらくNullには変換されないだろうから、Javaではおかしくなりそう。
0
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
0
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?