I want a MySQL table of mine to contain 2 timestamp columns, both set automatically without the client side help: one to be initialized once on insert:
`added` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
and another initialized the same on insert and updated on every update:
`updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
But this doesn't work this way:
[Err] 1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Is there a known workaround?
There is not just a workaround, there is a solution: Upgrade to MySQL 5.6.5 or higher and this is supported.
See: MySQL 5.6.6 TIMESTAMP columns and DEFAULT values
{edit} Since upgrading is not an option, you can make the first column a normal timestamp column and create a trigger that sets one timestamp when you insert the record. Then you can create the other colum with the DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, so it gets a timestamp on insertion and on update.
CREATE TRIGGER task_creation_timestamp BEFORE INSERT ON tasks
FOR EACH ROW
SET NEW.created = NOW();
I've stolen this trigger from this answer.
There is no "solution" as the error suggest, you can ONLY HAVE ONE TIMESTAMP per table (On previous versions of 5.6.6 as GolezTrol Suggested)
To workaround this i suggest you make of the "timestamps" a datetime and set the default to NOW() or CURRENT_TIMESTAMP() or any other synonym for NOW()
Related
I want to add at row "jailtime" timestamp. That timestamp would be the timestamp of when "pjailed" row was updated.
I tried to do the fallowing:
https://dba.stackexchange.com/questions/45470/get-the-time-of-last-update-of-a-column
But instead of creating table, I wanted to alter existing table, so I went for
ALTER TABLE `users`
CHANGE `jailtime` `jailtime` TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP NULL
DEFAULT 'updated_at pJailed';
But it gives me error "Invalid value for pJailed"
If the value for pJailed for a row is updated that whole row receives an update. So it would suffice to use ON UPDATE CURRENT_TIMESTAMP (and DEFAULT CURRENT_TIMESTAMP) on jailtime.
You can alter the column jailtime to reflect this using:
ALTER TABLE your_table
MODIFY COLUMN jailtime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Edit: OP mentioned in their comment that the attribute jailtime should only be updated when the attribute pJailed is updated. To accomplish this one should use a trigger as described here.
I am currently just inserting NOW() into the date field on my database which works just fine but it got me wondering, is there a way to automatically update a DATETIME row upon inserting data?
I found online in some places that I should set the extras to ON UPDATE CURRENT_TIMESTAMP but when doing this I get the following error:
An error occurred when trying to change the field 'uploaded' via
ALTER TABLE `uploads` CHANGE `uploaded` `uploaded` DATETIME
NOT NULL
ON UPDATE CURRENT_TIMESTAMP
COMMENT 'Upload Datetime'
MySQL said:
Invalid ON UPDATE clause for 'uploaded' column
This is what I do and it has always worked
create table if not exists my_table (
index1 char(32) not null primary key,
title varchar(50),
my_timestamp timestamp not null default current_timestamp on update current_timestamp
)
This will have the timestamp on inserts and on updates, it also is valid sql, maybe you are missing the default statement in your query?
It seems the issue was that the column needed to be a TIMESTAMP not DATETIME upon changing it, I was able to successfully add the CURRENT_TIMESTAMP argument.
Seems your query is wrong, try this out works for me:
ALTER TABLE `uploads` CHANGE `uploaded` `uploaded`
DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP;
Tested it.
Set the default value for the column upon table creation or alter it later.
In MySQL versions 5.6.5 and later:
default CURRENT_TIMESTAMP
In SQL Server:
default getdate()
In PostgreSQL:
default now()
Maybe this documentation can help you a bit at understanding how to auto update the datetime in a sql database
http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
Hope this helped you :)
For most SQL dialects, you can use a DATETIME column with a default of current_timestamp. That handles creation.
create table whatever (
...
updated datetime default current_timestamp
);
Then add an after update trigger.
create trigger foo_updated
after update
on foo
for each row
begin
update foo set updated = current_timestamp where id = new.id;
end;
MySQL has some special syntax to do this. You can make the trigger in your create table statement with an with an on update.
create table whatever (
...
updated datetime default current_timestamp on update current_timestamp
);
And then there's MySQL's timestamp date type. Timestamp will set itself to the current date and time anytime a row is updated... sorta.
The rules are very complicated which is why it's better to use datetime and on update.
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 a table with two timestamp fields. I simply defined them with a name and the type TIMESTAMP, yet for some reason MySQL automatically set one of them with a default value and the attribute on update CURRENT_TIMESTAMP. I was planning on having NO default value in either of the fields, but one of the fields is called "date_updated" so I suppose I could set the mentioned attribute to that field.
Unfortunately, it's the field "date_created" that was set with the on update CURRENT_TIMESTAMP attribute, and no matter what I do, MySQL won't let me remove it.
I've tried editing the "date_created" field and removing the attribute. When clicking save, the attribute is back. I have also tried selecting both fields, removing the attribute from one of them and setting it on the other. It gives me the error #1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause and suddenly both attribute columns on the values are set to on update CURRENT_TIMESTAMP the result:
Error
SQL query:
ALTER TABLE `pages` CHANGE `date_created` `date_created` TIMESTAMP NOT NULL ,
CHANGE `date_updated` `date_updated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
MySQL said:
#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
Must I really recreate both those columns in the correct order to fix this?
I would like to know how I could solve this problem correctly, for future reference.
Thanks
Now I've also tried to run
ALTER TABLE pages
CHANGE date_created
date_created TIMESTAMP NOT NULL
You should specify DEFAULT CURRENT_TIMESTAMP (or DEFAULT 0)
ALTER TABLE pages CHANGE date_created date_created TIMESTAMP NOT NULL DEFAULT 0,
CHANGE `date_updated` `date_updated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
As of MySQL version 5.6.6, you can use the explicit_defaults_for_timestamp option in the configuration file, therefore timestamp columns will not have 'DEFAULT CURRENT_TIMESTAMP' or 'ON UPDATE CURRENT_TIMESTAMP' attributes by default. It will also be possible so set these columns to NULL if they are not declared as NOT NULL.
See: http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp
I am trying to set my columns default date time to system datetime.
It shows me an error
Invalid default value for
'InsertionDate'
alter table `vts`.`tblpickpoint`
add column `InsertionDate`
datetime DEFAULT 'Now()' NULL after `PickPointLatLong`
The default value for a column in mysql cannot be the result of a function.
The one exception is the current_timestamp as astander points out.
Your statement should be
alter table `vts`.`tblpickpoint`
add column `InsertionDate` TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
Have a look at CURRENT_TIMESTAMP
If you want to init and update the value on every change, use this:
alter table `vts`.`tblpickpoint`
add column `InsertionDate`
TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
after `PickPointLatLong`
If you only want the creation time, use this:
alter table `vts`.`tblpickpoint`
add column `InsertionDate`
TIMESTAMP DEFAULT CURRENT_TIMESTAMP
after `PickPointLatLong`