1. 程式人生 > >MySQL--建立時間和更新時間欄位

MySQL--建立時間和更新時間欄位

這是我寫的第一篇技術部落格, 剛剛畢業, 已經投入工作快一個月了, 寫一下剛剛遇到的問題, 在建立資料庫的時候, 我在一個表中需要建立時間和更新時間這兩個欄位, 所以sql命令如下:

 `release_time` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '釋出時間',   
`update_time` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',

點選執行, 資料庫發來了錯誤警示

there can be only one TIMESTAMP column with
CURRENT_TIMESTAMP in default or ON UPDATE clause.

網上查找了一下原因, 我在本地測試用的資料庫是MySQL版本5.5, 在MySQL 5.5的文件中有這麼一段話

One TIMESTAMP column in a table can have the current timestamp as the default value for

initializing the column, as the auto-update value, or both. It is not possible to have the

current timestamp be the
default value for one column and the auto-update value for another column.

意思就是在一個表中只能有一個TIMESTAMP型別欄位可以有CURRENT_TIMESTAMP作為預設值.

在MySQL-5.6.1有如下改變:

Previously, at most one TIMESTAMP column per table could be automatically initialized or

updated to the current date and time. This restriction has been lifted. Any TIMESTAMP column

definition can have any
combination of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP clauses. In addition, these clauses now can be used with DATETIME column definitions. For more information, see Automatic Initialization and Updating for TIMESTAMP and DATETIME.

就是說允許任何一個TIMESTAMP或者DATETIME型別欄位將CURRENT_TIMESTAMP作為預設值了.