LoginSignup
10
8

More than 5 years have passed since last update.

MySQL DATETIMEにミリ秒、マイクロ秒設定

Last updated at Posted at 2017-08-15

豆メモ

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)
10
8
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
10
8