MySQL sets the current time as the default value.

Source: Internet
Author: User
Because the default value of the current MySQL field does not support functions, it is impossible to set the default value in the form of create_timedatetimedefanow now.

Because the default value of the current MySQL field does not support functions, it is impossible to set the default value in the form of create_time datetime default now.

The alternative is to use the TIMESTAMP type instead of the DATETIME type.
CURRENT_TIMESTAMP: When I update this record, this field of this record will not change.
CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP: When I UPDATE this record, this field of this record will change. Changes instantly to the Update time.

(Note that setting an UPDATE column as its existing value does not cause the TIMESTAMP column to be updated, because if you set a column as its current value, MySQL ignores the changes for efficiency .) If multiple TIMESTAMP columns exist, only the first one is automatically updated.

The following describes how to set the current time as the default value for MySQL.

Database: test_db1

Create Table: test_ta1

Field:

Id (auto-increment and primary key ),

Createtime creation date (default value: Current Time)

Method 1: Create with alert table statement:

The Code is as follows:

Use test_db1; create table test_ta1 (id mediumint (8) unsigned not nulll auto_increment, createtime datetime, primary key (id) engine = innodb default charset = gbk; alert table test_ta1 change createtime timestamp not null default now ();

Method 2: Create directly:

The Code is as follows:

Use test_db1; create table test_ta1 (id mediumint (8) unsigned not nulll auto_increment, createtime timestamp not null default current_timestamp, primary key (id) engine = innodb default charset = gbk;

Method 3: Use a visual tool (such as mysql-front) to create

Right-click the createtime attribute

Change the Type attribute value to timestamp.

Select the default Property

The above describes how to set the current time as the default value for MySQL.

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.