Because the default value of the current MySQL field does not support functions, it is impossible to set the default value in the form of create_timedatetimedefanow now.
Because the default value of the current MySQL field does not support functions, it is impossible 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.
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 following describes how to set the current time as the default value for MySQL.
Database: test_db1
Create Table: test_ta1
Field:
Id (auto-increment and primary key ),
Createtime creation date (default value: Current Time)
Method 1: Create with alert table statement:
| The 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: Create directly:
| The 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: Use a visual tool (such as mysql-front) to create
Right-click the createtime attribute
Change the Type attribute value to timestamp.
Select the default Property
The above describes how to set the current time as the default value for MySQL.