How to automatically update time with timestamp in MySQL database
The timestamp data type in MySQL is a special data type. It can automatically update the time as long as you update the record timestamp without using a program to update.
Usually there will be a Create date field in the table. Other databases have default value options. MySQL also has a default value timestamp, but in MySQL, not only insertion but also modification will update the timestamp value!
In this case, it is no longer the creation date. It is better to use it as the update date!
So to record the creation date in MySQL, you have to use datetime and then use the NOW() function to complete!
1, TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Refresh this data column when creating a new record and modifying an existing record
2, TIMESTAMP DEFAULT CURRENT_TIMESTAMP Set this
field to the current time when creating a new record, but in the future When modifying, it will no longer be refreshed
3, TIMESTAMP ON UPDATE CURRENT_TIMESTAMP Set this field to 0 when creating a new record
, automatically UPDATE and INSERT to the current time:
Table:
--------- --------------------------
Table Create Table
------ --------------- ----------- CREATE TABLE `t1` ( `p_c` int(11) NOT NULL, `p_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=gb2312
Data:
1 2007-10-08 11:53:35
2 2007-10-08 11:54:00
insert into t1(p_c) select 3;update t1 set p_c = 2 where p_c = 2;
data:
1 2007 -10-08 11:53:35
2 2007-10-08 12:00:37
3 2007-10-08 12:00:37
2. Automatically INSERT to the current time, but does not automatically UPDATE.
Table:
---------------------------------
Table Create Table
------ -- --------------------------
CREATE TABLE `t1` ( `p_c` int(11) NOT NULL, `p_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=gb2312
Data:
insert into t1(p_c) select 4;update t1 set p_c = 3 where p_c = 3;
1 2007-10-08 11:53:35
2 2007-10- 08 12:00:37
3 2007-10-08 12:00:37
4 2007-10-08 12:05:19
3. There cannot be two fields in a table whose default value is the current time, otherwise it will Something went wrong. But others are fine.
Table:
---------------------------------
Table Create Table
------ -- -------------------------- CREATE TABLE `t1` ( `p_c` int(11) NOT NULL, `p_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, ` p_timew2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ) ENGINE=InnoDB DEFAULT CHARSET=gb2312
Data:
1 2007-10-08 11:53:35 0000-00-00 00:00: 00
2 2007-10-08 12:00:37 0000-00-00 00:00:00
3 2007-10-08 12:00:37 0000-00-00 00:00:00
4 2007-10 -08 12:05:19 0000-00-00 00:00:00
In comparison, my statement lacks "on update CURRENT_TIMESTAMP" or adds "default CURRENT_TIMESTAMP". In this way, the timestamp field only establishes the time when the data is inserted, and there will be no change when it is updated. Of course, it doesn't matter if you just want to achieve this goal. 1: If both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses are defined when defining, the column value will use the current timestamp by default and be automatically updated.
2: If DEFAULT or ON UPDATE clause is not used, then it is equivalent to DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.
3: If there is only the DEFAULT CURRENT_TIMESTAMP clause but no ON UPDATE clause, the column value defaults to the current timestamp but is not automatically updated.
4: If the DEFAULT clause is not used, but the ON UPDATE CURRENT_TIMESTAMP clause is present, the column defaults to 0 and is automatically updated.
5: If there is a constant value DEFAULT, the column will have a default value and will not be automatically initialized to the current timestamp. If the column also has an ON UPDATE CURRENT_TIMESTAMP clause, this timestamp is automatically updated, otherwise the column has a default constant but is not automatically updated.
In other words, you can use the current timestamp to initialize the value and auto-update, or one of them, or neither. (For example, you can specify automatic update when defining, but not initialize.) The following field definition illustrates these situations:
The above is the content of the method of automatically updating the timestamp time in the MySQL database. Please pay attention to more related content. PHP Chinese website (www.php.cn)!

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

AI Hentai Generator
Generate AI Hentai for free.

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



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.
