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;