如何在mysql中将默认日期时间设置为系统日期时间

时间:2010-04-05 09:51:59

标签: mysql mysql-error-1067

我正在尝试将列的默认日期时间设置为系统日期时间。 它显示了一个错误

  

默认值无效   'InsertionDate'

alter table `vts`.`tblpickpoint` 
  add column `InsertionDate` 
      datetime DEFAULT 'Now()' NULL after `PickPointLatLong`

3 个答案:

答案 0 :(得分:3)

mysql中列的默认值不能是函数的结果。

一个例外是旁观者指出的current_timestamp。

你的陈述应该是

alter table `vts`.`tblpickpoint` 
  add column `InsertionDate` TIMESTAMP 
             DEFAULT CURRENT_TIMESTAMP 

答案 1 :(得分:2)

看一下CURRENT_TIMESTAMP

答案 2 :(得分:0)

如果要在每次更改时初始化并更新值,请使用:

alter table `vts`.`tblpickpoint` 
  add column `InsertionDate` 
       TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  after `PickPointLatLong`

如果您只想要创建时间,请使用:

alter table `vts`.`tblpickpoint` 
  add column `InsertionDate` 
       TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  after `PickPointLatLong`