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