I've two columns in my tables in mysql db:
1. ins_ts
2. upd_ts
1st column should get a default timestamp whenever a row is inserted
2nd column should be updated with the default current timestamp each time a row is updated.
So I need both column to have a default value.
That's what I'm trying to do:
alter table test MODIFY ins_ts timestamp DEFAULT CURRENT_TIMESTAMP;
alter table test MODIFY upd_ts timestamp ON UPDATE CURRENT_TIMESTAMP;
1st works but 2nd one gives me following error:
Error Code: 1293. Incorrect table definition; there can be only one TIMESTAMP column
with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
I had read somewhere that mysql now supports two default timestamp columns, but here it doesn't works.
Please suggest is there any way to achieve the same functionality.
Thanks
Related
Basically I want to add a new updatedTime column so that it auto set to current_timestamp on updating and creating. Also I want to set the updatedTime column to null for existing rows.
So I try something like below:
Add new column and make it default to null
Set the new column default to current_timestamp afterward
alter table A
add column updatedTime timestamp null
default null on update current_timestamp;
alter table A
alter column updatedTime set default current_timestamp;
The reason is because my table is huge around 7 million rows and it's on live database as well so I try to optimise my query. By doing it this way, I just need to loop through the whole table once for the first query. The second query will only change the table config file.
So I am able to run the first query but the second one got syntax error.
EDIT:
My database is mysql server 5.7
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 want to add a column to a table to keep track of when each record was last updated. I already have some data in the table.
The SQL query I am running is this:
ALTER TABLE `my_table` ADD COLUMN `last_modified` timestamp NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP`;
When adding columns of other types existent records get the default value I specify for that column. However in this case all existent records are getting assigned the value of: 0000-00-00 00:00:00
Why is this happening if I explicitly said I wanted the default to be the current timestamp?
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()
Is it possible to define a timestamp column in a MySQL table that will automatically be updated every time a field in the same row is modified? Ideally this column should initially be set to the time a row was inserted.
Cheers,
Don
You can use the timestamp column as other posters mentioned. Here is the SQL you can use to add the column in:
ALTER TABLE `table1` ADD `lastUpdated` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ;
This adds a column called 'lastUpdated' with a default value of the current date/time. When that record is updated (lets say 5 minutes later) that timestamp will automatically update to the current time.
That is the default functionality of the timestamp column type. However, note that the format of this type is yyyymmddhhmmss (all digits, no colons or other separation).
EDIT: The above comment about the format is only true for versions of MySQL < 4.1... Later versions format it like a DateTime
This is what I have observed (MySql 5.7.11) -
The first TIMESTAMP column in the table gets current timestamp as the default value. So, if you do an INSERT or UPDATE without supplying a value, the column will get the current timestamp.
Any subsequent TIMESTAMP columns should have a default value explicitly defined. If you have two TIMESTAMP columns and if you don't specify a default value for the second column, you will get this error while trying to create the table -
ERROR 1067 (42000): Invalid default value for 'COLUMN_NAME'
A MySQL timestamp is set with creation or update time only if their default value is set as it. ALTER TABLE some_table ADD when TIMESTAMP DEFAULT CURRENT_TIMESTAMP.
Otherwise it works just like a DateTime field, only that it's relative to 1970/01/01 UTC, so it's an absolute point in time not depending on a specific timezone as is DateTime.