How can I prevent a field in MySQL from being updated?

You can use the ON UPDATE CURRENT_TIMESTAMP attribute to prevent a field from being updated. This way, you can maintain the original value of the field without it being affected by update operations.

For example, when creating a table, you can use the following statement to specify a field that will not be updated:

CREATE TABLE my_table (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

In the example above, the “created_at” field will automatically be set to the current time when inserting data, while the “updated_at” field will automatically update to the current time when data is updated. Therefore, if you want a field to not update, you simply need to omit the ON UPDATE CURRENT_TIMESTAMP property.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds