MySQL設置當前時間爲默認值的方法

MySQL設置當前時間爲默認值的問題我們經常會遇到,下面就爲您介紹MySQL設置當前時間爲默認值的實現全步驟,希望對您能有所啓迪。

數據庫:test_db1

創建表:test_ta1

兩個字段:id              (自增 且爲主鍵),

createtime 創建日期(默認值爲當前時間)

方法一、是用alert table語句:

  1. use test_db1;  
  2.  
  3. create table test_ta1(  
  4.  
  5. id mediumint(8) unsigned not nulll auto_increment,  
  6.  
  7. createtime datetime,  
  8.  
  9. primary key (id)  
  10.  
  11. )engine=innodb default charset=gbk;  
  12.  
  13. alert table test_ta1 change createtime createtime timestamp not null default now();  
  14.  

方法二、直接創建方便:

  1. use test_db1;  
  2.  
  3. create table test_ta1(  
  4.  
  5. id mediumint(8) unsigned not nulll auto_increment,  
  6.  
  7. createtime timestamp not null default current_timestamp,  
  8.  
  9. primary key (id)  
  10.  
  11. )engine=innodb default charset=gbk;  
  12.  

方法三、可視化工具如 mysql-front

右擊createtime屬性

把Type屬性值改爲timestamp

default 屬性選擇<INSERT-TimeStamp>

以上就是MySQL設置當前時間爲默認值的方法介紹。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章