MySQL現在時刻をデフォルトに設定する方法

1035 ワード

MySQLの現在時刻をデフォルト値に設定する問題はよくありますが、以下ではMySQLの現在時刻をデフォルト値に設定するための実装の全手順を紹介します.
データベース:test_db1
表の作成:test_ta1
2つのフィールド:id(自増かつプライマリキー)、
createtime作成日(デフォルトは現在時刻)
方法1、alert table文を使う:
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();  
 
方法二、直接作成の便利さ:
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;  
 
方法3、mysql-frontなどの可視化ツール
createtimeプロパティを右クリック
Type属性値をtimestampに変更
defaultプロパティ選択
以上、MySQLが現在時刻をデフォルト値に設定する方法を説明しました.