MySQL timestamp type field current_timestamp with on UPDATE Current_timestamp property

Source: Internet
Author: User

TIMESTAMP has two properties, namely Current_timestamp and on UPDATE Current_timestamp two, respectively, the following are used:

1.current_timestamp

When you want to perform an insert operation to the database, if you have a timestamp field property set to

Current_timestamp, the current system time is inserted regardless of whether the field has a wooden set value.

2.ON UPDATE Current_timestamp

When the update operation is performed, and the field has the on Update Current_timestamp property. The value of the field is updated to the time of the current update operation, regardless of whether the value has changed.

Variants of the timestamp

1.TIMESTAMP DEFAULT current_timestamp on UPDATE current_timestamp

Refreshes the data column when new records are created and existing records are modified

2.TIMESTAMP DEFAULT Current_timestamp

This field is set to the current time when a new record is created, but it is not refreshed when you modify it later

3.TIMESTAMP on UPDATE Current_timestamp

Set this field to 0 when creating a new record, and refresh it later when you modify it

4.TIMESTAMP DEFAULT ' Yyyy-mm-dd hh:mm:ss ' on UPDATE current_timestamp

Set this field to the given value when you create a new record, and refresh it later when you modify it

Use triggers to override:

CREATE TABLE' Example ' (' ID ')INTEGERUNSIGNED not NULLauto_increment, ' created 'TIMESTAMP  not NULL DEFAULT Current_timestamp, ' lastupdated 'DATETIME  not NULL,  PRIMARY KEY(' id ')) ENGINE=InnoDB;DROP TRIGGER IF EXISTS' Update_example_trigger ';D elimiter//CREATE TRIGGER' Update_example_trigger ' beforeUPDATE  on' Example ' forEach ROWSETNEW ' lastupdated '=Now ()//DELIMITER;

MySQL timestamp type field current_timestamp and on UPDATE current_timestamp properties

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.