here can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT - mysql

I want to mofidy a TIMESTAMP column to take the CURRENT_TIMESTAMP as default value, and the CURRENT_TIMESTAMP when I modify it.
this is the script I tried :
ALTER TABLE annonce MODIFY COLUMN date_modif TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
but I'm getting this error message :
1293 - Incorrect table definition; there can be only one TIMESTAMP
column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
I have also another column (date_crea) which has CURRENT_TIMESTAMP as the default value.
Is that means that I can only use the value CURRENT_TIMESTAMP in one column ??
if it's how can I solve that ?

Is that means that I can only use the value CURRENT_TIMESTAMP in one
column ??
Yes, that is the fact. there is no way to fix that.
but you may want to use an after-insert trigger and a after-update trigger to set the time yourself.
read http://dev.mysql.com/doc/refman/5.7/en/create-trigger.html.
CREATE TRIGGER trigger_name AFTER INSERT ON tbl_name FOR EACH ROW set my_second_currenttime = now();
CREATE TRIGGER trigger_name AFTER UPDATE ON tbl_name FOR EACH ROW set my_second_currenttime = now();
untested in the real world - just a code example

Related

Explaining mysql timestamps of specific row

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.

Auto update DATETIME column on insert with SQL?

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.

How to create 2 auto-set timestamp columns in a MySQL table?

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()

How to set default date time as system date time in mysql

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`

Using Now() as a default for MySql DateTime type

How can I setup a MySql schema with a DateTime column such that its value will always be the time of the last insert/update to this row?
You can the TIMESTAMP instead and set the ON UPDATE CURRENT_TIMESTAMP NOT NULL, for example:
CREATE TABLE example (
lastUpdate TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
);
Anax was close, but missed the default value.
CREATE TABLE example (
lastUpdate TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
)
Like Anax said, using TIMESTAMP instead of DATETIME will give you a column that uses the current time instead of waiting for a provided date/time.
You have a lot of options with TIMESTAMP, the two that are most important are:
Having it use the current time ( NOW() ) when the row is created,
Having it modify the column to the current time when the row is modified.
If you don't set any options, like so:
CREATE TABLE stuff (ts TIMESTAMP);
It will put in the current time on creation and change that column on modification.
So if you want a column that only shows when it was created, you would use:
CREATE TABLE stuff (ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
Since you have not specified an ON UPDATE clause, it will not do anything if the row is updated.
If you are insane, like me, you'll want both, and go with:
CREATE TABLE stuff (create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
mod_date TIMESTAMP
);
and since I haven't written this out yet, that last one is equivalent to writing out:
CREATE TABLE stuff (create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
mod_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP
);
You could do it with triggers:
DELIMITER $$
CREATE TRIGGER `before_update` BEFORE UPDATE ON `table_name` FOR EACH ROW BEGIN
SET NEW.update_date = NOW();
END;
$$
CREATE TRIGGER `before_insert` BEFORE INSERT ON `table_name` FOR EACH ROW BEGIN
SET NEW.update_date = NOW();
END;
$$
DELIMITER ;