LoginSignup
8
7

More than 5 years have passed since last update.

mysqlでcreatedとmodifiedを作成

Posted at

以下をセットし、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

参照

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