Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Processing and Migration rewriting cases of update_time time Field column in Mysql

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the Mysql update_time time field column processing and migration rewriting cases, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

Phenomenon description

Demand:

1. When a new record is added, the database automatically set the current time of the system to the creation time and update time fields.

two。 When updating a record, the database only update the time of the update time field and does not modify the value corresponding to the creation time field.

Processing method Mysql:

Requirement 1: creation time field

`creat_ time`timestamp NULL DEFAULT CURRENT_TIMESTAMP

Requirement 2: update time field

`update_ time`timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Description: that is, it is handled by the default value of the field.

How to deal with Oracle and DM:

On Oracle or Damon, demand 1 (creation time field) is handled in the same way, but for requirement 2 (update time field), it needs to be handled through triggers. That is:

Requirement 1: creation time field

CREAT_TIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP

Requirement 2: update time field

UPDATE_TIME TIMESTAMP

The property cannot be specified by changing the field through the default value. You need to specify a trigger for this column:

Create or replace trigger your_tablename_update_time

Before delete or update on your_tablename

Referencing OLD AS "OLD" NEW AS "NEW"

For each row

BEGIN

/ * trigger body * /

IF UPDATING THEN

: new.update_time = CURRENT_TIMESTAMP

END IF

Exception when others then

Null

END

/

Description:

1. In fact, in the DM or Oracle system, this field, if there is a similar renewal, is maintained by the application itself, and the workload is very small. For example, when requirement 2 is update, there is only one more update field.

two。 If it was originally developed on Mysql, in order to save trouble and make use of the corresponding characteristics of mysql, when you migrate to DM or Oracle, you can achieve it through triggers without changing any application logic.

Thank you for reading this article carefully. I hope the article "the processing and migration and rewriting cases of update_time time field columns in Mysql" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 201

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report