﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-LIULIANG-随笔分类-mysql</title><link>http://www.cppblog.com/LIULIANG/category/20510.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 03 May 2013 14:39:48 GMT</lastBuildDate><pubDate>Fri, 03 May 2013 14:39:48 GMT</pubDate><ttl>60</ttl><item><title>mysql中TIMESTAMP设置默认值的灵活运用 </title><link>http://www.cppblog.com/LIULIANG/archive/2013/05/03/199951.html</link><dc:creator>BIG森林</dc:creator><author>BIG森林</author><pubDate>Fri, 03 May 2013 14:20:00 GMT</pubDate><guid>http://www.cppblog.com/LIULIANG/archive/2013/05/03/199951.html</guid><wfw:comment>http://www.cppblog.com/LIULIANG/comments/199951.html</wfw:comment><comments>http://www.cppblog.com/LIULIANG/archive/2013/05/03/199951.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LIULIANG/comments/commentRss/199951.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LIULIANG/services/trackbacks/199951.html</trackback:ping><description><![CDATA[<a href="http://blog.chinaunix.net/uid-20577907-id-2213871.html">mysql中TIMESTAMP设置默认值的灵活运用</a> <em>2011-08-18 12:12:51</em> 
<div class="Blog_con2">
<div class="Blog_con3">
<p>分类： <span>Mysql/postgreSQL</span></p>
<p>&nbsp;</p></div>
<div class="Blog_wz1">
<div class="post-body">
<p>默认值：</p></div>
<div class="post-body">CURRENT_TIMESTAMP ：当我更新这条记录的时候，这条记录的这个字段<span style="color: rgb(255,0,0)">不会改变</span>。</div>
<p class="post-body">CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ：当我更新这条记录的时候，这条记录的这个字段<span style="color: rgb(255,0,0)">将会改变</span>。即时间变为了更新时候的时间。（注意一个UPDATE设置一个列为它已经有的值，这将不引起TIMESTAMP列被更新，因为如果你设置一个列为它当前的值，MySQL为了效率而忽略更改。）</p>
<p class="post-body">如果有多个TIMESTAMP列，只有第一个自动更新。</p>
<p class="post-body"><br />#1创建一个有两个timestamp字段的表dj1。<br />root@localhost:test &gt;create table dj1 (a char(1), b timestamp ,c timestamp); <br />Query OK, 0 rows affected (0.01 sec)<br /><br />#2插入两行数据，仅赋值于列A<br />root@localhost:test &gt;insert into dj1 values (1,null,null);<br />Query OK, 1 row affected (0.00 sec)<br /><br />root@localhost:test &gt;insert into dj1 values (2,null,null); <br />Query OK, 1 row affected (0.00 sec)<br /><br />#3查询插入数据，b,c两列都使用current timestamp作为默认值。<br />root@localhost:test &gt;select * from dj1;<br />+------+---------------------+---------------------+<br />| a | b | c |<br />+------+---------------------+---------------------+<br />| 1 | 2009-09-09 13:48:40 | 2009-09-09 13:48:40 | <br />| 2 | 2009-09-09 13:48:44 | 2009-09-09 13:48:44 | <br />+------+---------------------+---------------------+<br />2 rows in set (0.00 sec)<br /><br />#4更新一行数据，发现b列timestamp被自动更新，而c列保持不变。<br />root@localhost:test &gt;update dj1 set a=9 where a=1; <br />Query OK, 1 row affected (0.00 sec)<br />Rows matched: 1 Changed: 1 Warnings: 0<br /><br />root@localhost:test &gt;select * from dj1;<br />+------+---------------------+---------------------+<br />| a | b | c |<br />+------+---------------------+---------------------+<br />| 9 | 2009-09-09 13:49:08 | 2009-09-09 13:48:40 | <br />| 2 | 2009-09-09 13:48:44 | 2009-09-09 13:48:44 | <br />+------+---------------------+---------------------+<br />2 rows in set (0.00 sec)<br /><br />#5再更新一列，仍然如#4<br />root@localhost:test &gt;update dj1 set a=8 where a=2; <br />Query OK, 1 row affected (0.00 sec)<br />Rows matched: 1 Changed: 1 Warnings: 0<br /><br />root@localhost:test &gt;select * from dj1;<br />+------+---------------------+---------------------+<br />| a | b | c |<br />+------+---------------------+---------------------+<br />| 9 | 2009-09-09 13:49:08 | 2009-09-09 13:48:40 | <br />| 8 | 2009-09-09 13:49:36 | 2009-09-09 13:48:44 | <br />+------+---------------------+---------------------+<br />2 rows in set (0.00 sec)<br /><br />#6在b列上创建唯一索引<br />root@localhost:test &gt;create unique index dj1_idx_u1 on dj1(b);<br />Query OK, 2 rows affected (0.01 sec)<br />Records: 2 Duplicates: 0 Warnings: 0<br /><br />#7更新所有行a列，报唯一性冲突。<br />root@localhost:test &gt;update dj1 set a=1;<br />ERROR 1062 (23000): Duplicate entry '2009-09-09 13:54:45' for key 'dj1_idx_u1'<br /><br />#8查看表定义，可以看到b列有个属性ON UPDATE CURRENT_TIMESTAMP，导致更新数据时，即便未涉及到该列，该列数据也被自动更新。<br />另一方面，c列默认值是'0000-00-00 00:00:00'，实际插入已经被自动赋值为current_timestamp。<br />root@localhost:test &gt;show create table dj1\G<br />*************************** 1. row ***************************<br />Table: dj1<br />Create Table: CREATE TABLE `dj1` (<br />`a` char(1) COLLATE utf8_bin DEFAULT NULL,<br />`b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,<br />`c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',<br />UNIQUE KEY `dj1_idx_u1` (`b`)<br />) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin<br />1 row in set (0.00 sec)<br /><br />#9创建表dj2，列b不带自动更新属性。<br />root@localhost:test &gt;CREATE TABLE `dj2` (<br />-&gt; `a` char(1) COLLATE utf8_bin DEFAULT NULL,<br />-&gt; `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,<br />-&gt; `c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',<br />-&gt; UNIQUE KEY `dj1_idx_u1` (`b`)<br />-&gt; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;<br />Query OK, 0 rows affected (0.01 sec)<br /><br />#10插入dj2测试数据<br />root@localhost:test &gt;insert into dj2 values (1,null,null); <br />Query OK, 1 row affected (0.00 sec)<br /><br />root@localhost:test &gt;insert into dj2 values (2,null,null); <br />Query OK, 1 row affected (0.00 sec)<br /><br />#11查看dj2数据<br />root@localhost:test &gt;select * from dj2;<br />+------+---------------------+---------------------+<br />| a | b | c |<br />+------+---------------------+---------------------+<br />| 1 | 2009-09-09 14:02:55 | 2009-09-09 14:02:55 | <br />| 2 | 2009-09-09 14:03:00 | 2009-09-09 14:03:00 | <br />+------+---------------------+---------------------+<br />2 rows in set (0.00 sec)<br /><br />#12dj2上创建唯一索引<br />root@localhost:test &gt;create unique index dj2_idx_u1 on dj2(b);<br />Query OK, 2 rows affected (0.02 sec)<br />Records: 2 Duplicates: 0 Warnings: 0<br /><br />#更新数据成功<br />root@localhost:test &gt;update dj2 set a=9;<br />Query OK, 2 rows affected (0.00 sec)<br />Rows matched: 2 Changed: 2 Warnings: 0<br /><br />root@localhost:test &gt;select * from dj2;<br />+------+---------------------+---------------------+<br />| a | b | c |<br />+------+---------------------+---------------------+<br />| 9 | 2009-09-09 14:02:55 | 2009-09-09 14:02:55 | <br />| 9 | 2009-09-09 14:03:00 | 2009-09-09 14:03:00 | <br />+------+---------------------+---------------------+<br />2 rows in set (0.00 sec)<br /><br />#13创建表dj3，b列默认值为CURRENT_TIMESTAMP，c列默认值为CURRENT_TIMESTAMP带自动更新属性，报错，不允许行为。<br />root@localhost:test &gt;CREATE TABLE `dj3` (<br />-&gt; `a` char(1) COLLATE utf8_bin DEFAULT NULL,<br />-&gt; `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,<br />-&gt; `c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,<br />-&gt; UNIQUE KEY `dj1_idx_u1` (`b`)<br />-&gt; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;<br />ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause<br /><br />#14创建表dj4，b列默认值为CURRENT_TIMESTAMP，c列默认值为'0000-00-00 00:00:00'带自动更新属性，报错，不允许行为。<br />root@localhost:test &gt;CREATE TABLE `dj4` (<br />-&gt; `a` char(1) COLLATE utf8_bin DEFAULT NULL,<br />-&gt; `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,<br />-&gt; `c` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,<br />-&gt; UNIQUE KEY `dj1_idx_u1` (`b`)<br />-&gt; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;<br />ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause<br /><br />#15创建表dj5，b列默认值为CURRENT_TIMESTAMP带自动更新属性，c列默认值为CURRENT_TIMESTAMP，报错，不允许行为。<br />root@localhost:test &gt;CREATE TABLE `dj5` (<br />-&gt; `a` char(1) COLLATE utf8_bin DEFAULT NULL,<br />-&gt; `b` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,<br />-&gt; `c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ,<br />-&gt; UNIQUE KEY `dj1_idx_u1` (`b`)<br />-&gt; ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;<br />ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause<br /><br /><font color="#ff0000">conclusion：<br />1.MySQL默认表的第一个timestamp字段为NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP属性，必须显式定义改变这种行为。<br />2.MySQL只允许一个timestamp字段拥有[DEFAULT CURRENT_TIMESTAMP |ON UPDATE CURRENT_TIMESTAMP]属性。 我的理解为要么都是DEFAULT CURRENT_TIMESTAMP 要么都是DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP</font></p>
<p class="post-body"><font color="#ff0000">3.修改字段属性值</font></p>
<div class="post-body"><font color="#ff0000">show create table tbl_ledgerrecord;</font></div>
<div class="post-body"><font color="#ff0000">alter table tbl_ledgerrecord change intoStorageDate&nbsp; intoStorageDate timestamp DEFAULT CURRENT_TIMESTAMP;</font></div></div></div><br /><br />转自:<a href="http://blog.chinaunix.net/uid-20577907-id-2213871.html">http://blog.chinaunix.net/uid-20577907-id-2213871.html</a> <img src ="http://www.cppblog.com/LIULIANG/aggbug/199951.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LIULIANG/" target="_blank">BIG森林</a> 2013-05-03 22:20 <a href="http://www.cppblog.com/LIULIANG/archive/2013/05/03/199951.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>