以下の内容はhttps://takuya-1st.hatenablog.jp/entry/2016/05/09/150747より取得しました。


MySQL でCurrent TimeStampを複数カラムで使う小手先テクニック

current_timesptam 便利だけど。

複数カラムには設定できない。

 create table my_test
 (
  id int primary key  auto_increment, 
  path  text  not null default "" , locked tinyint not null default 0 , 
  created_at timestamp not null default current_timestamp,
  modified_at timestamp not null  default current_timestamp 
)
;
ERROR 1293 (HY000): Incorrect table definition;
there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

current_timestamp を複数カラムに設定するには

非常に面倒くさいテクニックがアリまして。

timestamp not null  のカラムに null を入れる。

not null に null を入れると current_timestap が導入される。なんともユーザーの期待を裏切る仕様

これでこうなる。

+----+----------------------------+--------+---------------------+---------------------+
| id | path                       | locked | created_at          | modified_at         |
+----+----------------------------+--------+---------------------+---------------------+
| 17 | /home/takuya/test.1.jpeg   |      1 | 2016-05-09 04:53:16 | 2016-05-09 04:53:54 |
| 18 | /home/takuya/test.1.jpg    |      0 | 2016-05-09 04:53:34 | 2016-05-09 04:53:34 |
+----+----------------------------+--------+---------------------+---------------------+

current_timestamp を複数カラムに設定するときの小手先

create table で not null 入れる

  created_at timestamp not null ,
  modified_at timestamp not null  on update current timestamp_default current_timestamp

null を指定する

insert into mytable ( path, created_at) values ( "/path/to/name", null );

not null テーブル・カラムに null 入れるなんて、コードレビューでRejectしてたんだが、まさかこんなことになってたとは




以上の内容はhttps://takuya-1st.hatenablog.jp/entry/2016/05/09/150747より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14