
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Default and On Update Current Timestamp in MySQL
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)
Advertisements