The TIMESTAMP data type is used for values containing both date and time parts. Let us first create a table −
mysql> create table DemoTable662( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,UserName varchar(100),UserPostDate datetime ); Query OK, 0 rows affected (0.50 sec)
Following is the query for valid default timestamp values −
mysql> alter table DemoTable662 MODIFY COLUMN UserPostDate TIMESTAMP NOT NULL DEFAULT current_timestamp; Query OK, 0 rows affected (1.81 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of table once again −
mysql> desc DemoTable662;
This will produce the following output −
+--------------+--------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+-------------------+----------------+ | UserId | int(11) | NO | PRI | NULL | auto_increment | | UserName | varchar(100) | YES | | NULL | | | UserPostDate | timestamp | NO | | CURRENT_TIMESTAMP | | +--------------+--------------+------+-----+-------------------+----------------+ 3 rows in set (0.10 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable662(UserName) values('Chris'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable662(UserName) values('Robert'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable662(UserName,UserPostDate) values('Robert','2018-01-11'); Query OK, 1 row affected (0.27 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable662;
This will produce the following output −
+--------+----------+---------------------+ | UserId | UserName | UserPostDate | +--------+----------+---------------------+ | 1 | Chris | 2019-07-20 13:06:13 | | 2 | Robert | 2019-07-20 13:06:18 | | 3 | Robert | 2018-01-11 00:00:00 | +--------+----------+---------------------+ 3 rows in set (0.00 sec)