MYSQL Update time Auto sync vs. Create time default coexistence issues

Source: Internet
Author: User
Tags mysql update table definition

This paper mattered meters along

This address: http://blog.csdn.net/sushengmiyan/article/details/50326259

When using SQL, you want to automatically populate the update time when the data is updated, then how is it implemented in MySQL?

such as creating tables users

CREATE TABLE rs_signupuser (        ID VARCHAR (COMMENT)  ' primary key (business function independent) ',        USERNAME VARCHAR  COMMENT ' name ',        Usergender VARCHAR (3) DEFAULT ' 0 ' COMMENT ' sex 0 male 1 female ',        Idcardnumber varchar COMMENT ' ID number ',        usernation varchar (6) COMMENT ' Nation ',        BIRTHDAY DATE  COMMENT ' birthday ', C9/>userage INTEGER COMMENT ' age ',        mobilephone varchar (one) COMMENT ' mobile number ', BZ VARCHAR (6) COMMENT ' Notes ',        Createtime TIMESTAMP DEFAULT current_timestamp COMMENT ' registration time ',        createuserid VARCHAR COMMENT ' creator ', PX INTEGER COMMENT ' sort ',        updateuserid VARCHAR (COLLATE utf8_bin COMMENT ' update person ', UpdateTime TIMESTAMP DEFAULT Current_ TIMESTAMP on update current_timestamp NULL COMMENT ' update Time ',        CONSTRAINT   PRIMARY Key (ID),  UNIQUE KEY ( ZONECODE,USERZYBM)    ) COMMENT = ' Registered user table ';
Wherein, UpdateTime TIMESTAMP DEFAULT current_timestamp on update current_timestamp NULL COMMENT ' update Time ', will be automatically populated at the time of the update. La La. Happy with it.


Do not be happy so early, if you are using the mysql5.4 version, then you create the table when the error, not to mention the update time, see:


ERROR 1293 (HY000): incorrect table definition; There can is only one TIMESTAMP column with Current_timestamp in DEFAULT or on UPDATE clause

If a bird goes wrong, creating a table fails.

Look at the timestamp of version 5.5 document: http://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html

  • One TIMESTAMP column in a table can has the current timestamp as the default value for initializing the column, as the Auto-u Pdate value, or both. It is not a possible to having the current timestamp being the default value for one column and the Auto-update value for another Column.


You can see that this version has a limit on timestamps.

And look at version 5.7.

Http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html

There is no such limit. I think that the lower version of the only way to do something else, manually or using a trigger to do the update time this thing. Or just upgrade the version directly to fix the problem.


MYSQL Update time Auto sync vs. Create time default coexistence issues

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.