How to store time created in a MySQL table?


You can use DEFAULT CURRENT_TIMESTAMP. Keep in mind that it will work only at the time of insertion. Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Arrivaltime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
   );
Query OK, 0 rows affected (0.31 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Arrivaltime) values('2018-01-31 10:34:56');
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.06 sec)

mysql> insert into DemoTable(Arrivaltime) values('2019-01-31 11:10:12');
Query OK, 1 row affected (0.04 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+---------------------+
| Id | Arrivaltime         |
+----+---------------------+
| 1  | 2018-01-31 10:34:56 |
| 2  | 2019-05-02 12:50:24 |
| 3  | 2019-01-31 11:10:12 |
+----+---------------------+
3 rows in set (0.00 sec)

Updated on: 30-Jul-2019

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements