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 ?
Related
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.
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.
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);
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.
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.