---
title: "Add the row insertion time to a MySQL table"
description: "Automatically add the insertion and update time in MySQL"
author: "Bartosz Mikulski"
author_bio: "Principal AI Engineer & MLOps Architect. I bridge the gap between \"it works in a notebook\" and \"it works for 200 million users.\""
author_url: https://mikulskibartosz.name
author_linkedin: https://www.linkedin.com/in/mikulskibartosz/
author_github: https://github.com/mikulskibartosz
canonical_url: https://mikulskibartosz.name/add-row-insertion-time-to-mysql-table
---

It is a good practice to track when the data was inserted into a database or modified. However, relying on the backend applications to set that information correctly is error-prone. That's why we can configure a MySQL table to automatically use the current timestamp as the default value and store that time as the row creation time.

We have to do that when the table is created (or alter an existing table):

```sql
create table table_name (
    ...
    created_ts   timestamp default current_timestamp
);
```

Similarly, we can track the table update time. In this case, we have to use the `on update` clause:

```sql
updated_ts   timestamp default current_timestamp on update current_timestamp
```

