MySQL Auto set create_time and Update_time

Source: Internet
Author: User

Reference table Structure

CREATE TABLE ' T_baby ' (
' baby_id ' int (one) not NULL auto_increment,
' user_id ' int (one) is not NULL,
' Nickname ' varchar (+) DEFAULT NULL,
' Avatar ' varchar (+) DEFAULT ',
' Sex ' int (2) unsigned not NULL DEFAULT ' 0 ' COMMENT ' 0: unknown; \\n 1: Male; \\n 2: Female. ‘,
' Birthday ' datetime not NULL,
' creater ' varchar (+) DEFAULT ',
' Create_time ' timestamp not NULL DEFAULT Current_timestamp,
' Update_time ' timestamp null DEFAULT null on update Current_timestamp,
' Is_delete ' bigint () DEFAULT NULL COMMENT ' 0 ',
' Updater ' varchar (+) DEFAULT NULL,
PRIMARY KEY (' baby_id ')
) Engine=innodb auto_increment=10000000 DEFAULT charset=utf8mb4;

When we create business tables, we usually need to set create_time and update_time, but usually we need to set the time in the code to insert the database.

After the Current_timestamp field is set, MySQL automatically sets the current system time assignment to the property field when the Insert data

When the on Update Current_timestamp field is set, MySQL automatically sets the current system time assignment to the property field when the data is updated and changes successfully

Create_time Setting the Current_timestamp property

Update_time setting the on UPDATE Current_timestamp property

We don't need to manually assign values to these two parameters in code to save a certain amount of code.

MySQL Auto set create_time and Update_time

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.