日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TIMESTAMP 与 explicit_defaults_for_timestamp

發布時間:2025/4/14 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TIMESTAMP 与 explicit_defaults_for_timestamp 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在MySQL 5.6.6之前,TIMESTAMP的默認行為:

  • TIMESTAMP列如果沒有明確聲明NULL屬性,默認為NOT NULL。(而其他數據類型,如果沒有顯示聲明為NOT NULL,則允許NULL值。)設置TIMESTAMP的列值為NULL,會自動存儲為當前timestamp。
  • 表中的第一個TIMESTAMP列,如果沒有聲明NULL屬性、DEFAULT或者 ON UPDATE,會自動分配 DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP 屬性。
  • 表中第二個TIMESTAMP列,如果沒有聲明為NULL或者DEFAULT子句,默認自動分配’0000-00-00 00:00:00′。插入行時沒有指明改列的值,該列默認分配’0000-00-00 00:00:00′,且沒有警告。
因為默認情況下充許為空,當插入值時,分兩種情況:第一列與第二列情況 explicit_defaults_for_timestamp=false [默認值] mysql> create table timestamp(id int not null auto_increment,time1 timestamp,time2 timestamp,primary key(id))engine=innodb; Query OK, 0 rows affected (0.27 sec)mysql> insert into timestamp(id) values(1); Query OK, 1 row affected (0.21 sec)mysql> select * from timestamp; +----+---------------------+---------------------+ | id | time1 | time2 | +----+---------------------+---------------------+ | 1 | 2016-07-02 17:12:28 | 0000-00-00 00:00:00 | +----+---------------------+---------------------+ 1 row in set (0.19 sec)mysql> mysql> show create table timestamp;
CREATE TABLE `timestamp` (`id` int(11) NOT NULL AUTO_INCREMENT, `time1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,`time2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8

2列TIMESTAMP未聲明為NULL的默認行為 timestamp有兩個屬性,分別是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP兩種,使用情況分別如下:1. CURRENT_TIMESTAMP 當要向數據庫執行insert操作時,如果有個timestamp字段屬性設為 CURRENT_TIMESTAMP,則無論這個字段有沒有set值都插入當前系統時間 2. ON UPDATE CURRENT_TIMESTAMP當執行update操作時,并且字段有ON UPDATE CURRENT_TIMESTAMP屬性。則字段無論值有沒有變化,它的值也會跟著更新為當前UPDATE操作時的時間。

?

從MySQL5.6.6開始這種默認設置的方法被廢棄了。在MySQL啟動時會出現以下警告:

1 2 3 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (seedocumentation for more details).

關閉警告,在my.cnf中加入

1 2 [mysqld] explicit_defaults_for_timestamp=true

重啟MySQL后錯誤消失,這時TIMESTAMP的行為如下:

  • TIMESTAMP如果沒有顯示聲明NOT NULL,是允許NULL值的,可以直接設置改列為NULL,而沒有默認填充行為。
  • TIMESTAMP不會默認分配DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP屬性。
mysql> create table timestamp1(id int not null auto_increment,time1 timestamp,time2 timestamp,primary key(id))engine=innodb; Query OK, 0 rows affected (0.11 sec)mysql> insert into timestamp1(id) values(1); Query OK, 1 row affected (0.18 sec)mysql> show create table timestamp1; CREATE TABLE `timestamp1` (`id` int(11) NOT NULL AUTO_INCREMENT, `time1` timestamp NULL DEFAULT NULL,`time2` timestamp NULL DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 mysql> select * from timestamp1; +----+-------+-------+ | id | time1 | time2 | +----+-------+-------+ | 1 | NULL | NULL | +----+-------+-------+ 1 row in set (0.18 sec)

?

?

mysql> set sql_mode="STRICT_TRANS_TABLES"; Query OK, 0 rows affected (0.00 sec)mysql> create table timestamp4(id int not null auto_increment,time1 timestamp not null,time2 timestamp not null,primary key(id))engine=innodb; Query OK, 0 rows affected (0.03 sec) mysql> insert into timestamp4 select 1,"0000-00-00 00:00:00","0000-00-00 00:00:00"; Query OK, 1 row affected (0.17 sec) Records: 1 Duplicates: 0 Warnings: 0mysql> insert into timestamp4 select 1,"0000-00-00 00:00:00","5"; ERROR 1292 (22007): Incorrect datetime value: '5' for column 'time2' at row 1

mysql> set sql_mode="STRICT_TRANS_TABLES,NO_ZERO_DATE"; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> insert into timestamp4 select 1,"0000-00-00 00:00:00","0000-00-00 00:00:00"; ERROR 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00' for column 'time1' at row 1 mysql> set sql_mode="STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE"; Query OK, 0 rows affected, 2 warnings (0.00 sec) mysql> insert into timestamp4 select 5,"2000-10-10 10:20:10","2000-10-10 10:20:00"; Query OK, 1 row affected (0.18 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into timestamp4 select 7,"2000-10-00 10:20:10","2000-10-10 10:20:11"; 日期中不能有00 ERROR 1292 (22007): Incorrect datetime value: '2000-10-00 10:20:10' for column 'time1' at row 1

?

?

?

mysql> create table y( a int not null auto_increment primary key,b timestamp Default CURRENT_TIMESTAMP);
當有記錄插入時,把當前日期與時間寫入到 b字段中,但更新不會改變值
mysql> create table y1( a int not null auto_increment primary key,b timestamp on update CURRENT_TIMESTAMP);
當有記錄插入時,NULL寫入到B 字段中,當該行update時,b列值為當前更新時間值
mysql> create table y2( a int not null auto_increment primary key,b timestamp default current_timestamp on update CURRENT_TIMESTAMP); 當插入時,有一個默認值,隨著當前行的更新,b列值也隨著更新為當前更新時間mysql> insert into y(a) values(null); Query OK, 1 row affected (0.17 sec)mysql> insert into y1(a) values(null); Query OK, 1 row affected (0.17 sec)mysql> insert into y2(a) values(null); Query OK, 1 row affected (0.17 sec)mysql> select * from y; +---+---------------------+ | a | b | +---+---------------------+ | 1 | 2016-07-02 18:56:59 | +---+---------------------+ 1 row in set (0.00 sec)mysql> select * from y1; +---+------+ | a | b | +---+------+ | 1 | NULL | +---+------+ 1 row in set (0.00 sec) mysql> select * from y2; +---+---------------------+ | a | b | +---+---------------------+ | 1 | 2016-07-02 18:57:08 | +---+---------------------+ 1 row in set (0.00 sec)




mysql> update y set a=2 where a=1; Query OK, 1 row affected (0.20 sec) Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from y; +---+---------------------+ | a | b | +---+---------------------+ | 2 | 2016-07-02 18:56:59 | +---+---------------------+ 1 row in set (0.01 sec)mysql> update y1 set a=2 where a=1; Query OK, 1 row affected (0.17 sec) Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from y1; +---+---------------------+ | a | b | +---+---------------------+ | 2 | 2016-07-02 19:00:12 | +---+---------------------+ 1 row in set (0.00 sec)mysql> update y2 set a=2 where a=1; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from y2; +---+---------------------+ | a | b | +---+---------------------+ | 2 | 2016-07-02 19:00:35 | +---+---------------------+ 1 row in set (0.00 sec) 設置正常日期格式:

嚴格日期格式:
set
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE"; 手動指定默認值:
create table y2( a int not null auto_increment primary key,b timestamp default current_timestamp on update CURRENT_TIMESTAMP);

?

轉載于:https://www.cnblogs.com/zengkefu/p/5636593.html

總結

以上是生活随笔為你收集整理的TIMESTAMP 与 explicit_defaults_for_timestamp的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。