Manage two timestamps - mysql

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.

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 to ignore timestamp column for a particular column

I have a table user. There is a field date_last_update with type timestamp , default CURRENT_TIMESTAMP, extra on update CURRENT_TIMESTAMP.
There is another column last_exported_date with type datetime. When user data get exported to excel, last_exported_date get updated to today datetime. So date_last_update column value get updated.
Is there any way I can update last_exported_date without updating date_last_update column ?

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.

MySQL: Trying to insert a value in a timestamp throws an error

I have a table with this column:
last_modified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
And it looks like I can not insert a row with a custom timestamp, I get this error:
Incorrect datetime value: '1145868501' for column 'last_modified' at row 1
I am trying to populate this table with data coming from another table, that other table only has a creation_time field which is a DATETIME so I use UNIX_TIMESTAMP(creation_time) to populate the timestamp.
I think the timestamp column with "DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" prevents me from inserting my own stuff, am I right? If yes where is the official doc about that, and what is the best solution? Creating a simple timestamp first then alter the table after inserting data?
Thanks!
EDIT: since people are advising me to not use UNIX_TIMESTAMP, I have to say that I didn't want to use that at the beginning, but I got this kind of error:
Incorrect datetime value: '2010-03-28 02:15:51' for column 'last_modified'
So I thought I had to insert a "real" timestamp...
You can explicitedly insert a value in a TIMESTAMP column. Read: 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.
Update
Hehe, the error occurs because - well- there was no datetime with '2010-03-28 02:15:51'! This was in the daylight saving time gap (which usually appears some day in March, between 02:00 - 03:00 or 03:00 - 04:00.
See: Daylight Saving Time explanation.
You're trying to put a long integer into a datetime field. That doesn't work. Remove the call to UNIX_TIMESTAMP() and it should work.
The MySQL TIMESTAMP type is almost identical to a DATETIME; it just has some extra auto-update magic. As far as SELECT and UPDATE is concerned, it is a DATETIME.
If the column is always auto-updated, you can remove the property, getters and setters from the Entity.
Doing this way, it will be ignored in all queries.

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

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.