MySQL datetime data Type Setting the current time is the default value

Source: Internet
Author: User
Tags mysql command line

Environment: MySQL Sever 5.1 + MySQL command line tool

Problem: MySQL datetime data Type set the current time to the default value

Solve:

Method One:

Because the default value for the current field in MySQL does not support functions, it is not possible 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.

The timestamp column type automatically marks the INSERT or update operation with the current date and time. If there are multiple timestamp columns, only the first one is automatically updated.

The automatic update of the first timestamp column occurs under any of the following conditions:

1. The column values are not explicitly specified in an INSERT or load DATA infile statement.

2. Column values are not explicitly specified in an UPDATE statement and some other columns change values. (Note An update setting that lists the values it already has, which will not cause the timestamp column to be updated, because if you set a list as its current value, MySQL ignores the changes for efficiency.) )

3. You explicitly set the timestamp column to be null.

4. The timestamp column except the first one can also be set to the current date and time, as long as the column is set to NULL, or now ().

So choose the date type to timestamp allow empty.

  1. CREATE TABLE Test (

  2. uname varchar() not NULL,

  3. UpdateTime timestamp NULL defaultcurrent_timestamp on UPDATE Current_timestamp

  4. ) Engine=innodb DEFAULT Charset=utf8;

If you want to operate under Navicat, set the field to timestamp, and then write the default value Current_timestamp.

Method Two:

You can also use trigger to implement this feature in MySQL5.0 or later versions.

  1. Create Table Test_time (

  2. Idint (11),

  3. Create_time datetime

  4. );

  5. Delimiter |

  6. Create Trigger default_datetime beforeinsert on Test_time

  7. foreach row

  8. If New.create_time is null Then

  9. Set New.create_time = Now ();

  10. End if;|

  11. delimiter;

MySQL datetime data Type Setting the current time is 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.