ChatGPT解决这个技术问题 Extra ChatGPT

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?

Are you aware that this will update all rows in your table where visited = 0 to become visited = 1? Is this what you want?
After unchecking "Safe Updates" follow the step below: Query --> Reconnect to Server . Now execute your query
You must reconnect to MySQL Server (restart the MySQL connection) before this change takes affect.

j
juanes

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


Be sure to set this back with SET SQL_SAFE_UPDATES = 1 when you're done, since it is a worthwhile safety feature.
Thank you for keeping it simple.
What if SQL_SAFE_UPDATES was already off? Then calling SET SQL_SAFE_UPDATES = 0 would turn it off (again), but following it with SET SQL_SAFE_UPDATES = 1 would end up turning it on for the rest of the script, presumably even overriding the configured defaults. Perhaps using @@SQL_SAFE_UPDATES to read the current value, then restoring the old value afterwords would be a better approach?
I have accessed this answer more than any other answer on SO, you should get a special award, or I should commit this to memory.
I
Ivar

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!


For version 6.3 step 2 should be "Sql Editor" instead of "Sql Queries" and then there's a check box at the bottom for "Safe Updates"
there's no preferences under the edit menu in my workbench
This was helpful. Thanks.
by the way,if you cant see the "Safe Mode" option, try resizing the dialog box by click and holding the bottom right corner, and pulling. On Ubuntu, these options were hidden on a 1920x1080 screen.
J
Joundill
SET SQL_SAFE_UPDATES = 0;

# your code SQL here

SET SQL_SAFE_UPDATES = 1;

pls could mark this answer as correct one? thanks :)
u
user2531028
SET SQL_SAFE_UPDATES=0;
UPDATE tablename SET columnname=1;
SET SQL_SAFE_UPDATES=1;

R
Rudy De Volder

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.


Haha, that's a good trick. I used WHERE id > 0 as a trick along this line.
It doesn't work for me, it continue showing me the same Message. I resolved it By deactivating the safe update mode by : -Edit -> Preferences -> Sql Editor then uncheck Safe update.
How is this actually making it any safer, though?
Every other comment here are misguiding and you have managed to let us know why the solution occurred rather than how to bypass it. Also would have been great if you would have mentioned why the query is safe now by including the primary key column to the where clause(although one can google it out). Thank you so much ;)
@MattMessersmith This feature tries to prevent you to do an update/delete based on a non key column that can potentially affect (thus accidentally change or delete) most or all of your rows. This answer's suggestion doesn't make the query safer, it is a workaround to bypass an otherwise helpful security feature without permanently disabling it. This solution is not for production code, but for queries you run by hand. By doing it this way, the security feature makes you conscious about the impact of the query when you type it down.
k
kuttan pillai

All that's needed is: Start a new query and run:

SET SQL_SAFE_UPDATES = 0;

Then: Run the query that you were trying to run that wasn't previously working.


Welcome to SO, be sure to read the tour page and help page, and avoid posting answers like this one because many answers below this one say the exact same thing and have been posted months ago
You may also enable this option again after running the query.
What does this answer add? You just posted the same thing as Habibillah, more than two years later. Trying to reap karma?
T
Tính Ngô Quang

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"

https://i.stack.imgur.com/teRJd.png

MySQL Workbench => [Edit] => [Preferences] => [SQL Queries]

a
andrew

Preferences... "Safe Updates"... Restart server

https://i.stack.imgur.com/101Ys.png

https://i.stack.imgur.com/sDVA1.png


This should have been the accepted answer .it is a pity that it has not been
You shouldn't have to restart server. It should be sufficient to simply close and reopen the connection.
In MAC, preferences is under MySQLWorkbench top menu.
R
Renier
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


G
Gilsha

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

This page confirms this fact: bennadel.com/blog/…
In case of deletion DELETE FROM tablename WHERE id > 0 works. Thanks
A
Abdelhadi Lahlou

On WorkBench I resolved it By deactivating the safe update mode:

-Edit -> Preferences -> Sql Editor then uncheck Safe update.


R
Ruwantha

The simplest solution is to define the row limit and execute. This is done for safety purposes.


+1 on this solution. Delete from TABLE where column = 'xyz' limit 9999999999
This answer should be the accepted answer because it is the least unsafe way.
K
Knowledge Craving

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.


You can avoid to mention schema name by selecting schema at left panel. Selecting schema at left panel means you are using the selected schema/database
This is not the answer to solve the issue described in the question (and the title). You should mark another as accepted.
I tried with schemaname but getting same error, update qms-active-db.gh_table set bookmark='1660_207100000001000'
This is not the answer!
wrong answer,correct answer is answered by Habibillah (below)
f
ferdiado

In the MySQL Workbech version 6.2 don't exits the PreferenceSQLQueriesoptions.

SET SQL_SAFE_UPDATES=0;


It exists, but the option is under "SQL Editor" now.
I don't see this option. Please you can get a screenshot. Thanks
Open Preferences --> SQL Editor tab --> At the bottom. A 6.2-beta release lacked this option, so perhaps you'll need to upgrade to 6.2.3+.
my version is 6.3,It's under -->SQL Editor -->Other
u
urig

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..


J
Jainmiah

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;


In my case I only have this part "To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect." I don't understand why. Thx for the tip
S
Sonic-Mayhem

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

K
Krishnadas PC

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

https://i.stack.imgur.com/edTvd.png

On the new window, uncheck the option Safe updates

https://i.stack.imgur.com/Kww6O.png

Then close and reopen the connection. No need to restart the service.

Now we are going to try the DELETE again with successful results.

https://i.stack.imgur.com/rZPBu.png

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.


H
Hilal Aissani

just type SET SQL_SAFE_UPDATES = 0; before the delete or update and set to 1 again SET SQL_SAFE_UPDATES = 1


a
alayor

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$

S
Sercan

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;

N
Nwankwo Fortune

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


F
Feridun

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.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now