豆メモ
MySQLのDATETIMEに小数秒設定したい
ついでいうと、デフォルトの値もちゃんと設定したい
6桁マイクロ秒
dt DATETIME(6) not null default current_timestamp(6) on update current_timestamp(6)
バージョンはMySQL5.7
mysql> CREATE TABLE t1 (id bigint unsigned NOT NULL, dt DATETIME(6) not null default current_timestamp(6) on update current_timestamp(6));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t1(id) values (1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1(id) values (2);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+----+----------------------------+
| id | dt |
+----+----------------------------+
| 1 | 2017-08-15 11:15:05.337048 |
| 2 | 2017-08-15 11:15:07.473981 |
+----+----------------------------+
2 rows in set (0.00 sec)
ちなみに、上記ミリ秒をJavaScriptに持ってくると
var d = new Date('2017-08-15 11:15:05.337048');
d
Tue Aug 15 2017 11:15:05 GMT+0900 (JST)