How to solve the problem of Mysql using on update current_timestamp
Mysql uses on update current_timestamp. Notes
In recent projects, the time field (datetime, timestamp) has used the ON UPDATE CURRENT_TIMESTAMP attribute to automatically update the column timestamp. . The following records some points that need attention.
Let’s talk about the conclusion first
If you set the time to update with the current timestamp (ON UPDATE CURRENT_TIMESTAMP), then when you update the field, only the field value changes, your time field will be It will change to the time during UPDATE operation.
That is, when there is no field update, although the update statement is executed successfully, the time field will not be updated.
For example, we create a new table:
CREATE TABLE `t_temp` ( `id` int(11) NOT NULL DEFAULT '0', `username` varchar(255) DEFAULT NULL, `upd_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '默认取当前时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Then insert a row of data, and then perform an update.
UPDATE t_temp SET username = 'mm' WHERE id = 1;
You will find that the time column will not be updated.
Similarly, some frameworks are used in the program, such as tk-mybatis. When using the update method (such as int updateByPrimaryKeySelective(T record); method), if the field is not actually updated, although the syntax is executed, the time field It will not be updated, which is easy to overlook in the system.
CURRENT_TIMESTAMP() is related to ON UPDATE and index
Basic probability
CURRENT_TIMESTAMP() can set the TIMESTAMP type in MYSQL to the current time.
ON UPDATE personally thinks this is an artifact. It is generally used when a certain field is updateTIme and the last modified time is used. Just use this ON UPDATE. In this way, developers do not have to handle it themselves, MySQL can handle it automatically.
Finally, let’s talk about the index. This one comes from Baidu. I forgot who it is from: the index is a special file (the index on the InnoDB data table is a component of the table space) , they contain reference pointers to all records in the data table.
It can be said that a database index is like the table of contents of a book, which can increase the speed of database queries. Indexes are divided into clustered indexes and non-clustered indexes. Clustered indexes are ordered according to the physical location of data storage, but non-clustered indexes are different; clustered indexes can increase the speed of multi-row retrieval, while Non-clustered indexes are fast for retrieval of single rows.
If you create too many indexes, it will have an impact on the speed of update and insert operations, because each index file needs to be updated. For a table that frequently needs to be updated and inserted, there is no need to create a separate index for a rarely used where clause. For smaller tables, the sorting overhead will not be very large, and there is no need to create additional indexes.
Code and Example
The table creation code is as follows:
CREATE TABLE timeDemo( id VARCHAR(64) NOT NULL, timeTest TIMESTAMP NOT NULL, PRIMARY KEY (id) );
When adding a new data:
He will automatically create a time, such as an order table, and if he wants to record the time when an order is placed, he can use this method to process it.
Including updateTime, the last update time:
CREATE TABLE timeDemo2( id VARCHAR(64) NOT NULL, createTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(), updateTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(), PRIMARY KEY (id) );
//The above version of mysql5.7 or above will not have problems, but the version below 5.7 will have problems
// For versions below 5.7, it is recommended to use a program to insert.
Here we use 5.5 to demonstrate the effect of ON UPDATE
CREATE TABLE timeDemo3( id VARCHAR(64) NOT NULL, updateTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(), PRIMARY KEY (id) );
When some data in this table is modified, the value of updateTime will be automatically updated.
#Every time this record is updated, updateTime will be updated automatically. In this case, the programmer does not need to handle it himself.
It can be found here that unfortunately many Internet companies like to use MySQL5.7 or above, while traditional industries still use MySQL5.5 version.
Let’s talk about index key and index
CREATE TABLE timeDemo4( id VARCHAR(64) NOT NULL, id2 VARCHAR(64) NOT NULL, updateTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(), PRIMARY KEY (id), KEY(id2) )ENGINE=INNODB DEFAULT CHARSET=utf8
The only purpose of ordinary index is to improve the speed of data access. This kind of index is defined by the keyword KEY or INDEX. Therefore, indexes should only be created for those data columns that appear most frequently in query conditions (WHERE column = ...) or sorting conditions (ORDER BY column).
In order to create an index, you should choose the most regular and compact data column, such as an integer type data column, whenever possible.
The above is the detailed content of How to solve the problem of Mysql using on update current_timestamp. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
