MySQL set the current time as the default value method

Source: Internet
Author: User
Tags current time datetime

The alternative is to use the timestamp type instead of the datetime type.
Current_timestamp: When I update this record, this field in this record will not change.
Current_timestamp on update current_timestamp: When I update this record, this field in this record will change. That time becomes the time of the update.

(Note that an update setting lists the values it already has, which will not cause the timestamp column to be updated because if you set a list of its current values, MySQL ignores the changes for efficiency.) If there are multiple timestamp columns, only the first automatic update is available.

The following is an introduction to the full steps of MySQL setting the current time as the default value

Database: TEST_DB1

CREATE TABLE: Test_ta1

Field:

ID number (self-increasing and primary key),

Createtime Date Created (default is current time)

Method one, created with an alert table statement:

The code is as follows Copy Code

Use TEST_DB1; CREATE TABLE Test_ta1 (ID mediumint (8) unsigned not nulll auto_increment, Createtime datetime, PRIMARY key (ID)) E   Ngine=innodb default CHARSET=GBK; Alert Table test_ta1 Change createtime createtime timestamp NOT NULL default now ();

Method Two, create directly:

The code is as follows Copy Code

Use TEST_DB1; CREATE TABLE Test_ta1 (ID mediumint (8) unsigned not nulll auto_increment, createtime timestamp NOT NULL default Curren T_timestamp, primary key (ID)) Engine=innodb default CHARSET=GBK;

Method III, using visual tools such as Mysql-front to create

Right-click the Createtime property

Change the type attribute value to timestamp

Default Property Selection <INSERT-TimeStamp>

This is MySQL. Introduction to the method of setting the current time as the default value

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.