LoginSignup
3
2

More than 5 years have passed since last update.

SQLのDATETIME型について

Posted at

日時 (DATETIME) 型とは?

日時 (DATETIME) 型は、日付と時刻を格納します。

そして、MySQL5.6.5からDATETIME型で生成時刻と更新時刻を自動でセットすることができるようになりました。

作成時刻は、

DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP 

これで、

更新時間は

DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 

で宣言をする。

例文

 CREATE TABLE foo (
         `id` INT(11),
         `creation_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
         `modification_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
         PRIMARY KEY (id)
       );

これで、生成時刻と更新時刻を自動でセットすることができるようになります。
便利だな~。

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