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