I have a table with two timestamp fields. They are not nullable. The problem is that whenever I insert null into those fields, the current date is automatically saved, instead of throwing an error saying "Column 'first_data_dt' cannot be null", just like it happens when I insert a value into another non-nullable field.
There are no triggers associated to this table.
Does anybody know why this is happening?
EDIT to add table definition:
CREATE TABLE `ui_mytable` (
`id` int(11) NOT NULL,
`first_data_dt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`last_data_dt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I understand now why first_data_dt is updated to the current timestamp anytime I insert null. But what about last_data_dt?
That's what a TIMESTAMP column does:
The TIMESTAMP data type offers automatic initialization and updating to the current date and time (that is, the current timestamp). [...] 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.
Source: MySQL documentation
Maybe you want to use a DATETIME instead?
Related
I'm using the Sequel Pro app to work with my db.
Using Mysql 5.7.
I have the following table structure. When I attempt to reorder the 'created' table column, I get an error from mysql "Invalid default value for 'created'.
I have no rows in the materials table when attempting the reorder.
From everything I've read, CURRENT_TIMESTAMP is the correct default value.
CREATE TABLE `materials` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
My goal is to have the created column be automatically filled when the row is added. modified will automatically update to the current time when changed.
What am I missing?
CURRENT_TIMESTAMP is only acceptable on TIMESTAMP fields. DATETIME fields must be left either with a null default value, or no default value at all - default values must be a constant value, not the result of an expression.
relevant docs: http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
You can work around this by setting a post-insert trigger on the table to fill in a "now" value on any new records.
refer :Invalid default value for 'dateAdded'
Creating a database table in MySQL. I have created two fields to grab timestamps.
created_at timestamp default '0000-00-00 00:00:00',
updated_at timestamp default now() on update now(),
When I update the database, both fields are updating to the current timestamp. Any thoughts on how to prevent this from happening? I am not providing the 'created_at' field when I update -- I am also providing 'null' for the updated_at field to auto update.
Depending on the version of MySQL you should be able to use the following for your default value on the updated_at field:
CURRENT_TIMESTAMP on UPDATE CURRENT_TIMESTAMP
Your created_at field should just have CURRENT_TIMESTAMP as the default.
I would also set both to the datatype datetime instead of timestamp.
Timestamp initialisation can be an ugly beast in mysql!
Based on the commment below you have mysql v5.6.4 or earlier:
I can't have two current_timestamps declared without getting an error -- and although it's default is 0 -- it is producing a current timestamp upon creation(without me feeding in the timestamp) – Spencer Rohan
You can have a single timestamp field that are initialised and/or updated to the current timestamp when you insert / update your record. In earlier versions this had to be the 1st timestamp field in the table.
I would simply set the created_at to be a nullable column with a default value of null because according to mysql documentation on timestamp initialization:
In other words, a TIMESTAMP column defined to permit NULL values auto-initializes only if its definition includes DEFAULT CURRENT_TIMESTAMP
By changing the default value to null you go around any problems you may have with zero dates and various sql modes.
created_at timestamp null default null,
I have this table with newspost, i want to add timestamps on date added, and i want to update another col when the post is edited. I would like it to happen automaticly in MySql. without the use of any PHP code.
CREATE TABLE IF NOT EXISTS news (
id int(11) NOT NULL AUTO_INCREMENT,
data text,
date_published timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
date_edited timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
What's the best practice in a case like this?
Consider using triggers. From the MySQL docs:
A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.
Example of a trigger:
CREATE TRIGGER trigger_example AFTER UPDATE ON news
FOR EACH ROW UPDATE some_table SET another_column = NEW.data;
You can only use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP features on one timestamp column in a table.
Use the following table definition to turn off the features for date_published and use them for date_edited:
CREATE TABLE IF NOT EXISTS news (
id INT(11) NOT NULL AUTO_INCREMENT,
data TEXT,
date_published TIMESTAMP NOT NULL DEFAULT 0,
date_edited TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
When inserting a new row, pass a NULL value for date_published to automatically assign the current timestamp to that column.
MySQL Docs on Automatic Initialization and Updating for TIMESTAMP:
It need not be the first TIMESTAMP column in a table that is
automatically initialized or updated to the current timestamp.
However, to specify automatic initialization or updating for a
different TIMESTAMP column, you must suppress the automatic properties
for the first one. Then, for the other TIMESTAMP column, the rules for
the DEFAULT and ON UPDATE clauses are the same as for the first
TIMESTAMP column, except that if you omit both clauses, no automatic
initialization or updating occurs.
To suppress automatic properties for the first TIMESTAMP column, do
either of the following:
Define the column with a DEFAULT clause that specifies a constant default value.
Specify the NULL attribute. This also causes the column to permit NULL values, which means that you cannot assign the current timestamp by setting the column to NULL. Assigning NULL sets the column to NULL.
Following is my SQL query, it throws an error:-
CREATE TABLE IF NOT EXISTS USER_PROFILE(Id INT PRIMARY KEY AUTO_INCREMENT, date DATETIME NOT NULL DEFAULT NOW) ;
It says Invalid default value for 'date'.
I've tried synonyms for NOW() as well, namely CURRENT_TIMESTAMP, but still the same error.
How can I create a column date with default value current time?
On the documentation page, it says to assign this way
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
dt DATETIME DEFAULT CURRENT_TIMESTAMP
);
From the document
The DEFAULT value clause in a data type specification indicates a
default value for a column. With one exception, the default value must
be a constant; it cannot be a function or an expression. This means,
for example, that you cannot set the default for a date column to be
the value of a function such as NOW() or CURRENT_DATE. The exception
is that you can specify CURRENT_TIMESTAMP as the default for TIMESTAMP
and DATETIME columns
So no function is allowed in the default value hence the first query is failing.
Again from the document
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically
initializated and updated to the current date and time (that is, the
current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and
for at most one TIMESTAMP column per table. The following notes first
describe automatic initialization and updating for MySQL 5.6.5 and up,
then the differences for versions preceding 5.6.5.
Before 5.6.5, this is true only for TIMESTAMP
So your mysql version is less than 5.6.5 hence the 2nd query is failing too.
So you need to create the table as
CREATE TABLE IF NOT EXISTS
USER_PROFILE
(
Id INT PRIMARY KEY AUTO_INCREMENT,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ;
It might be that DATE, as a reserved word, is confusing it by the time it gets to the DEFAULT clause. Try a different name and if that works, try quoting "date".
brief:
I'm experiencing strange MySQL behavior - on update CURRENT_TIMESTAMP attribute is being added although I don't want it to be added. I want to find out why is this happening - is it a matter of MySQL server or MySQL Workbench I'm using (v5.2.38).
detail:
I've modelled the database structure using EER diagrams, an example table is below:
CREATE TABLE IF NOT EXISTS `privilege` (
`id` INT NOT NULL ,
`name` VARCHAR(64) NOT NULL COMMENT 'privilege name (just a label)' ,
`created_at` TIMESTAMP NOT NULL COMMENT 'when the privilege was created' ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
The above script fragment is created using Export > Forward Engineer SQL Create Script inside WorkBench. The created_at column is siginifant here. It is NOT NULL and there is no default value defined for the timestamp when a record is defined. So I guess, that if someone tries to insert a record without defining created_at, an error will be raised.
I run this script inside MySQL server to create the whole structure. And the created structure is different - show create table privilege returns the following:
CREATE TABLE `privilege` (
`id` int(11) NOT NULL,
`name` varchar(64) NOT NULL COMMENT 'privilege name (just a label)',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP COMMENT 'when the privilege was created',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Where did "on update current_timestamp" come from? I'm 100% sure that I didn't choose any appropriate option, so MySQL should not create anything he's not asked to.
Does anyone have an idea why those clauses are added?
As documented under Automatic Initialization and Updating for TIMESTAMP:
The following rules describe the possibilities for defining the first TIMESTAMP column in a table with the current timestamp for both the default and auto-update values, for one but not the other, or for neither:
[ deletia ]
With neither DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP, it is the same as specifying both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP.
[ deletia ]
To suppress automatic properties for the first TIMESTAMP column, do either of the following:
Define the column with a DEFAULT clause that specifies a constant default value.
Specify the NULL attribute. This also causes the column to permit NULL values, which means that you cannot assign the current timestamp by setting the column to NULL. Assigning NULL sets the column to NULL.