将NULL插入MySQL时间戳 [英] Inserting NULL into MySQL timestamp

查看:169
本文介绍了将NULL插入MySQL时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观察到在MySQL时间戳列中插入/更新NULL的意外行为。



请考虑以下语句。

  create table temp(id int,hireDate timestamp default 0); 
insert into temp(id)values(1);
insert into temp(id,hireDate)values(2,null);
select * from temp;
id hireDate
-----------------------------
1
2 2012 -09-19 10:54:10.0

第一次插入( hiredate c> $

$ <$ p



注意:Hibernate使用第二种类型的插入和因此它成为一个问题如何在时间戳列中插入null?

解决方案

http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html


此外,您可以通过为其指定NULL值来初始化或更新任何TIMESTAMP列到当前日期和时间,除非它已使用NULL属性定义为允许NULL值。



I'm observing unexpected behavior with inserting/updating NULL in a MySQL timestamp column.

Consider the following statements.

create table temp(id int, hireDate timestamp default 0);
insert into temp (id) values(1);
insert into temp (id, hireDate) values(2, null);
select * from temp;
id  hireDate
-----------------------------
1   
2   2012-09-19 10:54:10.0

The first insert (when hiredate is not specified in the SQL, hireDate is null(0) which is expected.

However when an explicit null passed in SQL, the current date time is inserted which is unexpected. Why does this happen?

Note: Hibernate uses second type of insert and hence it become an issue. How do I insert null into a timestamp column?

解决方案

http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html

In addition, you can initialize or update any TIMESTAMP column to the current date and time by assigning it a NULL value, unless it has been defined with the NULL attribute to permit NULL values.

这篇关于将NULL插入MySQL时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆