We often encounter the problem of setting the current time as the default value in MySQL. The following describes how to set the current time as the default value in MySQL.
Database: test_db1
Create Table: test_ta1
Two fields: id (auto-increment and primary key ),
Createtime creation date (default value: Current Time)
Method 1: Use the alert table statement:
Copy codeThe 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: easy to create directly:
Copy codeThe 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: visual tools such as mysql-front
Right-click the createtime attribute
Change the Type attribute value to timestamp.
Select <INSERT-TimeStamp> for the default attribute.
The preceding describes how to set the current time as the default value for MySQL.