I don't think you are supposed to use DEFAULT (constant)
with ON UPDATE
.
According to the MySQL Docuementation
With an ON UPDATE CURRENT_TIMESTAMP
clause and a constant DEFAULT
clause, the column is automatically updated to the current timestamp
and has the given constant default value.
CREATE TABLE t1
(
ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP
);
This might just be stomping over with the current datetime.
On the same page, it says
With an ON UPDATE CURRENT_TIMESTAMP
clause but no DEFAULT
clause, the column is automatically updated to the current timestamp. The default is 0 unless the column is defined with the NULL attribute, in which case the default is NULL.
CREATE TABLE t1
(
ts TIMESTAMP ON UPDATE CURRENT_TIMESTAMP -- default 0
);
CREATE TABLE t2
(
ts TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL
);
I suggest just removing the DEFAULT '0000-00-00 00:00:00'
Give it a Try !!!
This is the default behaviour of the (first) timestamp
column. Many more details can be read in the documentation: Automatic Initialization and Updating for TIMESTAMP
.
If you don't want this, you have to explicitly tell MySQL when you create the table. You can declare a default value (or default null) and the automatic properties will be supressed. Any of the following will do (with differences in behaviour explained):
Option 1: not null
and default 0
brought_in timestamp NOT NULL DEFAULT 0, -- option 1a
brought_in timestamp DEFAULT 0, -- option 1b
These two options are identical. The column is defined as NOT NULL
and has a default value of 0
. Note that TIMESTAMP
is the only datatype that has a default property of NOT NULL
, in contrast with all other datatypes and with the ISO/ANSI standard.
If you try to insert NULL
in the column, the CURRENT_TIMESTAMP
is inserted (surprise, another MySQL feature.)
Option 2: null
and default null
brought_in timestamp NULL DEFAULT NULL, -- option 2a
brought_in timestamp NULL, -- option 2b
These are identical, they allow NULL
(and both result in default being NULL
as well.)
If you try to insert NULL
in the column, it is allowed and inserted fine (no surprises here.)
Option 3: null
and default 0
or some other constant
brought_in timestamp NULL DEFAULT 0, -- option 3a
brought_in timestamp NULL DEFAULT '2013-01-01', -- option 3b
This is similar to option 2 but has a different default value.
If you try to insert NULL
in the column, it is allowed and inserted fine, too (no surprises here either.)
Best Answer
A unique timestamp is a mistake waiting to happen.
If there is also an
AUTO_INCREMENT id
, you can do this:Then dups are not possible.