自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL數(shù)據(jù)庫TIMESTAMP設(shè)置默認(rèn)值的靈活運(yùn)用

數(shù)據(jù)庫 MySQL
本文我們主要介紹了MySQL數(shù)據(jù)庫TIMESTAMP設(shè)置默認(rèn)值的靈活運(yùn)用,是以實(shí)際的例子來進(jìn)行說明的,希望本次的介紹能夠?qū)δ兴鶐椭?/div>

MySQL數(shù)據(jù)庫TIMESTAMP設(shè)置默認(rèn)值是本文我們主要要介紹的內(nèi)容,我們知道,CURRENT_TIMESTAMP :當(dāng)我更新這條記錄的時(shí)候,這條記錄的這個(gè)字段不會(huì)改變。

CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP :當(dāng)我更新這條記錄的時(shí)候,這條記錄的這個(gè)字段將會(huì)改變。即時(shí)間變?yōu)榱烁聲r(shí)候的時(shí)間。(注意一個(gè)UPDATE設(shè)置一個(gè)列為它已經(jīng)有的值,這將不引起TIMESTAMP列被更新,因?yàn)槿绻阍O(shè)置一個(gè)列為它當(dāng)前的值,MySQL為了效率而忽略更改。)

如果有多個(gè)TIMESTAMP列,只有***個(gè)自動(dòng)更新。

接下來我們看幾個(gè)實(shí)際的例子:

#1創(chuàng)建一個(gè)有兩個(gè)timestamp字段的表dj1。

  1. root@localhost:test >create table dj1 (a char(1), b timestamp ,c timestamp);   
  2. Query OK, 0 rows affected (0.01 sec) 

 

#2插入兩行數(shù)據(jù),僅賦值于列A

  1. root@localhost:test >insert into dj1 values (1,null,null);  
  2. Query OK, 1 row affected (0.00 sec)  
  3. root@localhost:test >insert into dj1 values (2,null,null);   
  4. Query OK, 1 row affected (0.00 sec) 

 

#3查詢插入數(shù)據(jù),b,c兩列都使用current timestamp作為默認(rèn)值。

  1. root@localhost:test >select * from dj1;  
  2. +------+---------------------+---------------------+  
  3. | a | b | c |  
  4. +------+---------------------+---------------------+  
  5. | 1 | 2009-09-09 13:48:40 | 2009-09-09 13:48:40 |   
  6. | 2 | 2009-09-09 13:48:44 | 2009-09-09 13:48:44 |   
  7. +------+---------------------+---------------------+  
  8. 2 rows in set (0.00 sec) 

 

#4更新一行數(shù)據(jù),發(fā)現(xiàn)b列timestamp被自動(dòng)更新,而c列保持不變。

  1. root@localhost:test >update dj1 set a=9 where a=1;   
  2. Query OK, 1 row affected (0.00 sec)  
  3. Rows matched: 1 Changed: 1 Warnings: 0  
  4. root@localhost:test >select * from dj1;  
  5. +------+---------------------+---------------------+  
  6. | a | b | c |  
  7. +------+---------------------+---------------------+  
  8. | 9 | 2009-09-09 13:49:08 | 2009-09-09 13:48:40 |   
  9. | 2 | 2009-09-09 13:48:44 | 2009-09-09 13:48:44 |   
  10. +------+---------------------+---------------------+  
  11. 2 rows in set (0.00 sec) 

 

#5再更新一列,仍然如#4

  1. root@localhost:test >update dj1 set a=8 where a=2;   
  2. Query OK, 1 row affected (0.00 sec)  
  3. Rows matched: 1 Changed: 1 Warnings: 0  
  4. root@localhost:test >select * from dj1;  
  5. +------+---------------------+---------------------+  
  6. | a | b | c |  
  7. +------+---------------------+---------------------+  
  8. | 9 | 2009-09-09 13:49:08 | 2009-09-09 13:48:40 |   
  9. | 8 | 2009-09-09 13:49:36 | 2009-09-09 13:48:44 |   
  10. +------+---------------------+---------------------+  
  11. 2 rows in set (0.00 sec) 

 

#6在b列上創(chuàng)建***索引

  1. root@localhost:test >create unique index dj1_idx_u1 on dj1(b);  
  2. Query OK, 2 rows affected (0.01 sec)  
  3. Records: 2 Duplicates: 0 Warnings: 0 

 

#7更新所有行a列,報(bào)***性沖突。

  1. root@localhost:test >update dj1 set a=1;  
  2. ERROR 1062 (23000): Duplicate entry '2009-09-09 13:54:45' for key 'dj1_idx_u1' 

 

結(jié)論:

1.MySQL默認(rèn)表的***個(gè)timestamp字段為NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP屬性,必須顯式定義改變這種行為。

2.MySQL只允許一個(gè)timestamp字段擁有[DEFAULT CURRENT_TIMESTAMP |ON UPDATE CURRENT_TIMESTAMP]屬性。 我的理解為要么都是DEFAULT CURRENT_TIMESTAMP 要么都是DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

3.修改字段屬性值

 

  1. show create table tbl_ledgerrecord;  
  2. alter table tbl_ledgerrecord change intoStorageDate  intoStorageDate timestamp DEFAULT CURRENT_TIMESTAMP; 

 

關(guān)于MySQL數(shù)據(jù)庫TIMESTAMP設(shè)置默認(rèn)值的靈活運(yùn)用的知識(shí)就介紹到這里了,希望本次的介紹能夠?qū)δ兴斋@!

【編輯推薦】

  1. MySQL數(shù)據(jù)庫my.cnf配置文件注釋詳解
  2. SQL Server數(shù)據(jù)庫復(fù)制失敗的原因及解決方案
  3. SQL Server 2005數(shù)據(jù)庫游標(biāo)調(diào)用函數(shù)實(shí)例解析
  4. MySQL數(shù)據(jù)庫中EXPLAIN的使用及其注意事項(xiàng)詳解
  5. SQL Server數(shù)據(jù)庫用視圖來處理復(fù)雜的數(shù)據(jù)查詢關(guān)系
責(zé)任編輯:趙鵬 來源: ChinaUnix博客
相關(guān)推薦

2011-08-23 18:46:27

MySQLTIMESTAMP

2009-12-07 17:20:29

PHP stdClas

2009-10-23 15:30:53

無線接入技術(shù)

2011-07-25 16:25:47

2019-10-21 15:30:54

JS技巧前端

2009-02-20 10:59:21

Vista幫助系統(tǒng)使用技巧

2010-04-21 14:56:23

Unix 線程

2010-04-27 17:06:16

AIX vmstat

2009-07-01 17:58:20

JSP

2011-07-08 13:56:00

域控制器服務(wù)器

2012-02-04 14:56:52

JP1數(shù)據(jù)中心

2013-04-10 10:39:57

2013-04-07 10:15:34

2021-02-25 13:40:17

MySQL數(shù)據(jù)庫默認(rèn)值

2021-07-12 07:08:52

Spring Boot集成框架

2024-01-26 16:28:28

C++動(dòng)態(tài)內(nèi)存開發(fā)

2009-12-01 11:33:03

PHP判斷字符串的包含

2012-01-10 10:05:47

文件目錄訪問控制UGO

2010-11-23 16:49:42

MySQL設(shè)置當(dāng)前時(shí)間

2010-05-27 13:32:36

IIS服務(wù)安全認(rèn)證
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)