How to resolve MySQL to set the current time as the default value

Source: Internet
Author: User

We often encounter the problem of setting the current time as the default value in MySQL. The following describes how to set the current time as the default value in MySQL.
Database: test_db1
Create Table: test_ta1
Two fields: id (auto-increment and primary key ),
Createtime creation date (default value: Current Time)

Method 1: Use the alert table statement:
Copy codeThe 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: easy to create directly:
Copy codeThe 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: visual tools such as mysql-front
Right-click the createtime attribute
Change the Type attribute value to timestamp.
Select <INSERT-TimeStamp> for the default attribute.
The preceding 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.