
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we insert current date and time automatically on inserting values in other columns in MySQL?
In MySQL, we can insert the current date and time automatically to a column on inserting the values in another column by declaring that column as DEFAULT CURRENT_TIMESTAMP.
Example
mysql> Create table testing -> ( -> StudentName varchar(20) NOT NULL, -> RegDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP -> ); Query OK, 0 rows affected (0.49 sec)
Above query will create a table ‘testing’ with a column named StudentName and other column named ‘RegDate’ declared as DEFAULT CURRENT_TIMESTAMP. Now, on inserting the values i.e. names in StudentName column, the current date and time will be inserted in the other column automatically.
mysql> Insert into testing(StudentName) values ('Ram'); Query OK, 1 row affected (0.14 sec) mysql> Insert into testing(StudentName) values ('Shyam'); Query OK, 1 row affected (0.06 sec) mysql> Select * from testing; +-------------+---------------------+ | StudentName | RegDate | +-------------+---------------------+ | Ram | 2017-10-28 21:24:24 | | Shyam | 2017-10-28 21:24:30 | +-------------+---------------------+ 2 rows in set (0.02 sec) mysql> Insert into testing(StudentName) values ('Mohan'); Query OK, 1 row affected (0.06 sec) mysql> Select * from testing; +-------------+---------------------+ | StudentName | RegDate | +-------------+---------------------+ | Ram | 2017-10-28 21:24:24 | | Shyam | 2017-10-28 21:24:30 | | Mohan | 2017-10-28 21:24:47 | +-------------+---------------------+ 3 rows in set (0.00 sec)
From the above queries, we can see that on inserting the values in StudentName, the date and time is also inserting automatically.
With the help of the above concept, we can also know that exactly on what date and time the values in other column inserted.
- Related Articles
- In MySQL, how can I insert date and time automatically while inserting NULL values to the other columns?
- How can we insert current date automatically in a column of MySQL table?
- How to insert current date/time in MySQL?
- MySQL - Insert current date/time?
- How can we insert current year automatically in a YEAR type column of MySQL table?
- Update the date and time values while inserting them in MySQL
- MySQL query to insert current date plus specific time?
- How to insert current date/ time using now() in a field with MySQL?
- How to Automatically Hide Columns Based on Date in Excel?
- Insert current time minus 1 hour to already inserted date-time records in MYSQL
- How can we specify default values in MySQL INSERT statement?
- How to insert current date and time in a database using JDBC?
- Insert current date in datetime format MySQL?
- How can we offload the time/date handling in MySQL?
- Can we skip a column name while inserting values in MySQL?

Advertisements