Update mysql row or column data with current datetime? - mysql

Here is my table structure below, How can I update my Num_pages column data so that it will insert with newly updated DateTime, rather than the old one.

You can alter your Date_Time_Acqure field as following:
ALTER TABLE mytablename
CHANGE COLUMN `Date_Time_Acqured` `Date_Time_Acqured` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ;
The column then will update to current timestamp whenever there's any change on the row; means that if you change any value from column title and not just Num_Pages, it will also update the Date_Time_Acqure to a current time.

Related

Cannot alter a timestamp column to have a default value as current_timestamp

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

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.

Touch MYSQL record to update TIMESTAMP field

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 ...

Can I amend the properties of a column in a table after the fact

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.

How to add "ON update current timestamp" to existing table column

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.