Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting NULL into MySQL timestamp

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?

like image 337
Rejeev Divakaran Avatar asked Sep 19 '12 05:09

Rejeev Divakaran


People also ask

Can we insert NULL in date in MySQL?

In MySQL, we can insert current date and time automatically to a column on inserting the NULL values in other columns by declaring that column as DEFAULT CURRENT_TIMESTAMP. In this case, we cannot declare the column NOT NULL in which we want to insert NULL values.

How do you NULL in MySQL?

Let's look at an example of how to use MySQL IS NULL in a SELECT statement: SELECT * FROM contacts WHERE last_name IS NULL; This MySQL IS NULL example will return all records from the contacts table where the last_name contains a NULL value.

How do you insert a NULL?

You also can specify the NULL keyword in the VALUES clause to indicate that a column should be assigned a NULL value. The following example inserts values into three columns of the orders table: INSERT INTO orders (orders_num, order_date, customer_num) VALUES (0, NULL, 123);

How do I create a TIMESTAMP in MySQL?

MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.


2 Answers

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.

In order to allow a TIMESTAMP to be nullable, create it using the NULL attribute, or alter the table and add the NULL attribute. In a create statement, it would resemble.

CREATE TABLE t1 (tsvalue TIMESTAMP NULL, ... ); 

Inserting a NULL value into a TIMESTAMP field with the NULL attribute set will result in the field being set to NULL instead of to NOW().

like image 194
Juris Malinens Avatar answered Sep 21 '22 16:09

Juris Malinens


You can insert and update NULL into a MySQL timestamp.

Create the table:

create table penguins(id int primary key auto_increment, the_time timestamp NULL); 

Insert some rows:

insert into penguins(the_time) values (now()); insert into penguins(the_time) values (null); insert into penguins(the_time) values (0); insert into penguins(the_time) values ('1999-10-10 01:02:03'); 

Which prints:

select * from penguins  1   2015-01-23 15:40:36 2    3   0000-00-00 00:00:00 4   1999-10-10 01:02:03 

The column called the_time with datatype: timestamp in 2nd row has the value NULL.

like image 24
Eric Leschinski Avatar answered Sep 20 '22 16:09

Eric Leschinski