With the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP, a column has the current timestamp for its default value and is automatically updated to the current timestamp.
Let us see an example and create a table −
mysql> create table DemoTable737 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(100), StudentAdmissiondate datetime ); Query OK, 0 rows affected (0.68 sec)
Following is the query to for CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP in MySQL −
mysql> alter table DemoTable737 modify column StudentAdmissiondate timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP; Query OK, 0 rows affected (2.20 sec) Records: 0 Duplicates: 0 Warnings: 0
Let us check the description of table once again −
mysql> desc DemoTable737;
This will produce the following output -
+----------------------+--------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +----------------------+--------------+------+-----+-------------------+-----------------------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentName | varchar(100) | YES | | NULL | | | StudentAdmissiondate | timestamp | YES | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +----------------------+--------------+------+-----+-------------------+-----------------------------+ 3 rows in set (0.01 sec)