Mysql giving error sometimes as Incorrect DateTime value - mysql

I am running a mysql query using golang:
query: UPDATE RESTORE_TIMESTAMP SET last_restored_at=? where id=?, updatedTime, 1
Here updatedTime is in strfmt.DateTime format in go.
This query runs fine for some time but after that it starts failing with below error.
Sometimes after mysql retries it passes but most of the times it is failing.
I'm not getting any clue why is this happening.
Error 1292: Incorrect datetime value: '2020-10-19T16:25:08.958Z' for column db.RESTORE_TIMESTAMP.last_restored_at at row 1"
table details:
MariaDB [db]> show columns from RESTORE_TIMESTAMP;
+------------------+-----------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-----------+------+-----+---------------------+-------+
| id | enum('1') | NO | PRI | NULL | |
| last_restored_at | timestamp | NO | | current_timestamp() | |
+------------------+-----------+------+-----+---------------------+-------+
2 rows in set (0.001 sec)
I have only one row in my table as above and I need to update that every time.
Please point me somewhere how can I resolve this query failure.

You should to apply STR_TO_DATE conversion for convert your date into DB format
UPDATE RESTORE_TIMESTAMP
SET last_restored_at=STR_TO_DATE(?, '%Y-%m-%dT%H:%i:%s.%fZ')
WHERE id=?;
Test it on SQLize.online

Related

MySQL throwing syntax error while trying to alter a table

I have been trying to alter a table to include a date column with default value of CURDATE() but MySQL is constantly throwing syntax error. Now, I have checked syntax for altering a table from several sources but I believe I do not have any syntax error. When I remove the default value part, the query runs fine but for some reason it cannot add a default value for the date column. I don't know why that is the case.
The code:
mysql> describe test;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| col1 | int | YES | | 0 | |
| col2 | varchar(100) | YES | | hello | |
| col3 | varchar(5) | YES | | T | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
mysql> ALTER TABLE test ADD COLUMN col4 DATE DEFAULT CURDATE();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURDATE()' at line 1
mysql>
Edit: My MySQL version: 8.0.31
I think it has to be like this now:
ALTER TABLE test ADD COLUMN col4 DATE DEFAULT (CURRENT_DATE);
Note the parenthesis, or (curdate())

Mariadb 10.4 Data truncated for column 'column_name' on insert but not on update

I have the following table (mariadb 10.4) called p:
+----------------+----------------------------------------------------------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+----------------------------------------------------------------------------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| description | text | YES | | NULL | |
| url | text | YES | | NULL | |
| source | enum('source_a','source_b','source_c','source_d','source_e') | YES | | NULL | |
I currently have a couple of million rows on this table with the sources a, b, c, and d. Just recently we applied a migration to add source_e and we started getting the error ERROR 1265 (01000): Data truncated for column 'source' at row 1 when trying to inset a row with the source_e. The used command that yields the error is the following:
INSERT INTO p (description, url, `source`) VALUES ('test', 'https://google.com.br', 'source_e');
Insertions with any of the other sources are still working.
The behavior changes when editing a row that is already on the db, the error is not shown:
UPDATE `p` SET `source`='source_e' WHERE `id`='3';
Yields:
Query OK, 1 rows affected (0.001 sec)
Is there a way to debug this scenario? I've tried changing the log level of the db to get a better insight on the problem (SET GLOBAL log_warnings=3;) but the error message did not change.
I also tried changing the source_e name to source_e_, the error persisted.
Btw, i did change the name of the fields to comply with company policies.
It turns out it was my bad. We happen to have a trigger on insertions of this table that feeds a materialized view kind of table. All I had to do was add 'source_e' to the source field on the other table.

How can I make MySQL automatically delete records of a table when date of deletion is specified in each record?

I have made the following table in MySQL:
mysql> use test;
Database changed
mysql> desc NeoTec_test;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| Product_Key | varchar(10) | NO | PRI | NULL | |
| Validation | date | YES | | NULL | |
| Expiry | date | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.03 sec)
mysql> select * from NeoTec_test;
+-------------+------------+------------+
| Product_Key | Validation | Expiry |
+-------------+------------+------------+
| GF427DHH5 | 2017-11-16 | 2017-11-17 |
| GFHJV75HG | 2017-11-16 | 2017-11-18 |
| GFJYFRTV5 | 2017-11-16 | 2017-11-20 |
+-------------+------------+------------+
3 rows in set (0.00 sec)
Now coming to the point, I need some help with a part of my project. I want MySQL to automatically delete the Product keys that have expired, i.e., I want to get the product keys deleted automatically on their expiry dates given under the "Expiry" Column of the table. How can I do so? I am a total newbie to MySQL events so I would appreciate the full code... Thank you! :-)
Earlier research I did was not fruitful, but I did found this, which was half helpful...:
How to delete a MySQL record after a certain time
You can use event scheduler to perform the task like below:
DELIMITER //
CREATE EVENT eventName
ON SCHEDULE EVERY 1 WEEK
STARTS 'Some Date to start'
ENDS 'End date If any'
DO
BEGIN
DELETE FROM NeoTec_test WHERE NOW() > Expiry
END//
DELIMITER ;
Thete is no functionality in mysql to automatically delete a record. You need to trigger the deletion either through a scheduler (mysql's as shown in the question you found, or an external scheduler such as cron), or via a database trigger.
The latter one is probably an overkill.
I would use a scheduler set to a convenient interval based on your business requirements to clean up the table.

Update only the time stamp in a MySql table

Given this table
mysql> describe last_user_activity;
+--------------+------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+-------------------+-------+
| customer_id | int(11) | NO | | NULL | |
| token | text | NO | | NULL | |
| time_stamp | timestamp | YES | | CURRENT_TIMESTAMP | |
| is_logged_in | tinyint(1) | NO | | 0 | |
+--------------+------------+------+-----+-------------------+-------+
4 rows in set (0.00 sec)
I want to "touch" a row of the table, setting the time_stamp to its default, CURRENT_TIMESTAMP.
I thought that I could try
UPDATE last_user_activity WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
(that is a valid token in the table), but that resulted in
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'WHERE
token="40aed4d9-c9ac-471e-8d53-b2baa0d72523"' at line 1
Oh, well, it seemed clever, but NVM. So, then I tried
UPDATE last_user_activity SET token="40aed4d9-c9ac-471e-8d53-b2baa0d72523"
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
which said that it succeeded, BUT, the timestamp field was not updated.
What am I doing wrongly?
UPDATE last_user_activity
SET time_stamp = NULL
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
OR
UPDATE last_user_activity
SET time_stamp = NOW()
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
It looks as if you have given default value of column time_stamp, but not on update.
You may considering this change :
ALTER TABLE last_user_activity
MODIFY COLUMN time_stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
I hope this works.After that you could try using your update commands.

Converting MySQL warning into an error

Is there any way to convert the warning that MySQL is issuing about an invalid datetime into a hard error? I've tried using SET sql_mode='TRADITIONAL'; which apparently is supposed to turn (some) things that are warnings into errors, but it does not have any effect here. This is MySQL 5.1.56. Something that works on a session-level would be ideal, but I'll take what I can get.
mysql> describe test_table2;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| value | int(11) | YES | | NULL | |
| name | varchar(16) | YES | | NULL | |
| sometime | datetime | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from test_table2;
+-------+-------+---------------------+
| value | name | sometime |
+-------+-------+---------------------+
| 1 | one | 2002-09-01 10:00:00 |
| 2 | two | 2002-09-02 11:00:00 |
| 3 | three | 2002-09-03 12:00:00 |
| 4 | four | 2002-01-04 13:00:00 |
| 5 | five | 2002-01-05 14:00:00 |
+-------+-------+---------------------+
5 rows in set (0.00 sec)
mysql> select * from test_table2 where sometime = 'foo';
Empty set, 2 warnings (0.00 sec)
Warning (Code 1292): Incorrect datetime value: 'foo' for column 'sometime' at row 1
Warning (Code 1292): Incorrect datetime value: 'foo' for column 'sometime' at row 1
With SET sql_mode='TRADITIONAL', doing an INSERT with an invalid date causes an error, but doing a SELECT with an invalid date still causes a warning. You can trigger the error by passing the (possibly invalid) date value to this query first:
CREATE TEMPORARY TABLE IF NOT EXISTS date_guard (date DATE) SELECT 'foo' AS date;
where 'foo' is the date value you want to validate.
Who is supposed to see the error?
If this is a fixed string 'foo' just try converting 'foo' to a date and see if you can a valid result (i.e. not 00-00-000). Do a pre-query to check the validity of the date, and then continue after.
I have not been able to make MySQL give an error in this case (or even convert the invalid date to a NULL - it insists on making it 00-00-0000).