Add the row insertion time to a MySQL table

This article is a part of my "100 data engineering tutorials in 100 days" challenge. (92/100)

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):

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:

updated_ts   timestamp default current_timestamp on update current_timestamp

Remember to share on social media!
If you like this text, please share it on Facebook/Twitter/LinkedIn/Reddit or other social media.

If you want to contact me, send me a message on LinkedIn or Twitter.

Would you like to have a call and talk? Please schedule a meeting using this link.

Bartosz Mikulski
Bartosz Mikulski * data/machine learning engineer * conference speaker * co-founder of Software Craft Poznan & Poznan Scala User Group

This website DOES NOT use cookies
but you may still see the cookies set earlier if you have already visited it.