In this tutorial, we’ll learn how to change the current timestamp in MySQL. Timestamps are a crucial part of any database system, especially when tracking the creation and modification of records. MySQL provides a built-in function called CURRENT_TIMESTAMP()
to generate the current timestamp. We’ll guide you through the process of updating a column with a new timestamp value using SQL queries.
Step 1: Create a Table with Timestamp Column
First, let’s create a sample table containing a column with the TIMESTAMP
data type. If you’re using the MySQL command line, you can execute the following SQL query to create the table:
1 2 3 4 5 6 |
CREATE TABLE sample_table ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); |
In this table schema, the created_at
and updated_at
columns store the timestamp information. The created_at
column records when a new record is inserted, while the updated_at
column records when the record is updated.
Step 2: Insert Data Into the Table
Now we’ll insert a new record into the sample_table
. This record will include the name
value and use the CURRENT_TIMESTAMP()
function for the created_at
and updated_at
columns.
1 |
INSERT INTO sample_table (name, created_at, updated_at) VALUES ('John Doe', CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP()); |
You can query the table to confirm that the record was successfully inserted with the current timestamp:
1 |
SELECT * FROM sample_table; |
Output:

Step 3: Update the Current Timestamp on an Existing Record
Now that we have a record in our table, we’ll update it with a new timestamp in the updated_at
column. To do this, we can use the UPDATE
statement with the CURRENT_TIMESTAMP()
function:
1 |
UPDATE sample_table SET updated_at = CURRENT_TIMESTAMP() WHERE id = 1; |
After executing the query, you can re-run the SELECT
query to confirm the changes:
1 |
SELECT * FROM sample_table; |
Output:

As you can see, the updated_at
column now displays the new timestamp, while the created_at
column remains the same.
Full Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
CREATE TABLE sample_table ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); INSERT INTO sample_table (name, created_at, updated_at) VALUES ('John Doe', CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP()); SELECT * FROM sample_table; UPDATE sample_table SET updated_at = CURRENT_TIMESTAMP() WHERE id = 1; SELECT * FROM sample_table; |
Conclusion
In this tutorial, we learned how to change the current timestamp of a record in a MySQL table. Using the UPDATE
statement along with the CURRENT_TIMESTAMP()
function, we can easily change the timestamp value for a specific column. This is particularly useful when tracking changes and updates to records in a database.