How To Change Current Timestamp In MySql

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:

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.

You can query the table to confirm that the record was successfully inserted with the current timestamp:

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:

After executing the query, you can re-run the SELECT query to confirm the changes:

Output:

As you can see, the updated_at column now displays the new timestamp, while the created_at column remains the same.

Full Code

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.