MySQL 8
My query:
"UPDATE `users` SET `start_date` = '2007-04-09' AND `eligibility` = 1 WHERE `user_id` = 36;
I am getting the following error:
Warning: #1292 Truncated incorrect DOUBLE value: '2007-04-09'
I checked the type for the start_date field and it it set to date.
When I check the row, I find that it has NOT been modified, even though this is a warning.
I am using the PHPMyAdmin interface to interact with the MySQL DB/Server.
Any ideas?
ANSWER:
The answer is that I used an AND statement, instead of a comma. I have not used MySQL for a while. I use ORM instead, so I did not notice the error, and the error message threw me off.
You are setting start_date to:
'2007-04-09' AND `eligibility` = 1
You need a comma instead of AND there if you want to set eligibility too.
That specific message comes because '2007-04-09' AND interprets that string as a Boolean, which it is calling a DOUBLE.
Related
I am trying to run a select statement of a string column to set out alphanumeric values free of pure integars using the following statement:
select some_col_1 from some_table where some_col_1 = 'some value' and length(cast(some_col_1 as unsigned)) != length(some_col_1)
Whereas, I am trying to update some other column based on that condition using update statement,
My problem is, whenever I attempt to run the select statement, it is being successfully executed, meanwhile when I essentially use an update statement only gives some kind of unexpected error which is: ERROR 1292 (22007): Truncated incorrect INTEGER value: 'the first non-integer result from my table'.
I just can't figure out why it is doing this. Is there any experts who can see anything odd in my update statement? I am using mysql server version 8.0 and my update stmt precisely goes as follows:
update some_table set some_col_2 = true where some_col_1 = 'some value' and length(cast(some_col_1 as unsigned)) != length(some_col_1)
Your support with this would be very highly appreciated,
Thank you in advance,
What you are running into is mysql's sloppy way of implementing STRICT_TRANS_TABLES mode, which is intended to give an error when trying to set a value to something that has to be modified to meet the column's specification.
mysql chose to implement this by making things like cast or date parsing just give errors, instead of warnings, when done in a data modification statement, even when those expressions weren't what was actually attempted to be stored.
You can either disable STRICT_TRANS_TABLES mode for the duration of your update:
set session sql_mode=replace(##sql_mode,'STRICT_TRANS_TABLES','');
update ...
or use an alternate way of detecting your case that doesn't involve something that triggers a warning, or under STRICT_TRANS_TABLES, an error. For instance:
update some_table set some_col_2=true where some_col_1 not regexp '^([1-9][0-9]*|0)$';
This might be a question that has already been answered but I haven't found anything relating to it.
I think I might have a unique scenario, I need to add a parameter to the query for the LIMIT clause. Stack Overflow consider:
query = "SELECT * FROM `v_someview` WHERE `id` = %s LIMIT %s;"
x.execute(query,(str(id_variable),str(limit_variable))
Now it comes back with:
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near ''5'' at line 1
Am I supposed to use %d or is this specific use discouraged?
Note: the field from which this limit variable I drawn is a tinyint, so I don't see anyway for an SQL injection attack but I would still like to use parametized query with it.
Use with %d stops execution and bypasses the catch block:
try:
query = """SELECT * FROM `v_someview` WHERE `id` = %s LIMIT %d;"""
x.execute(query,(str(id),int(l)))
except Exception as ex:
#some code
The library in use is MySQLi.
Variable 'x' is a cursor.
Variable 'id_variable' is an int with the id
Just solved it myself by using:
try:
query = """SELECT * FROM `v_someview` WHERE `id` = %s LIMIT %s;"""
x.execute(query,(str(id),int(l)))
except Exception as ex:
#some code
So I'm declaring that i'm going to use a string (%s) but casting that parameter to an int().
I have a table with a column that is filled by a before insert trigger, this column is set to be NOT NULL and has no DEFAULT VALUE.
When I do a INSERT TABLE without passing this column, I receive the error: 1364 - Field 'column_name' doesn't have a default value. I'd search the web for a solution and have encountered this question: Field 'id' doesn't have a default value?. I then checked and changed the mysql_mode from:
"STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION"
to:
"NO_ENGINE_SUBSTITUTION"
but the insert query still returns the same error.
What should I do to bypass this "strict" mode? Or is it something else?
Please let me know if I need to include any config variable. I'm using mysql 5.6.39.
This is a known bug for the mysql 5.6.39 version. And has been fixed since version 5.7.
I keep getting this error for an insert into ... update query, the problem, this column 'str' does not exist in the table being updated, or any of the tables I'm pulling data from, and it's not in the query.
Error Code: 1406. Data too long for column 'str' at row 215710
I'm totally stumped here. It this a mysql bug? I went as far as to isolate the query to just one column, still got this error.
UPDATE 1:
I just tried updating with a manual value, on one column only set to longtext. I'm still getting the exact same error.
UPDATE 2:
Major update, I isolated the problem down to the select query, the original error implied a table column, however, it seems to be pointing to what I assume is some kind of temp table column for the following row. When I yanked this out of the query, it worked. Ironically, this is the same column I did my one column test with where I manually entered an value in the update on duplicate key part of my query.
CONCAT_WS('', UC_Words(`name`), ' | ', UC_Words(`city`), ' ', UC_Words(`state`), ' ', UC_Words(`country`), CONCAT('|---|',`name-key`)) AS `owner-data`
I'm currently using lots of GROUP_CONCAT's, but I have already adjusted the length. Is there a parameter for CONCAT_WS length? NOTE: UC_Words is a custom function. This could possibly be a culprit, still need to test it...
UPDATE 3:
The error appears to be a result of the UC_Words function. The 'str' is the name field in that function. Type was set to VARCHAR 255, which was too short.
MySQL will truncate any insert value that exceeds the specified column width.
to make this without error try Switch your MySQL mode to not use STRICT.
EDIT:
To change the mode
This can be done in two ways:
Open your "my.ini" file within the MySQL installation directory, and look for the text "sql-mode".
Find:
Code:
Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Replace with:
Code:
Set the SQL mode to strict
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Or
You can run an SQL query within your database management tool, such as phpMyAdmin:
Code:
SET ##global.sql_mode= '';
The error appears to be a result of the UC_Words function. The 'str' is the name field in that function. Type was set to VARCHAR 255, which was too short.
I am aware that the certain field doesn't have a default value.
I've been using MySQL 5.5.28 and it does work whenever I insert without specifying a value on that field. The field is TINYINT and by default, without specifying any value AND without declaring a default value during creation of the table, a value of 0 will be inserted in that field during INSERT statement.
However, after updating to MySQL 5.5.30, the query doesn't work anymore and is returning Field doesn't have a default value.
I've been looking through the changelogs and didn't find any clue that something has changed with regards to the default values of Integer.
MySQL 5.5.29 : http://dev.mysql.com/doc/relnotes/mysql/5.5/en/news-5-5-29.html#mysqld-5-5-29-feature
MySQL 5.5.30 : http://dev.mysql.com/doc/relnotes/mysql/5.5/en/news-5-5-30.html
Test queries:
MyTable has the Fields MyField1 and MyField2
INSERT INTO MyTable(MyField2)VALUES('MICHAEL');
Result on MySQL 5.5.28:
MyField1 | MyField2
0 | MICHAEL
With warning: 1 row(s) affected, 1 warning(s): 1364 Field 'MyField1' doesn't have a default value
Result on MySQL 5.5.30:
No changes on data and throws an error
Error Code: 1364. Field 'MyField1' doesn't have a default value
INSERT INTO MyTable(MyField1, MyField2)VALUES(0, 'MICHAEL');
The above query will work though.
In the 1st server strict sql mode was not enabled, while in the 2nd one it was. Read more about strict mode in the mysql documentation.
Specifically:
If strict mode is not in effect, MySQL inserts adjusted values for invalid or missing values and produces warnings (see Section 13.7.5.40, “SHOW WARNINGS Syntax”). In strict mode, you can produce this behavior by using INSERT IGNORE or UPDATE IGNORE.