MYSQL timestamp column auto update even if there is no changes? - mysql

I have a column update_date in a table and type is timestamp. I set the deault value by using phpmyadmin drop down menu to CURRENT_TIMESTAMP. But later when ever I run sql UPDATE x SET ...
it updates the timestamp column if only there is a changes in any of the columns. What I would like to achieve is that whether there is a change or not set the current time everytime the update sql runs. Is there any way of doing it in the mysql or I need to set the update_date explicitly every time the update is called?
Thank you

You need to explicitly update the column. From the MySQL manual, TIMESTAMP properties:
The auto-update TIMESTAMP column, if there is one, is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. If all other columns are set to their current values, the TIMESTAMP column does not change. Automatic updating does not apply if the TIMESTAMP column is explicitly assigned a value other than NULL.
Emphasis mine.

Related

MySQL timestamp field default CURRENT_TIMESTAMP not working for existing rows

In the server, I have MySQL version:
5.1.61
It has a table called test with 10 columns and 10K rows.
Now I have decided to add a new column
ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
New column ts is added. but the problem is for existing rows this new column(ts)'s value is 00:000:000 Not current time.
PS: When I add new rows or update any existing row then the ts is updated with current time stamp.
why is default current_timestamp/now() not working for existing rows?
Edited:
I can run a simple update SQL to update existing rows to the current time. But I am new in the database and I am trying to know if it is possible to update existing rows with a default value.
The DEFAULT clause in a MySQL table only concerns what happens when new records get added to the table. There is no legacy behavior where the DBMS goes back to already existing records and applies some default value. In this case, if you want the already existing records to bear the current timestamp, you may do a blanket update:
UPDATE yourTable
SET ts = CURRENT_TIMESTAMP;
After this point, when you add new records and do not specify a value for the ts column, it will be assigned CURRENT_TIMESTAMP.

How do I prevent timestamp columns from updating when a MySQL row is updated?

I have a users table that has a column for the date that they joined. It is a timestamp datatype. I want the value of the column to always be the current time when the user joined, so the default value is the current time. If the user's profile is updated, the user's join date is updated to the current time. In other words, say a user joins on August 2nd and then updates their profile on September 17th. The new value of the join date will be September 17th. This happens even though I don't tell my sql statements to update that row.
How can I make it so that the timestamp column is only affected when a new row is created?
Sounds like you need to configure the default constraint so that it populates the column on insertion only:
DEFAULT CURRENT_TIMESTAMP
ALTER TABLE table CHANGE datetime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
Changing it to only be this means that any revisions will not trigger the timestamp value to be updated.

Manage two timestamps

I have two timestamps in one table.
1 - timestamp01 - This will record timestamp when user creating new row in database
2 - timestamp02 - This should record timestamp when user update/ edit anything in the specific row.
My question is how do I setup timestamp02 to record current timestamp when user did some changes to the specific row?
I am using MySQL as my database
This link should help you https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html. So you would have to mark that column as auto-updated to keep track of all updates to row
timestamp02 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
However, take note as the doc says
One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.

Is it possible to set up multiple automatic TIMESTAMP columns in MySQL for to track changes made in every column?

MySQL Database will be being modified using a PHP Crud system, and I need to track when each record was modified.
For example, if my table has 30 rows, and 30 columns, and I want to put a "Last Modified" beside each individual entry, can I somehow set up 30 TIMESTAMP columns related to each existing column to achieve that?
Or is there a better way?
Here's what I basically want:
Column 1 C1s TIMESTAMP Column 2 C2s TIMESTAMP Column 3 C3s TIMESTAMP...
Red (lastModified) Green (lastModified) Blue (lastModified)
Orange (lastModified) Purple (lastModified) Pink (lastModified)
Yellow (lastModified) Black (lastModified) Brown (lastModified)
... etc
It should be possible to determine which column changed though. As you have access to the OLD and NEW values, you could check for every field if it changed and if it changed enter a new timestamp into the according field. Something like:
CREATE TRIGGER updateTable
BEFORE UPDATE ON tableA
FOR EACH ROW
BEGIN
IF OLD.field1 != NEW.field1 THEN
SET NEW.timestamp1 = now()
END IF;
IF OLD.field2 != NEW.field2 THEN
SET NEW.timestamp2 = now()
END IF;
END
I think that might work, but I haven't tested it.
Not with the native MySQL auto initializing TIMESTAMP/ DATETIME functionality alone.
As of Mysql 5.6.5 you can have as many auto intitializing TIMESTAMP (or DATETIME) columns as you want, however these work at the level of the record, not column.
What I mean by this is you can auto set any TIMESTAMP on creation to the CURRENT_TIMESTAMP. You can also set the TIMESTAMP column to update on modification of the RECORD. However, all columns set to update timestamps on modifcation will update when any column is updated (http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html)
Given this, it only makes sense to have at most 2 auto TIMESTAMP columns: one for recording the creation date, the other for the modification date.
Here's how you create auto incrementing timestamp columns:
ALTER TABLE sometable
ADD COLUMN created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN modified_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP;
There is no way with just the auto timestamp functionality to only update when a specific column is modified.
You'd need to implement a trigger with a custom procedure to accomplish your need.
Another way to do what you want as a subset of a greater facility would be to implement a change log. Place a trigger on every table. In each trigger, write an "audit entry" record into a table which has details determined by the trigger logic: which field changed, the old value, the new value, and the timestamp when it occurred.
The audit entry could have an autofilled timestamp instead of explicitly managing it. With a little bit of cleverness, the triggers could feed a common stored procedure which accepts a tablename parameter as well as the other information.
This would provide a complete history of all changes, not just the last one.

MySQL Update DATETIME to DATE

I have a large table with a DATETIME column and for index reasons I want to add a column which just contains a DATE type. It seems that MySQL is not able to use an index by the following expression GROUP BY DATE(datetime) therefore I want to add another column with a second index.
For updating I use this simple statement:
UPDATE table SET datecol = DATE(datetimecol)
Now a strange behavior occurs: the datecol-column contains the correct values. But the datetimecolumn changes as well: to the current timestamp. This is the default value for this column.
I'm working now for many years with databases and MySQL but I cannot explain this behavior.
The current version is MySQL 5.1.66-0.
Do you have any suggestions or explanations for this?
Your datetimecol is not of type DATETIME, but of type TIMESTAMP which (by default) has the ON UPDATE CURRENT_TIMESTAMP attribute that automatically updates to the current time when a record is modified.
To suppress automatic properties for the first TIMESTAMP column, do either of the following:
Define the column with a DEFAULT clause that specifies a constant default value.
Specify the NULL attribute. This also causes the column to permit NULL values, which means that you cannot assign the current timestamp by setting the column to NULL. Assigning NULL sets the column to NULL.
Therefore:
ALTER TABLE my_table
MODIFY datetimecol TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
To update the records this one time without altering the values in datetimecol, you should explicitly set them to their incumbent values:
UPDATE my_table SET
datetimecol = datetimecol,
datecol = DATE(datetimecol);