Two TIMESTAMP fields are urgently needed when designing a MYSQL table.

Source: Internet
Author: User
Tags table definition
When designing a MYSQL table, two TIMESTAMP fields are required. Sometimes, a database table requires a record creation time and a record modification time. The ideal design is as follows: the initial value of the update time is the same as the Creation time: CREATETABLE 'test _ table' ('id' INT (10) NOTNULL, 'create _ time' TIMEST

When designing a MYSQL table, two TIMESTAMP fields are required. Sometimes, a database table requires a record creation time and a record modification time. The ideal design is as follows: the initial value of the update time is the same as the Creation time: create table 'test _ table' ('id' INT (10) not null, 'create _ time' TIMEST

When designing a MYSQL table, two TIMESTAMP fields are required.

Sometimes, a database table requires a record creation time and a record modification time.

The ideal design is as follows: the initial value of the update time is the same as the creation time:
CREATE TABLE `test_table` (`id` INT( 10 ) NOT NULL,`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) ENGINE = INNODB;


Or, the initial value of the update time is null and the value is available only during the update:
CREATE TABLE `test_table` (`id` INT( 10 ) NOT NULL,`create_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,`update_time` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) ENGINE = INNODB;

Such an SQL statement for table creation cannot be executed. If an error is reported during execution, it is estimated that many people have encountered this problem:
Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause


Find a solution on the Internet (only applicable to the initial value of the update time, which is the same as the creation time. Of course, this is also true ):
CREATE TABLE `test_table` (`id` INT( 10 ) NOT NULL,`create_time` TIMESTAMP NOT NULL DEFAULT 0,`update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) ENGINE = INNODB;


INSERT statement:
INSERT INTO test_table (id, create_time, update_time) VALUES (1, NULL, NULL);


Or write like this (Note: create_time is not written ):
INSERT INTO test_table (id, update_time) VALUES (1, NULL);


The UPDATE statement is written normally (Suppose test_table.id can be modified ):
UPDATE test_table (id) VALUES (2);
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.