
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How do I remove ON UPDATE CURRENT_TIMESTAMP from an existing column in MySQL?
The ON UPDATE CURRENT_TIMESTAMP defines that an update without an explicit timestamp would result in an update to the current timestamp value.
You can remove ON UPDATE CURRENT_TIMESTAMP from a column using ALTER command.
The syntax is as follows
ALTER TABLE yourTableName CHANGE yourTimeStampColumnName yourTimeStampColumnName timestamp NOT NULL default CURRENT_TIMESTAMP;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table removeOnUpdateCurrentTimeStampDemo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > Name varchar(20), - > UserUpdateTimestamp timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP - > ); Query OK, 0 rows affected (0.54 sec)
Check the description of the table using DESC command.
The query is as follows
mysql> desc removeOnUpdateCurrentTimeStampDemo;
The following is the output
+---------------------+-------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +---------------------+-------------+------+-----+-------------------+-----------------------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | Name | varchar(20) | YES | | NULL | | | UserUpdateTimestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +---------------------+-------------+------+-----+-------------------+-----------------------------+ 3 rows in set (0.04 sec)
Now look at the Extra field there is on update CURRENT_TIMESTAMP. The query to remove ON UPDATE CURRENT_TIMESTAMP is as follows:
mysql> alter table removeOnUpdateCurrentTimeStampDemo - > change UserUpdateTimestamp UserUpdateTimestamp timestamp NOT NULL default CURRENT_TIMESTAMP; Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0
Check the description of table once again.
The query is as follows
mysql> desc removeOnUpdateCurrentTimeStampDemo;
The following is the output
+---------------------+-------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------------+-------------+------+-----+-------------------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | Name | varchar(20) | YES | | NULL | | | UserUpdateTimestamp | timestamp | NO | | CURRENT_TIMESTAMP | | +---------------------+-------------+------+-----+-------------------+----------------+ 3 rows in set (0.00 sec)
If you want to remove the default CURRENT_TIMESTAMP, then the query is as follows
mysql> alter table removeOnUpdateCurrentTimeStampDemo - > change UserUpdateTimestamp UserUpdateTimestamp timestamp NOT NULL; Query OK, 0 rows affected (0.12 sec) Records: 0 Duplicates: 0 Warnings: 0
Check the description of table once again.
The query is as follows
mysql> desc removeOnUpdateCurrentTimeStampDemo;
The following is the output
+---------------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------------+-------------+------+-----+---------+----------------+ | Id | int(11) | NO | PRI | NULL | auto_increment | | Name | varchar(20) | YES | | NULL | | | UserUpdateTimestamp | timestamp | NO | | NULL | | +---------------------+-------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec)
Now look at the above sample output, we have removed the ON UPDATE CURRENT TIMESTAMP.
- Related Articles
- How can I drop an existing column from a MySQL table?
- Update existing column data in MySQL and remove the last string from a varchar column with strings and numbers
- How can we remove NOT NULL constraint from a column of an existing MySQL table?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- How can I change the name of an existing column from a MySQL table?
- How do I update the decimal column to allow more digits in MySQL?
- How do I run a command on an already existing Docker container?
- Remove and update the existing record in MongoDB?
- How do I remove a uniqueness constraint from a MySQL table?
- How do I detect if the ON UPDATE event fired with query in MySQL?
- How to add current date to an existing MySQL table?
- How to rename a column in an existing MySQL table?
- How to write PHP script to update an existing MySQL table?
- How do I remove a MySQL database?
