mysql的timestamp注意事項

2021-10-12 07:39:42 字數 1636 閱讀 9558

datetime型別取值範圍:1000-01-01 00:00:00 到 9999-12-31 23:59:59

timestamp型別取值範圍:1970-01-01 00:00:00 到 2037-12-31 23:59:59

timestamp型別具有自動初始化和自動更新的特性。

2.1 下面列舉下那些情況會自動更新

第乙個timestamp的字段,不指定是否為空缺省為not null,

下方test1和test2中create_date 都會自動更新

create table test1 (

id int primary key ,

create_date timestamp ,

update_date timestamp null default null

)engine=innodb default charset=utf8;

create table test2 (

id int primary key ,

create_date timestamp not null ,

update_date timestamp null default null

)engine=innodb default charset=utf8;

還有一種情況會自動更新:

指定更新是自動更新eg: on update current_timestamp

下面舉乙個例子:允許為空的時候,指定更新時自動更新

create table test5 (

id int primary key ,

create_date timestamp null on update current_timestamp,

update_date timestamp null default null

)engine=innodb default charset=utf8;

test5:插入的時候,不會插入時間

更新的時候,會自動更新create_date

2.2 下面列舉下哪些情況不會自動更新

第乙個timestamp,指定允許為null,或者指定預設值,not null default current_timestamp

create table test3 (

id int primary key ,

create_date timestamp null,

update_date timestamp null default null

)engine=innodb default charset=utf8;

create table test4 (

id int primary key ,

create_date timestamp not null default current_timestamp,

update_date timestamp null default null

)engine=innodb default charset=utf8;

允許為null,則插入和更新都不會自動插入該值

不允許為null,有預設值,第一次插入的時候會自動插入 預設值,更新的時候不會在自動更新該字段的值

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 ...