How to set default date time as system date time in MySQL?


You can use CURRENT_TIMESTAMP to set system date time.

 Let us first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientFirstName varchar(20),
   ClientLastName varchar(20),
   ClientAge int
);
Query OK, 0 rows affected (0.66 sec)

Following is the query to set default datetime as system date time in MySQL −

mysql> alter table DemoTable add column ClientProjectDeadline timestamp default current_timestamp;
Query OK, 0 rows affected (0.46 sec)
Records: 0 Duplicates: 0 Warnings: 0

Let us check the description of table once again −

mysql> desc DemoTable;

This will produce the following output −

+-----------------------+-------------+------+-----+-------------------+----------------+
| Field                 | Type        | Null | Key | Default           | Extra          |
+-----------------------+-------------+------+-----+-------------------+----------------+
| Id                    | int(11)     | NO   | PRI | NULL              | auto_increment |
| ClientFirstName       | varchar(20) | YES  |     | NULL              |                |
| ClientLastName        | varchar(20) | YES  |     | NULL              |                |
| ClientAge             | int(11)     | YES  |     | NULL              |                |
| ClientProjectDeadline | timestamp   | YES  |     | CURRENT_TIMESTAMP |                |
+-----------------------+-------------+------+-----+-------------------+----------------+
5 rows in set (0.22 sec)
raja
Published on 29-Apr-2019 10:43:11
Advertisements