MySQL sets the Default Time Value

Source: Internet
Author: User

Therefore CopyCode The Code is as follows: create_time datetime default now ()

It is impossible to set the default value.
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 timestamp column type Automatically marks the insert or update operation with the current date and time.
If multiple timestamp columns exist, only the first one is automatically updated.
The first timestamp column is automatically updated under any of the following conditions:
The column value is not explicitly specified in an insert or load data infile statement.
The column value is not explicitly specified in an update statement and other columns change the value. (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 .)
You explicitly set the timestamp column as null.
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 ().
In addition, you can also use trigger to implement this function in Versions later than 5.0. Copy code The Code is as follows: Create Table test_time (
Id int (11 ),
Create_time datetime
);
Delimiter |
Create trigger default_datetime before insert on test_time
For each row
If new. create_time is null then
Set new. create_time = now ();
End if; |
Delimiter;

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.