In my Database i have a table with column
`LastUpdated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
i would like to Touch a record in that table so that LastUpdated column will be automatically updated but i don't want to change any value in that row.
Is that possible?
Thank you.
AFAIK, You don't have options to use touch to update mysql table records like you touch file in unix systems. You have to issue an update query to update the timestamp in the LastUpdated column .
UPDATE mytable SET LastUpdated=NOW() WHERE ...
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 need to alter a column with timestamp datatype. The thing is when record is inserted the current time stamp should be inserted in that column. I know there is ON UPDATE CURRENT_TIMESTAMP but for insertion i can't find a way.
What you're looking for is the DEFAULT keyword.
ALTER TABLE yourTable MODIFY COLUMN yourColumn timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
What Rock 'Em wanted was to be told to use CURRENT_TIMESTAMP as the default for the column. Not on update. I use this all of the time. That way when a record gets inserted you know exactly when it happened. It is good practice in many tables to do this for audit tracking, etc.
Simply use DEFAULT clause, ignoring ON UPDATE clause, that's it should work for you!
ALTER TABLE yourTable MODIFY COLUMN yourColumn timestamp DEFAULT CURRENT_TIMESTAMP
I have a Timestamp columnn in a MySQL table. The default value of this column is CURRENT_TIMESTAMP, I want to make the ON UPDATE property of this column also equal to CURRENT_TIMESTAMP without having to drop and re-add the column, as this would cause me to lose all of the data.
Is this possible?
You ought to be able to do this successfully with an ALTER statement:
ALTER TABLE yourtable
MODIFY the_timestamp_column TIMESTAMP
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
It works correctly in my quick testing.
I have a column, say ts_activity, with datatype timestamp in MYSQL and default value to current timestamp. I want to add on update apply current time stamp to this column value.
I am not able to get alter query to do this. While create new table and its column I can add that, but not able to modify existing column by adding on update apply current timestamp.
Can someone tell me exact query to handle this? Can we alter this after creating table?
Devart's suggestion will work, but I prefer to use MODIFY COLUMN instead of CHANGE COLUMN if I'm not renaming the column.
I also assume that your ts_activity column is not nullable since you have a default, so I am setting it to NOT NULL, but that's up to you.
This is the statement I would use:
ALTER TABLE your_table
MODIFY COLUMN ts_activity TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Try this query -
ALTER TABLE table_name
CHANGE COLUMN column_name column_name TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Note, in the table only one field can be with 'ON UPDATE CURRENT_TIMESTAMP' option.