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!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.