Update query failing with error : 1175 - mysql

I am trying to update a table using the following query
update at_product A join
(
SELECT atbillfields.billeditemguid,count(*) AS numberOfPeopleBought
,sum(atbillfields.billeditemqty) AS soldquantity
FROM jtbillingtest.at_salesbill atsalesbill
JOIN jtbillingtest.at_billfields atbillfields
ON atsalesbill.billbatchguid=atbillfields.billbatchguid
WHERE atsalesbill.billcreationdate BETWEEN '2013-09-09' AND date_add('2013-09-09', INTERVAL 1 DAY)
GROUP BY atbillfields.billeditemguid) B ON B.billeditemguid = A.productguid
SET A.productQuantity = A.productQuantity - B.soldquantity
But, getting the following exception:
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.
When I gave a where clause with the update like A.productQuantity = 1, it updated that particular.
Can someone point why I am unable to execute the query and how to solve the issue?

Have a look at:
http://justalittlebrain.wordpress.com/2010/09/15/you-are-using-safe-update-mode-and-you-tried-to-update-a-table-without-a-where-that-uses-a-key-column/
If you want to update without a where key you must execute
SET SQL_SAFE_UPDATES=0;
right before your query.
Another option is to rewrite your query o include a key.

This error means you're operating in safe update mode and therefore you have two options:
you need to provide a where clause that includes an index for the update to be successful or
You can disable this feature by doing SET SQL_SAFE_UPDATES = 0;

You can try on MysqlWorkbench
Go to Edit --> Preferences
Click "SQL Editor" tab and uncheck "Safe Updates" check box
Query --> Reconnect to Server (logout and then login)
I hope it is helpful for you.

In MySQL 5.5, if you're using MySQL Workbench then
Go to Edit --> Preferences
Click "SQL Queries" tab and uncheck "Safe Updates" check box.
Query --> Reconnect to Server (logout and then login)
This works.

In my case, I disable checking the foreign key using this Mysql command:
SET FOREIGN_KEY_CHECKS=0;

Related

How to avoid MySQL Workbench error code: 1175 during this UPDATE *without* disabling "safe updates"

I have a particular MySQL UPDATE statement which does specify the required primary key in its WHERE clause and yet which still produces Error 1175 when run in MySQL Workbench.
I am perfectly aware of MySQL error code: 1175 during UPDATE in MySQL Workbench. My case appears to be the same as MySQL error code: 1175 during UPDATE (MySQL-Workbench vs. console). Like that questioner, I do not wish to disable MySQL-Workbench's "safe update/delete" option. That question failed to get a solution. I would like to try to get an actual solution.
SQL UPDATE statement:
-- update new columns' values from corresponding rows in `charges_arc`
UPDATE `charges`
INNER JOIN `charges_arc` ON `charges`.`ChargeID` = `charges_arc`.`ChargeID`
SET `charges`.`ChargeClearDate` = `charges_arc`.`ChargeClearDate`
WHERE `charges`.`ChargeID` = `charges_arc`.`ChargeID`;
ChargeID is indeed the Primary Key column in both charges and charges_arc tables.
This means that this statement does satisfy MySQL Workbench's 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.
Is there a solution to rewrite this query such that Workbench does not Error 1175, and which does not require setting SET SQL_SAFE_UPDATES=0/changing Workbench's preferences?
Well, having played further, so far I have found that the following seems to keep Workbench happy:
-- update new columns' values from corresponding rows in `charges_arc`
UPDATE `charges`
INNER JOIN `charges_arc` ON `charges`.`ChargeID` = `charges_arc`.`ChargeID`
SET `charges`.`ChargeClearDate` = `charges_arc`.`ChargeClearDate`
WHERE `charges`.`ChargeID` = `charges_arc`.`ChargeID`
AND `charges`.`ChargeID` <> -9999
That's just adding AND charges.ChargeID <> -9999 to the condition. It hardly narrows the scope much(!), and it's pretty ugly(!). I can only guess that Workbench would like to "see some kind of literal test against the PK", so that you show it you have thought about the PK in a certain way! It does at least allow you to do the query without disabling "safe updates".
I will leave this open for a couple of days to see if someone can think of something neater.
For my own part, I have a lot of these kind of UPDATEs in a large upgrading script file, this looks so ugly to me that I may end up going for SET SQL_SAFE_UPDATES=0 over the whole file after all...
EDIT: In the end I decided it was so ugly having to add something like the extra AND clause above to these types of UPDATE ... JOIN ...s that I preferred to SET SQL_SAFE_UPDATES=0 around them, at least for clarity.
Using MySQL 5.6 and MySQLWorkbench 8, I received this error in the same circumstances. I was able to fix the error by qualifying the field name in the WHERE clause.
For example, this caused the 1175 error:
UPDATE `tReports`
SET
`Title` = Title,
`Descr` = Descr
WHERE `ID` = ID;
And this resolved it:
UPDATE `tReports`
SET
`Title` = Title,
`Descr` = Descr
WHERE `tReports`.`ID` = ID;

Update query not working in mysql workbench

I have a MySql query, which is given below:
UPDATE signup SET lastname='Lastname', password='123'
WHERE firstname='Firstname';
I am using MySql Workbench to execute the query.
But it's not updating the row and shows this error:
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 Editor and reconnect.
In mysql workbench the safe mode is enabled by default, so if your WHERE clause doesn't have a key it will prevent running the query. Try disabling that using these steps -
Edit > Preferences > Sql Editor > uncheck the "Safe Updates"
Note - try reconnecting the server (Query > Reconnect to Server) and than run your query again.
MySQL helps you particularly avoid updating/deleting multiple rows in one shot. To achieve that, it doesn't allow you to run UPDATE queries without passing the ID parameter. This is called as the SAFE UPDATES mode.
As said by #ManojSalvi, you can set it permanently from the settings.
In case you want to temporarily disable the SAFE UPDATE mode, you can try the following:-
SET SQL_SAFE_UPDATES = 0;
UPDATE signup SET lastname='Lastname', password='123'
WHERE firstname='Firstname';
SET SQL_SAFE_UPDATES = 1;
[edit]
#ManojSalvi got it, workbench related
MySQL error code: 1175 during UPDATE in MySQL Workbench
Work fine for me...
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE t
(`firstname` varchar(6), `lastname` varchar(14), `password` varchar(3))
;
INSERT INTO t
(`firstname`, `lastname`, `password`)
VALUES
('Pramod', 'Alfred', '***'),
('test', 'hello h.', '***')
;
UPDATE t SET lastname='Alfred Schmidt', password='123' WHERE firstname='Pramod';
Query 1:
select * from t
Results:
| firstname | lastname | password |
|-----------|----------------|----------|
| Pramod | Alfred Schmidt | 123 |
| test | hello h. | *** |
"Safe mode" is on by default in MySQL workbench. You can change it go to mysqlworkbench at the top left –> preferences–> sql editor –> uncheck the safe mode and then try reconnecting. Or you can just type
SET SQL_SAFE_UPDATES = 0;
This will do the same.
I don't think it has anything to to with the SAFE UPDATES since you have clearly stated WHERE you wanted to make changes.
I had the same issue, but I tried wrapping the column's name in backticks ` and it worked. You can find backticks to the left of number 1 on the keyboard.
One other thing you can try is to SELECT the table and double click on the item you want to UPDATE then apply the changes at the bottom right of the window.

MySQL Workbench - How do I set a value to NULL using the GUI? [duplicate]

I'm using the MySQL Query Browser (part of the MySQL GUI Tools) and need to change a field to NULL, but I can't figure out how to do it - if I delete the value it tries to update it to ''. Typing "NULL" makes it try to update to 'NULL' (a string).
I know I could just write a query to do it, but that defeats the entire purpose of the tool, no?
In MySQL Query Browser, right click on the cell and select 'Clear field content' while the focus is in another cell.
In MySQL Workbench, right click on the cell and select 'Set Field to NULL'.
In MySQL Workbench, with the cell selected, press Shift + Delete
Tested on 6.3.4.0
Right-click on that column and select the
'Set field to Null'
option from the context menu.
Removing the contents:
This works for some tools (sorry to hear it doesn't for yours).
This may not appear as null but will when you perform a query.
Shift + Delete
work well in MySql Workbench 8
I'd try Ctrl-0 (zero), because that works in some tools.

Update Field in Database A With Field From Database B and Avoiding Safe Update Warning

I want to update the field xfade in samdb.songlist with the values from xfade in another database (on the same host), samdbtmp.songlisttmp. As long as there is the text APPLE in the filename field.
Here is my attempt...
UPDATE samdb.songlist
SET
samdb.songlist.xfade = (SELECT
samdbtmp.songlisttmp.xfade
FROM
samdbtmp.songlisttmp
WHERE
samdbtmp.songlisttmp.ID = samdb.songlist.ID)
WHERE filename LIKE '%201411.mp3';
I would like to do this without taking off safe updates in Workbench, and I thought by adding the WHERE, with key field ID, I should be able to do that. But I get 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.
Question... is my MySQL correct? And how do I avoid the error?
Not really an answer as much as a workaround. I went with the below...
SET SQL_SAFE_UPDATES=0;
UPDATE samdb.songlist
SET
samdb.songlist.xfade = (SELECT
samdbtmp.songlisttmp.xfade
FROM
samdbtmp.songlisttmp
WHERE
samdbtmp.songlisttmp.ID = samdb.songlist.ID)
WHERE filename LIKE '%201411.mp3';
SET SQL_SAFE_UPDATES=1;
I was happy to have safe updates disabled for this query, just not permanently off.

MySQL error code: 1175 during UPDATE in MySQL Workbench

I'm trying to update the column visited to give it the value 1. I use MySQL workbench, and I'm writing the statement in the SQL editor from inside the workbench. I'm writing the following command:
UPDATE tablename SET columnname=1;
It gives me the following error:
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
....
I followed the instructions, and I unchecked the safe update option from the Edit menu then Preferences then SQL Editor. The same error still appear & I'm not able to update this value. Please, tell me what is wrong?
It looks like your MySql session has the safe-updates option set. This means that you can't update or delete records without specifying a key (ex. primary key) in the where clause.
Try:
SET SQL_SAFE_UPDATES = 0;
Or you can modify your query to follow the rule (use primary key in where clause).
Follow the following steps before executing the UPDATE command:
In MySQL Workbench
Go to Edit --> Preferences
Click "SQL Editor" tab and uncheck "Safe Updates" check box
Query --> Reconnect to Server // logout and then login
Now execute your SQL query
p.s., No need to restart the MySQL daemon!
SET SQL_SAFE_UPDATES = 0;
# your code SQL here
SET SQL_SAFE_UPDATES = 1;
SET SQL_SAFE_UPDATES=0;
UPDATE tablename SET columnname=1;
SET SQL_SAFE_UPDATES=1;
No need to set SQL_SAFE_UPDATES to 0, I would really discourage it to do it that way. SAFE_UPDATES is by default on for a REASON. You can drive a car without safety belts and other things if you know what I mean ;)
Just add in the WHERE clause a KEY-value that matches everything like a primary-key comparing to 0, so instead of writing:
UPDATE customers SET countryCode = 'USA'
WHERE country = 'USA'; -- which gives the error, you just write:
UPDATE customers SET countryCode = 'USA'
WHERE (country = 'USA' AND customerNumber <> 0); -- Because customerNumber is a primary key you got no error 1175 any more.
Now you can be assured every record is (ALWAYS) updated as you expect.
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 Editor and reconnect.
Turn OFF "Safe Update Mode" temporary
SET SQL_SAFE_UPDATES = 0;
UPDATE options SET title= 'kiemvieclam24h' WHERE url = 'http://kiemvieclam24h.net';
SET SQL_SAFE_UPDATES = 1;
Turn OFF "Safe Update Mode" forever
Mysql workbench 8.0:
MySQL Workbench => [ Edit ] => [ Preferences ] -> [ SQL Editor ] -> Uncheck "Safe Updates"
Old version can:
MySQL Workbench => [Edit] => [Preferences] => [SQL Queries]
Preferences...
"Safe Updates"...
Restart server
SET SQL_SAFE_UPDATES=0;
OR
Go to Edit --> Preferences
Click SQL Queries tab and uncheck Safe Updates check box
Query --> Reconnect to Server
Now execute your sql query
If you are in a safe mode, you need to provide id in where clause. So something like this should work!
UPDATE tablename SET columnname=1 where id>0
On WorkBench I resolved it By deactivating the safe update mode:
-Edit -> Preferences -> Sql Editor then uncheck Safe update.
The simplest solution is to define the row limit and execute. This is done for safety purposes.
I found the answer. The problem was that I have to precede the table name with the schema name. i.e, the command should be:
UPDATE schemaname.tablename SET columnname=1;
Thanks all.
In the MySQL Workbech version 6.2 don't exits the PreferenceSQLQueriesoptions.
In this case it's possible use: SET SQL_SAFE_UPDATES=0;
Since the question was answered and had nothing to do with safe updates, this might be the wrong place; I'll post just to add information.
I tried to be a good citizen and modified the query to use a temp table of ids that would get updated:
create temporary table ids ( id int )
select id from prime_table where condition = true;
update prime_table set field1 = '' where id in (select id from ids);
Failure. Modified the update to:
update prime_table set field1 = '' where id <> 0 and id in (select id from ids);
That worked. Well golly -- if I am always adding where key <> 0 to get around the safe update check, or even set SQL_SAFE_UPDATE=0, then I've lost the 'check' on my query. I might as well just turn off the option permanently. I suppose it makes deleting and updating a two step process instead of one.. but if you type fast enough and stop thinking about the key being special but rather as just a nuisance..
I too got the same issue but when I off 'safe updates' in Edit ->
Preferences -> SQL Editor -> Safe Updates, still I use to face the
error as "Error code 1175 disable safe mode"
My solution for this error is just given the primary key to the table if not given and update the column using those primary key value.
Eg: UPDATE [table name] SET Empty_Column = 'Value' WHERE
[primary key column name] = value;
True, this is pointless for the most examples. But finally, I came to the following statement and it works fine:
update tablename set column1 = '' where tablename .id = (select id from tablename2 where tablename2.column2 = 'xyz');
This is for Mac, but must be same for other OS except the location of the preferences.
The error we get when we try an unsafe DELETE operation
On the new window, uncheck the option Safe updates
Then close and reopen the connection. No need to restart the service.
Now we are going to try the DELETE again with successful results.
So what is all about this safe updates? It is not an evil thing. This is what MySql says about it.
Using the --safe-updates Option
For beginners, a useful startup option is --safe-updates (or
--i-am-a-dummy, which has the same effect). It is helpful for cases when you might have issued a DELETE FROM tbl_name statement but
forgotten the WHERE clause. Normally, such a statement deletes all
rows from the table. With --safe-updates, you can delete rows only by
specifying the key values that identify them. This helps prevent
accidents.
When you use the --safe-updates option, mysql issues the following
statement when it connects to the MySQL server:
SET sql_safe_updates=1, sql_select_limit=1000, sql_max_join_size=1000000;
It is safe to turn on this option while you deal with production database. Otherwise, you must be very careful not accidentally deleting important data.
just type SET SQL_SAFE_UPDATES = 0; before the delete or update and set to 1 again
SET SQL_SAFE_UPDATES = 1
If you're having this problem in a stored procedure and you aren't able to use the key in the WHERE clause, you can solve this by declaring a variable that will hold the limit of the rows that should be updated and then use it in the update/delete query.
DELIMITER $
CREATE PROCEDURE myProcedure()
BEGIN
DECLARE the_limit INT;
SELECT COUNT(*) INTO the_limit
FROM my_table
WHERE my_column IS NULL;
UPDATE my_table
SET my_column = true
WHERE my_column IS NULL
LIMIT the_limit;
END$
As stated in previous posts, changing the default settings of the database server will result in undesired modification of existing data due to an incorrect query on the data in a published project. Therefore, to implement such commands as stated in previous posts, it is necessary to run them in a test environment on sample data and then execute them after testing them correctly.
My suggestion is to write a WHERE conditional statement that will loop through all the rows in all conditions if an update should work for all rows in a table. For example, if the table contains an ID value, the condition ID > 0 can be used to select all rows:
/**
* For successful result, "id" column must be "Not Null (NN)" and defined in
* INT data type. In addition, the "id" column in the table must have PK, UQ
* and AI attributes.
*/
UPDATE schema_name.table_name
SET first_column_name = first_value, second_column_name = second_value, ...
WHERE id > 0;
If the table does not contain an id column, the update operation can be run on all rows by checking a column that cannot be null:
/**
* "first_column_name" column must be "Not Null (NN)" for successful result.
*/
UPDATE schema_name.table_name
SET first_column_name = first_value, second_column_name = second_value, ...
WHERE table_name.first_column_name IS NOT NULL;
MySql workbench gave me the same error, after I unchecked safe mode , I then reconnected the server and the update function worked.
Go to Query in the menu bar and reconnect the server
Query Menu -> Reconnect to Server
You can enable and disable safe update option by following commands.
To Disable,
SET SQL_SAFE_UPDATES=0;
or
SET SQL_SAFE_UPDATES=OFF;
To Enable,
SET SQL_SAFE_UPDATES=1;
or
SET SQL_SAFE_UPDATES=ON;
First:
Please make sure you want to update all records in that table because without the where clause it is dangerous to update all records in that table. It's rare time you want to update all records in the table.
most of the time you want to update specific records which should include where cluase if again you want to update all records open MySQL workbench> Edit> Preference>SQL Editor > scroll down at right and uncheck the "Safe Updates(rejects UPDATEs and DELETEs with no restrictions)".
It is for safe updates.
If you uncheck the above said then there are chances that you update all records instead of one record which leads to a database backup restore. there is no rollback.
I've just added COMMIT; in the end
You can enable and disable safe update option by following commands.
SET SQL_SAFE_UPDATES=1;