How to Update Current Timestamp in MySQL?
Last Updated :
30 Nov, 2021
MySQL is an easy-to-use RDBMS. Many organizations prefer to use it because of its easy maintainability, easier to prepare schemas, stored procedures, triggers, and database maintenance. In this article, let us see how to update to Current Timestamp in MySQL.
Step 1: Database creation
Firstly we create the database.Here GEEKSFORGEEKS is the db name.
Query:
CREATE DATABASE GEEKSFORGEEKS;
Step 2: Make the database active
Query:
USE GEEKSFORGEEKS;
Step 3: Create a table employee with empLoginTime field as Timestamp.
Query:
CREATE TABLE employee
(
empId int NOT NULL,
empName VARCHAR(20),
empLoginTime TIMESTAMP
);
Output:

Step 4: We can set the time zone as follows
Query:
SET TIME_ZONE = '+00:00';
If we want to change the time Zone that also can be done.
Step 5: Now, let us insert the records
Query:
INSERT INTO employee
(
empId, empName, empLoginTime
)
VALUES
(1, 'XXX', '2021-01-01 00:00:01'),
(2, 'YYY', '2021-01-01 00:00:01'),
(3, 'ZZZ', '2021-01-01 00:00:01'),
(4, 'XYZ', '2021-01-01 00:00:01');
Step 6: Display the records
Query:
SELECT * FROM employee;
Output:

Here, the ''empLoginTime' column indicates the value in the mentioned timezone pattern.
Step 7: There are possibilities of viewing data from different timezones. During those times, we can set the timezone as we like
Query:
SET time_zone ='+03:00';
SELECT * FROM employee;
Output:

In MySQL, there is a facility to keep the default current timestamp value for a column. i.e. empLoginTime should automatically get the timestamp value whenever the employee login into the system. It is equivalent to updating the column to a current timestamp value.
Query:
ALTER TABLE employee MODIFY
COLUMN empLoginTime
TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
Let us check the structure of the table now
Query:
DESC employee;

Advantage of having the update of current_timestamp:
With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP, the "empLoginTime" column has the current timestamp for its default value and is automatically updated to the current timestamp.
Now let us insert the records in the table. Since we have the default current_timestamp and on update also to current_timestamp, we no need to give the values for 'empLoginTime'. Below is the query to do that.
Query:
**Since empLoginTime has the current
timestamp value, we no need to add the value
explicitly to that column**
INSERT INTO employee (empId, empName) VALUES
(5, 'ABC'),(6,'CDE');
Display the records
Query:
SELECT * FROM employee;

It is always better to have the 'login' and 'logout' columns and also similar kinds of columns to have to default current_timestamp values. If it is not set, we can update the column values easily.
In MySQL workbench, we have to do the following :
Steps to update the data
- Navigate to Edit --> Preferences.
- Then click the "SQL Editor" tab and uncheck the "Safe Updates" check box.
- Then click on Query --> Reconnect to Server.
Now execute your SQL query
Query:
UPDATE employee set empLoginTime = CURRENT_TIMESTAMP
WHERE empId < 4;
Display the records
Query:
SELECT * FROM employee;

Usually, we should have "created time" and "modified time" in a table to have the updated values. That will help for the auditing purpose. The default constraint is helpful to set the current timestamp value. During table creation itself, the default value can be set, or else by using ALTER command, we can set the default constraint. The other way is by using the 'UPDATE' command and which is seen in the above example.
Similar Reads
CURRENT_TIMESTAMP() function in MySQL
CURRENT_TIMESTAMP() function in MySQL is used to check the current date and time value. In MySQL function, CURRENT_TIMESTAMP is needed when you need to keep a record of the exact time of delivery which is helpful in improving the services and functionalities of any organization. The format of this f
2 min read
How to Convert Timestamp to Datetime in MySQL?
In this article, we are going to learn how to convert Timestamp to Datetime in MySQL.To execute these queries, we need to first add integer data(written in timestamp format) and then use the FROM_UNIXTIME() function to convert it into "Datetime" Data Type. FROM_UNIXTIME(): This function in MySQL ret
2 min read
UTC_TIMESTAMP() function in MySQL
UTC_TIMESTAMP() function in MySQL is used to check current Coordinated Universal Time (UTC) date and time value. It returns the current UTC date and time value in YYYY-MM-DD HH:MM:SS or YYYYMMDDHHMMSS.uuu format, depending on whether the function is used in string or numeric context. Syntax : UTC_TI
2 min read
UNIX_TIMESTAMP() function in MySQL
UNIX_TIMESTAMP() : This function in MySQL helps to return a Unix timestamp. We can define a Unix timestamp as the number of seconds that have passed since â1970-01-01 00:00:00âUTC. Even if you pass the current date/time or another specified date/time, the function will return a Unix timestamp based
2 min read
TIMESTAMPDIFF() function in MYSQL
TIMESTAMPDIFF() : This function in MySQL is used to return a value after subtracting a DateTime expression from another. Syntax : TIMESTAMPDIFF(unit,expr1,expr2) Parameters : It will accept three parameters. unit â It denotes the unit for the result. It can be one of the following.MICROSECOND, SECON
2 min read
How To Update Multiple Columns in MySQL?
To update multiple columns in MySQL we can use the SET clause in the UPDATE statement. SET clause allows users to update values of multiple columns at a time. In this article, we will learn how to update multiple columns in MySQL using UPDATE and SET commands. We will cover the syntax and examples,
3 min read
PLSQL | CURRENT_TIMESTAMP Function
The PLSQL CURRENT_TIMESTAMP function is used to return the current date and time in session time zone. The time zone used is the time zone of the current SQL session as set by the ALTER SESSION command. The CURRENT_TIMESTAMP function returns a value of TIMESTAMP WITH TIME ZONE while the CURRENT_DATE
2 min read
CURRENT_TIME() function in MySQL
In MySQL, the CURRENT_TIME() function is a valuable tool for retrieving the current system time from the server, providing the exact time in hours, minutes, and seconds. It is widely used in scenarios where tracking or querying the current time is essential without needing the full date. In this art
3 min read
How to Get Current Date and Time in SQL?
Managing date and time efficiently is crucial for any database system. SQL provides built-in functions to retrieve the current date and time, which are especially useful for applications involving logging, reporting, and auditing. In this article, we will explain the three main SQL functions to fetc
4 min read
CURRENT_TIMESTAMP() Function in SQL Server
In SQL Server, the CURRENT_TIMESTAMP function is a widely used feature for retrieving the current date and time directly from the database server. It returns the exact moment when the SQL query is executed in the YYYY-MM-DD hh:mm:ss.mmm format.In this article, We will learn about CURRENT_TIMESTAMP()
3 min read