MySQL Update DATETIME to DATE - mysql

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);

Related

MySQL set default value for DATE column using DATE_ADD?

I'm trying to add a DATE column to my table with DEFAULT DATE value using an expression:
ALTER TABLE `wp_ezts_project_params` ADD `est_completion` DATE NOT NULL
DEFAULT DATE_ADD( CURRENT_DATE(), INTERVAL 1 MONTH ) AFTER `client_id`
I have tried different variations of several SQL functions, but every time get an syntax error near DATE_ADD.
Are we not allowed to use expressions as default values in phpMyAdmin?
Are we not allowed to use expressions as default values in phpMyAdmin?
Yes, it's not allowed. It's explictly stated in the manual
The DEFAULT value clause in a data type specification indicates a
default value for a column. With one exception, the default value must
be a constant; it cannot be a function or an expression. This means,
for example, that you cannot set the default for a date column to be
the value of a function such as NOW() or CURRENT_DATE. The exception
is that you can specify CURRENT_TIMESTAMP as the default for TIMESTAMP
and DATETIME columns
So if you really want to assign a value for this column you will a) need to pass that in or b) use a trigger
But really you don't need this column at all. It sounds like you are storing a value that's the result of a simple date add - storing it means you are introducing redundancy. Simply calculate it on the fly.

MySQL date column auto fill with current date

I have a table where I have a date column. Is there a way for MySQL to auto fill this field whenever I insert a new registry with the current date? Or is this made automatically by default?
P.S.: I'm using PHPMyAdmin
Although it is an old post, maybe this image will help as it is more explicit:
(For phpMyAdmin users)
This configuration sets that field with a value like:
2015-12-11 07:50:47
PS: Note that the timestamp will set the time OF your server!! (i.e. the example above got the time from Pacific Time (07:50:47) but it could have been from a Spanish user at 16:50:47 local time) Keep this in mind.
Also, if you already have a "Created Date" you might need another column that updates the modification date whenever there is an update:
You only need to set on update CURRENT TIME STAMP in Attributes Field.
Ready to rock!
Set Default to in your mySql query
CURRENT_TIMESTAMP
you have to use
now()
function where you want to fill current time.
i.e.:
INSERT INTO user_rights (`user_id`,`right`,`group_id`,`created_date`) VALUES ( '42', '160', '1', now());
I realize this may not be a direct answer to the question but I do believe this is the most useable solution.
I highly recommend using a DATETIME or TIMESTAMP data type for the column in question.If you are utilizing a fairly current version of MySQL, MySQL will do the work for you.
Details:
To be very clear, as of 5.6.5, for both the TIMESTAMP & DATETIME datatypes, you can do the following:
Set a DEFAULT value of the current date & time (using NOW() or one of its aliases such as CURRENT_TIMESTAMP)This means every time you insert a new row into this table a TIMESTAMP or DATETIME column with this default will get the current date and time
Set an ON UPDATE constraint that will UPDATE a column to the current date & time when, (you guessed it) the row is updated
Here's how:
An Example in a CREATE TABLE statement:
CREATE TABLE t1 (
ts1 DATETIME ON UPDATE CURRENT_TIMESTAMP
,ts2 DATETIME DEFAULT NOW()
);
Please note that DATETIME can be replaced with TIMESTAMP for effectively the same functionality.
Additionally I suggest the use of the DATETIME data type over TIMESTAMP as DATETIME has a much larger range of dates it can support. It's worth mentioning that TIMESTAMP is smaller for those few cases that matters. For more details please read my answer here: https://stackoverflow.com/a/26117532/1748266
I have added this to my table and it works
ALTER TABLE Medewerkers ADD med_created TIMESTAMP DEFAULT now();
When you insert data into your record it update automatically the med_created
MySQL unfortunately doesn't allow specifying values other than constants as the default for columns other than TIMESTAMPs.
This is a feature available in MySQL versions 8.0+, but for older versions the only solution for a database defined default would be to use a trigger.
You can do something like this from the SQL screen
ALTER TABLE `table_name` CHANGE `created_at` `created_at` TIMESTAMP NOT NULL

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.

Is there a reason to use CURRENT_TIMESTAMP in inserts/updates?

I was using phpmyadmin to insert some dummy data into a table, and noticed it structured the insert like this:
INSERT INTO `arc`.`transactions` (
`txn_id` ,
`date_time` )
VALUES (
'50005',
CURRENT_TIMESTAMP );
Normally, I'd just not include the field "date_time" and the value is created by mySQL (as the field is of type timestamp), and auto-updates whenever you insert or update a record.
Is there any reason to structure the query as you see it above, explicitly inserting CURRENT_TIMESTAMP? Or is this a phpmyadmin oddity?
It could also be a matter of compatibility. CURRENT_TIMESTAMP is defined by the SQL standard. Automatically updated timestamp columns are not. An app that wants to be portable and record timestamps is better off explicitly specifying the currrent timestamp. And what better way to do that than to use the standard, built-in CURRENT_TIMESTAMP function?
You can have TIMESTAMP fields that don't auto-update (see the TIMESTAMP properties page for how to specify these), so depending on how you define the column, it might not be the case that an UPDATE query automatically adjusts the TIMESTAMP field.
PHPMyAdmin is probably taking the safe approach here and specifying the value to ensure it's updated, no matter the column definition. PHPMyAdmin can probably detect the default value if it wants to, so another possible explanation would be compatibility between various server versions and modes for any SQL that it generates.
It depends solely on date_time column definition. if it is like
`date_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Then you surely don't need to specify anything when inserting or updating a row, but if it is:
`date_time` TIMESTAMP NOT NULL
Then you have to specify something every time you create or update a row