
- 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
In MySQL, how can I insert date and time automatically while inserting NULL values to the other columns?
In MySQL, we can insert current date and time automatically to a column on inserting the NULL values in other columns by declaring that column as DEFAULT CURRENT_TIMESTAMP. In this case, we cannot declare the column NOT NULL in which we want to insert NULL values.
mysql> Create Table Testing1(Name Varchar(20), RegStudent TIMESTAMP DEFAULT CURRENT_TIMESTAMP); Query OK, 0 rows affected (0.15 sec)
Above query will create a table ‘Testing1’ with a column named ‘Name’(not declared ‘NOT NULL’) and other column named ‘RegDate’ declared as DEFAULT CURRENT_TIMESTAMP. Now, on inserting the NULL values Name column, the current date and time will be inserted in the other column automatically.
mysql> Insert into Testing1(Name) Values(NULL); Query OK, 1 row affected (0.08 sec) mysql> Insert into Testing1(Name) Values(NULL); Query OK, 1 row affected (0.04 sec) mysql> Select * from Testing1; +------+---------------------+ | Name | RegStudent | +------+---------------------+ | NULL | 2017-10-29 04:46:59 | | NULL | 2017-10-29 04:47:02 | +------+---------------------+ 2 rows in set (0.05 sec)
From the above queries, we can see that on inserting the NULL values in ‘Name’, the date and time are also inserting automatically.
- Related Articles
- How can we insert current date and time automatically on inserting values in other columns in MySQL?
- Update the date and time values while inserting them in MySQL
- How can I use TIME_FORMAT() function to offload time/date values in MySQL?
- Format date while inserting records in MySQL
- How can we insert current date automatically in a column of MySQL table?
- How can I insert the values in columns without specifying the names of the column in MySQL INSERT INTO statement?
- How to insert current date/time in MySQL?
- Can we skip a column name while inserting values in MySQL?
- Can we use INTERVAL keyword while inserting date records in a MySQL table?
- MySQL - Insert current date/time?
- Can MySQL automatically convert empty strings to NULL?
- When inserting a new row, should I include the columns that are null in the MySQL query?
- How do I insert a NULL value in MySQL?
- Populate null columns in a MySQL table and set values
- How to Automatically Hide Columns Based on Date in Excel?

Advertisements