MySQL error code: 1175 during UPDATE in MySQL Workbench

前端 未结 20 1005
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 09:58

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 workben

相关标签:
20条回答
  • 2020-11-22 10:51
    SET SQL_SAFE_UPDATES = 0;
    
    # your code SQL here
    
    SET SQL_SAFE_UPDATES = 1;
    
    0 讨论(0)
  • 2020-11-22 10:52

    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.

    0 讨论(0)
  • 2020-11-22 10:53

    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$
    
    0 讨论(0)
  • 2020-11-22 10:55

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

    0 讨论(0)
  • 2020-11-22 10:55

    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.

    0 讨论(0)
  • 2020-11-22 10:55

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

    In this case it's possible use: SET SQL_SAFE_UPDATES=0;

    0 讨论(0)
提交回复
热议问题