What would happen if we insert empty values in a table with a column set as type TIMESTAMP CURRENT_TIMESTAMP?

MySQLMySQLi Database

If we will insert nothing in the INSERT statement, then for timestamp type, it would insert the current date-time. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserLoginDate timestamp default current_timestamp
   -> );
Query OK, 0 rows affected (0.61 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable(UserId) values(null);
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------+---------------------+
| UserId | UserLoginDate       |
+--------+---------------------+
|      1 | 2019-11-03 12:55:44 |
|      2 | 2019-11-03 12:55:45 |
|      3 | 2019-11-03 12:55:46 |
|      4 | 2019-11-03 12:56:06 |
+--------+---------------------+
4 rows in set (0.00 sec)
raja
Published on 17-Dec-2019 11:28:41
Advertisements