23
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQLでレコードを生成した時刻を、カラムの値(created_at)に入れる

Last updated at Posted at 2017-10-06
  • 任意のテーブルの__created_at__に、レコードの生成時刻を入れたい。
select * from users;
+----+------------+---------------------+
| id | name       | created_at          |
+----+------------+---------------------+
|  1 | kapibara   | 2017-10-06 07:25:42 |
+----+------------+---------------------+
1 row in set (0.00 sec)

■ 実施例

  • テーブルを作成
  • カラム(created_at)のデフォルト値:current_timestamp
create table users (
  id int,
  name text,
  created_at datetime default current_timestamp
);
  • レコードの挿入
insert into users (id, name) value (1, 'kapibara');

■ アップデートの時刻を入れたい場合 : on update current_timestamp

create table users (
  id int,
  name text,
  created_at datetime  default current_timestamp,
  updated_at timestamp default current_timestamp on update current_timestamp
);
23
14
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
23
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?