免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6173 | 回复: 12
打印 上一主题 下一主题

mysql 中TIMESTAMP 类型的问题。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-16 14:51 |只看该作者 |倒序浏览
2可用积分
我数据库中有个TIMESTAMP 类型字段许可为NULL,更新时出了个问题。因为我不想更新这个字段所以更新没有给它赋值。但是如果有2条以么一样的SQL第1条时TIMESTAMP 类型字段会自动更新,但是第2条TIMESTAMP 类型字段就没有自动更新。请问为什么?先谢谢了

最佳答案

查看完整内容

其实问题没有想象的那么简单:各位查过手册没有?

评分

参与人数 1可用积分 -2 信誉积分 -8 收起 理由
yueliangdao0608 -2 -8 违反版规

查看全部评分

论坛徽章:
0
2 [报告]
发表于 2008-06-16 14:51 |只看该作者
其实问题没有想象的那么简单:
To specify automatic default or updating for a TIMESTAMP column other than the first one, you must suppress the automatic initialization and update behaviors for the first TIMESTAMP column by explicitly assigning it a constant DEFAULT value (for example, DEFAULT 0 or DEFAULT '2003-01-01 00:00:00'). Then for the other TIMESTAMP column, the rules are the same as for the first TIMESTAMP column, except that if you omit both of the DEFAULT and ON UPDATE clauses, no automatic initialization or updating occurs.

Example. These statements are equivalent:

CREATE TABLE t (
    ts1 TIMESTAMP DEFAULT 0,
    ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                  ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t (
    ts1 TIMESTAMP DEFAULT 0,
    ts2 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
                  DEFAULT CURRENT_TIMESTAMP);

Beginning with MySQL 4.1.3, you can set the current time zone on a per-connection basis, as described in Section 5.10.8, “MySQL Server Time Zone Support”. TIMESTAMP values still are stored in UTC, but are converted from the current time zone for storage, and converted back to the current time zone for retrieval. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different than the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable.

Beginning with MySQL 4.1.6, you can include the NULL attribute in the definition of a TIMESTAMP column to allow the column to contain NULL values. For example:

CREATE TABLE t (
  ts1 TIMESTAMP NULL DEFAULT NULL,
  ts2 TIMESTAMP NULL DEFAULT 0,
  ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);

Before MySQL 4.1.6 (and even as of 4.1.6 if the NULL attribute is not specified), setting the column to NULL sets it to the current timestamp. Note that a TIMESTAMP column which allows NULL values not take on the current timestamp except under one of the following conditions:

Its default value is defined as CURRENT_TIMESTAMP

NOW() or CURRENT_TIMESTAMP is inserted into the column

In other words, a TIMESTAMP column defined as NULL will auto-initialize only if it is created using a definition such as the following:

CREATE TABLE t (ts TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP);

Otherwise — that is, if the TIMESTAMP column is defined to allow NULL values but not using DEFAULT TIMESTAMP, as shown here…

CREATE TABLE t1 (ts TIMESTAMP NULL DEFAULT NULL);
CREATE TABLE t2 (ts TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00');

…then you must explicitly insert a value corresponding to the current date and time, for example:

INSERT INTO t1 VALUES (NOW());
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);

·         要为TIMESTAMP列而不是第1列指定自动默认或更新,必须通过将第1个TIMESTAMP列显式分配一个常量DEFAULT值来禁用自动初始化和更新。(例如,DEFAULT 0或DEFAULT'2003-01-01 00:00:00')。然后,对于其它TIMESTAMP列,规则与第1个TIMESTAMP列相同,例外情况是不能忽略DEFAULT和ON UPDATE子句。如果这样做,则不会自动进行初始化或更新。

例如:下面这些语句是等效的:

CREATE TABLE t (
    ts1 TIMESTAMP DEFAULT 0,
    ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                  ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t (
    ts1 TIMESTAMP DEFAULT 0,
    ts2 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
                  DEFAULT CURRENT_TIMESTAMP);
可以对每个连接设置当前的时区,相关描述参见5.10.8节,“MySQL服务器时区支持”。TIMESTAMP值以UTC格式保存,存储时对当前的时区进行转换,检索时再转换回当前的时区。只要时区设定值为常量,便可以得到保存时的值。如果保存一个TIMESTAMP值,应更改时区然后检索该值,它与你保存的值不同。这是因为在两个方向的转换中没有使用相同的时区。当前的时区可以用作time_zone系统变量的值。

可以在TIMESTAMP列的定义中包括NULL属性以允许列包含NULL值。例如:

CREATE TABLE t
(
  ts1 TIMESTAMP NULL DEFAULT NULL,
  ts2 TIMESTAMP NULL DEFAULT 0,
  ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);
如果未指定NULL属性,将列设置为NULL设置则会将它设置为当前的时间戳。请注意允许NULL值的TIMESTAMP列不会采用当前的时间戳,除非要么其 默认值定义为CURRENT_TIMESTAMP,或者NOW()或CURRENT_TIMESTAMP被插入到该列内。换句话说,只有使用如下定义创建,定义为 NULL的TIMESTAMP列才会自动更新:

CREATE TABLE t (ts NULLDEFAULT CURRENT_TIMESTAMP);
否则-也就是说,如果使用NULL而不是DEFAULT TIMESTAMP来定义TIMESTAMP列,如下所示...

CREATE TABLE t1 (ts NULL DEFAULT NULL);
CREATE TABLE t2 (ts NULL DEFAULT '0000-00-00 00:00:00');
...则必须显式插入一个对应当前日期和时间的值。例如:

INSERT INTO t1 VALUES (NOW());
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);


各位查过手册没有?

论坛徽章:
0
3 [报告]
发表于 2008-06-16 15:26 |只看该作者
There is only one timestamp column defined with default value.

论坛徽章:
0
4 [报告]
发表于 2008-06-16 15:32 |只看该作者
原帖由 yueliangdao0608 于 2008-6-16 15:26 发表
There is only one timestamp column defined with default value.

斑竹的意思是timestamp 只能是一种属性?那为什么不事每次更新都发生变化那?

论坛徽章:
0
5 [报告]
发表于 2008-06-16 17:25 |只看该作者
原帖由 Catal 于 2008-6-16 15:32 发表

斑竹的意思是timestamp 只能是一种属性?那为什么不事每次更新都发生变化那?



I'm sorry.
I mean there is only one timestamp column in a table and its default value is current_timestamp.

[ 本帖最后由 yueliangdao0608 于 2008-6-16 20:58 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2008-06-16 20:59 |只看该作者

论坛徽章:
0
7 [报告]
发表于 2008-06-17 10:41 |只看该作者
通过适当的表结构定义,完全可以做到同表两个或以上TIMESTAMP列同时自动更新,或同时不自动更新。

论坛徽章:
0
8 [报告]
发表于 2008-06-17 10:53 |只看该作者
至于楼主的问题,因为没说到底是希望能自动更新还是不希望,所以就自己参照上面的改一下表定义吧

论坛徽章:
0
9 [报告]
发表于 2008-06-17 15:04 |只看该作者
原帖由 sunnyfun 于 2008-6-17 10:41 发表
通过适当的表结构定义,完全可以做到同表两个或以上TIMESTAMP列同时自动更新,或同时不自动更新。



手册是对的,LZ的意思是说要靠表本身来实现两个列的自动时间更新。这个是不可能的。

必须手动插入,因为现在也就只能有一列可以定义为自动更新。

论坛徽章:
0
10 [报告]
发表于 2008-06-18 12:59 |只看该作者
原帖由 yueliangdao0608 于 2008-6-17 15:04 发表



手册是对的,LZ的意思是说要靠表本身来实现两个列的自动时间更新。这个是不可能的。

必须手动插入,因为现在也就只能有一列可以定义为自动更新。


mysql> CREATE TABLE `tt` (
    ->   `cc` timestamp NOT NULL default '0000-00-00 00:00:00',
    ->   `dd` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> SELECT * FROM `tt`;
Empty set (0.00 sec)

mysql>
mysql> INSERT INTO `tt` ( `cc` , `dd` ) VALUES (NULL , NULL);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> SELECT * FROM `tt`;
+---------------------+---------------------+
| cc                  | dd                  |
+---------------------+---------------------+
| 2008-06-18 12:57:17 | 2008-06-18 12:57:17 |
+---------------------+---------------------+
1 row in set (0.00 sec)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP