MySQL error code: 1175 during UPDATE (MySQL-Workbench vs. console) - mysql

Am very aware of that this issue can be resolved with disabling safe update mode enabled (e.g. see here: MySQL error code: 1175 during UPDATE in MySQL Workbench). However, I do not wish to disable safe update mode (and there are many many solutions that propose this).
Similarly, I am aware that setting the WHERE clause to KEY-value that matches everything is supposed to work. However, doesn't appear to work on mysql-workbench - at least not the way I hoped (or the way it did work on the console).
For example, the following didn't work on mysql-workbench (but did on the console):
UPDATE FUEL_SOURCES AS FS
INNER JOIN
FUEL_CATEGORY FC ON FC.FUEL_CATEGORY = FS.FUEL_CATEGORY
SET
FS.FUEL_CATEGORY_ID = FC.ID
WHERE
FC.ID <> 0 AND FS.ID <> 0
...If I explicitly / exactly set the ID's (e.g. WHERE FC.ID = 20 AND FS.ID <> 10 for example) it would work in mysql-workbench. But doing this would involve iterating through every key-pair combination.
Be intereted to know what is causing this behaviour, or if I am doing something horribly wrong. Using mysql-workbench 6.3

From https://dev.mysql.com/doc/workbench/en/workbench-faq.html#faq-workbench-delete-safe
By default, Workbench is configured to not execute DELETE or UPDATE
queries that do not include a WHERE clause on a KEY column.
Such configuration prevents you from deleting or updating table mistakenly, since you are doing a batch update on data without a key.
To resolve this, as you may be already aware the following options.
Open your Workbench Preferences, select the SQL Editor section, and disable the following preference: "Safe Updates" - Forbid UPDATEs and DELETEs with no key in WHERE clause or no LIMIT clause.
Run SET SQL_SAFE_UPDATES=0;

If you are using workbech, you can first execute
SET SQL_SAFE_UPDATES = 0;
And then execute delete statement

If you want to still update your data with safe update on, you must retool your where clause so that it includes references to the table's primary key(s). See this page.

Related

Can't specify target table for update in FROM clause - Mysql 5.7.10 - derived_merge not working

I have the following query:
update tblwerknemerdienst toUpdate
set datumtot = (select subdate(datumvanaf,1)
from (select * from tblwerknemerdienst) nextDienst
where nextDienst.Werknemer = toUpdate.Werknemer
and nextDienst.datumvanaf > toUpdate.DatumVanaf
order by DatumVanaf
LIMIT 1)
WHERE DatumTot IS NULL;
The query runs fine on MySql versions other than MySql 5.7.10. I've searched around the web and found that you have to set derived_merge=off, but sadly this had no effect and the query still fails with the same error.
I have also tried several different ways of rewriting the query, but all to no avail.
Is there something I'm missing or is there another way to accomplish this?
In the end I fixed this by rewriting the whole thing in a procedure, where I used a cursor to execute the query and get the necessary data. Then I perform the update statement based upon the fields selected in the cursor.
This seemed to be the only way to reliably perform the operation required on different versions of MySql.

DELETE query results in 'Query Interrupted' MySQL Workbench?

I can successfully delete records manually by click-selecting & deleting row(s) but executing delete queries result in 'Query Interrupted'.
My deletion queries are in the form:
DELETE FROM table where column = value;
The select statement uses the same values:
SELECT * FROM table WHERE column = value;
and returns desired results.
What could be causing the delete statement to fail? Are there limits on the amount of records you can delete at once in workbench?
If you wish to delete the entire contents of a table you can use Truncate.
TRUNCATE [TABLE] tbl_name
Please see the docs: https://dev.mysql.com/doc/refman/5.7/en/truncate-table.html
Using the DELETE function is usually used for deleting single rows.
According to the documentation, in the Preferences >> SQL Editor >> Other, the Safe Updates setting is on by default.
Safe Updates (rejects UPDATEs and DELETEs with no restrictions)
Enabled by default. Prevents UPDATE and DELETE queries that lack a corresponding key in a WHERE clause, or lack a LIMIT clause, from executing. This option requires a MySQL server reconnection.
When selected, this preference makes it possible to catch UPDATE and DELETE statements with keys that are not used properly and that can probably accidentally change or delete a large number of rows.
I think what this says is that if the setting is on, then the column you are filtering by in the DELETE or UPDATE statement must be the primary key, it cannot be just any column.
If you change the setting to off, then you might need to restart MySQL Workbench for the change to take effect (at least under Linux).
There is a default thousand-row limit in MySQL-Workbench. The SELECT query will return results but DELETE will fail if the number of records to be deleted exceeds one thousand. One option is to limit the results in the query itself or you can adjust the settings as stated in the documentation.

MySQL Update table with Select/Join

Please note: I am using MySQL Workbench for this so some errors may be related to that specifically (I'm not 100% sure I'm afraid)
Hi,
I am working on a project for my Uni where I accept a CSV file containing a list of guests and information such as the course area they are interested in. The CSV file will contain the actual name of the Area of Interest as this comes from a registration web page (for Open Days).
I am trying to take the Area of Interest in my Guests (joinTest) table, do a join to the Interests table (that contains the ID numbers for them as well as the Interest Name) and populate the InterestID column in the Guests table.
I am using a test table for now until I've got the SQL right. This is the SQL I am currently using:
UPDATE joinTest
SET interestID =
(
SELECT interest.interestID FROM joinTest
LEFT JOIN interest on joinTest.interestName = interest.interestName
)
WHERE interestID IS NULL;
I've tried that and get the error
Error Code: 1093. You can't specify target table 'joinTest' for update in FROM clause
So I'm assuming I can't use a SELECT clause while join a join in an UPDATE.
I then tried this:
UPDATE joinTest
INNER JOIN interest
ON jointest.interestname = interest.interestName
SET joinTest.interestID = interest.interestID;
I get this error:
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.
I'm completely stumped now. I'm not sure where to go from here.
If any more information is required, please let me know.
SQL_SAFE_UPDATES is disabled by default but Workbench enables it. Like the error message says: toggle the option in Preferences -> SQL Queries and reconnect.
This error shouldn't exist unless you specifically turn on SQL_SAFE_UPDATES in your web app (php).
It's MySQL Workbench specific thing. You can disable this behavior in settings as your error message advices you: ... toggle the option in Preferences -> SQL Queries and reconnect.
PS You can safely run this query in your code without SET SQL_SAFE_UPDATES = 0;.

mysql workbench cannot : update where like

Normally i can update my rows w/ queries such as
UPDATE t SET col1='123' WHERE col2 LIKE '%abc%';
or
UPDATE mydb.t SET col1='123' WHERE col2 LIKE '%abc%';
But with MySQL Workbench I seem to only be able to SELECT but cannot UPDATE the tables with queries.
Does anyone know a fix for working with workbench?
FIX
To disable safe mode, toggle the option in Preferences -> SQL Editor -> Query Editor and reconnect.
Mysql workbench usually disables by default updates without a where clause, and/or updates that hit more than a configured X elements.
When you try to update and fail, if it is how I think it is, then pay attention to the error message, as it tells you exactly which setting to change.

error appear when I execute a SQL request

I have a SQL request that I can't execute ,this error appear:
To disable safe mode, toggle the option in Preferences -> SQL Editor -> Query Editor and reconnect.
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
My request:
update customfieldvalue
set stringvalue="****"
where customfieldvalue.customfield IN
(select customfield.id
from customfield
where customfield.cfname="Resoltion"
and customfieldvalue.issue=12345);
how can I fix this problem,because in my production environment I can't do this manipulation?
PS:I'am using mysql
To disable it just for the current session run:
SET SQL_SAFE_UPDATES=0;
To fix this problem:
To disable safe mode:
Toggle the option in Preferences -> SQL Editor -> Query Editor and reconnect.
The cause:
You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
(Did you read the message? It explains everything)
If you can't turn off safe update in production, you'll need to rework your query to involve at least one key column in customfieldvalue. Without it, you'll be performing a table scan; using key column #1, you might end up with the logical equivalent ( WHERE customfieldvalue.keycol1 IS NOT NULL ), but that will satisfy the safe update constraints.
SET SQL_SAFE_UPDATES=0;
update customfieldvalue
set stringvalue="****"
where customfieldvalue.customfield IN
(select customfield.id
from customfield
where customfield.cfname="Resoltion"
and customfieldvalue.issue=12345);