Resolve the MySQL method that sets the current time to the default value

  • 2020-05-19 06:05:41
  • OfStack

We often encounter the problem of setting the current time as the default value of MySQL. The following is the full implementation step of setting the current time as the default value of MySQL. I hope you can get some enlightenment.
Database: test_db1
Create a table: test_ta1
Two fields: id (self-incrementing and primary key),
createtime creation date (default is current time)

Method 1. Use the alert table statement:


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 createtime timestamp not null default now(); 

Method 2. Easy to create directly:

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. Visualization tools such as mysql-front
Right click on the createtime property
Change the Type property value to timestamp
default property selection < INSERT-TimeStamp >
That's how MySQL sets the current time to the default.


Related articles: