The SQL CURRENT_TIMESTAMP function is a built-in function that returns the current date and time according to the timezone of the database server. The function is often used in SQL queries to record the current date and time when inserting or updating data in a database table.
Syntax
CURRENT_TIMESTAMP
The syntax of the CURRENT_TIMESTAMP function is simple and easy to use. It can be used in a SELECT statement or as part of an INSERT or UPDATE statement.
Example
Here is an example of using the CURRENT_TIMESTAMP function to insert a new record into a table:
INSERT INTO my_table (column1, column2, created_at) VALUES ('value1', 'value2', CURRENT_TIMESTAMP);
In the above example, the CURRENT_TIMESTAMP function is used to insert the current date and time into the created_at column of the my_table table. This allows you to keep track of when each record was created in the table.
Similarly, you can use the CURRENT_TIMESTAMP function in an UPDATE statement to update the timestamp of an existing record:
UPDATE my_table SET column1 = 'new_value', updated_at = CURRENT_TIMESTAMP WHERE id = 1;
In the above example, the CURRENT_TIMESTAMP function is used to update the updated_at column with the current date and time when updating the record with the id of 1 in the my_table table.
In summary, the SQL CURRENT_TIMESTAMP function is a useful tool for recording and tracking the date and time of database activity. It is widely supported by most database management systems and can be easily incorporated into SQL queries to keep track of when records were created or updated.