以下をセットし、INSERTのときにcreatedにnullを指定すると現在の時間が保存される。
created timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
カラムを作成
create テーブル名 (id int auto_increment not null primary key, name varchar(255), created timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);
データを入れてチェック
INSERT INTO テーブル名 (name, created) VALUES('田中太郎', null);
結果
id | name | created | modified |
---|---|---|---|
1 | 田中太郎 | 2015-07-15 21:19:48 | 2015-07-15 21:19:48 |