MySQL的timestamp型別自動更新問題

2021-06-28 23:29:04 字數 3342 閱讀 1305

注:本文討論的範圍為未啟用maxdb模式的表!

今天建了乙個表,裡面有乙個列是timestamp型別,我本意是在資料更新時,這個欄位的時間能自動更新。豈知對這個型別的值還不甚了解,導致出錯。發現這個字段只是在這行資料建立的時候有值,在更新的卻無變化。

查詢資料,發現是我建表的語句有問題:

以下是**片段:

create table `test` (

`t1` timestamp not null default current_timestamp,

`ww` varchar(5) not null

) engine=myisam ;

而實際上,以下兩個建表語句的效果是一樣的:

以下是**片段:

create table `test` (

`t1` timestamp not null ,

`ww` varchar( 5 ) not null

) engine = myisam

create table `test` (

`t1` timestamp not null default current_timestamp on update current_timestamp,

`ww` varchar(5) not null

) engine=myisam ;

比較之下,我的語句少了「on update current_timestamp」或多了「default current_timestamp」。如此一來,這個timestamp欄位只是在資料insert的時間建立時間,而update時就不會有變化了。當然,如果你就是想達到這個目的倒也無所謂。

查詢英文手冊(得到更詳細的解說,翻譯如下:

-----------------------------------翻譯開始--------------------------------

在create table語句中,第1個timestamp列可以用下面的任何一種方式宣告:

1: 如果定義時default current_timestamp和on update current_timestamp子句都有,列值為預設使用當前的時間戳,並且自動更新。

2: 如果不使用default或on update子句,那麼它等同於default current_timestamp on update current_timestamp。

3: 如果只有default current_timestamp子句,而沒有on update子句,列值預設為當前時間戳但不自動更新。

4: 如果沒用default子句,但有on update current_timestamp子句,列缺省為0並自動更新。

5: 如果有乙個常量值default,該列會有乙個預設值,而且不會自動初始化為當前時間戳。如果該列還有乙個on update current_timestamp子句,這個時間戳會自動更新,否則該列有乙個預設的常量但不會自動更新。

換句話說,你可以使用當前的時間戳去初始化值和自動更新,或者是其中之一,也可以都不是。(比如,你在定義的時候可以指定自動更新,但並不初始化。)下面的字段定義說明了這些情況:

以下是**片段:

自動初始化和更新:

ts timestamp default current_timestamp on update current_timestamp

只自動初始化:

ts timestamp default current_timestamp

只自動更新

ts timestamp default 0 on update current_timestamp

只是給乙個常量(注:0000-00-00 00:00:00)

ts timestamp default 0

-----------------------------------翻譯結束--------------------------------

上面我用紅色字型標出了"第1個timestamp列",這是因為,乙個表有多個timestamp列的話,定義起來是不一樣的。

比如上面的第二條:「如果不使用default或on update子句,那麼它等同於default current_timestamp on update current_timestamp。」如果是第二個timestamp列就不一樣了,請看:

以下是**片段:

create table `test` (

`ww` varchar( 9 ) not null ,

`t1` timestamp not null default current_timestamp ,

`t2` timestamp not null

) engine = myisam

這個情況下,資料插入時,t1會記錄當前時間,t2為預設值(0000-00-00 00:00:00),等同下面的語句:

以下是**片段:

create table  `test` (

`ww` varchar( 9 ) not null ,

`t1` timestamp not null default current_timestamp ,

`t2` timestamp not null default '0000-00-00 00:00:00'

) engine = myisam

而根據上面的第二條,應該是:

以下是**片段:

create table `test` (

`ww` varchar( 9 ) not null ,

`t1` timestamp not null default current_timestamp ,

`t2` timestamp on update current_timestamp not null default current_timestamp

) engine = myisam

但實際上,上面的語句是個語法錯誤,mysql會返回:

以下是引用片段:

#1293 - incorrect table definition; there can be only one timestamp column with current_timestamp in default or on update clause

我曾經想這樣設計乙個表,這個表有兩個timestamp列,乙個可以記錄更新時間,乙個可以記錄初始時間,但是嘗試多次以後,我發現mysql好像做不到這一點,不知道這個是mysql的缺陷還是自我優化,因為,這個功能可以使用datetime實現記錄初始化的時間,只是需要insert的時候指定一下。

mysql中timestamp的使用

mysql中timestamp的使用 mysql create table t1 id mediumint 9 not null auto increment,name char 11 default null,rq timestamp default current timestamp on up...

mysql多個TimeStamp設定

timestamp設定預設值是default current timestamp timestamp設定隨著表變化而自動更新是on update current timestamp 但是由於 兩行設定default current timestamp是不行的。還有一點要注意 create table...

mysql多個TimeStamp設定

timestamp設定預設值是default current timestamp timestamp設定隨著表變化而自動更新是on update current timestamp 但是由於 兩行設定default current timestamp是不行的。還有一點要注意 1 2 3 4 5 6 ...