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
datetimecolis not of typeDATETIME, but of typeTIMESTAMPwhich (by default) has theON UPDATE CURRENT_TIMESTAMPattribute that automatically updates to the current time when a record is modified.Therefore:
To update the records this one time without altering the values in
datetimecol, you should explicitly set them to their incumbent values: