mysql delete under safe mode

前端 未结 4 2104
醉梦人生
醉梦人生 2020-12-12 12:41

I have a table instructor and I want to delete the records that have salary in a range An intuitive way is like this:

delete from instructor where salary be         


        
相关标签:
4条回答
  • 2020-12-12 13:22

    I have a far more simple solution, it is working for me; it is also a workaround but might be usable and you dont have to change your settings. I assume you can use value that will never be there, then you use it on your WHERE clause

    DELETE FROM MyTable WHERE MyField IS_NOT_EQUAL AnyValueNoItemOnMyFieldWillEverHave

    I don't like that solution either too much, that's why I am here, but it works and it seems better than what it has been answered

    0 讨论(0)
  • 2020-12-12 13:24

    Googling around, the popular answer seems to be "just turn off safe mode":

    SET SQL_SAFE_UPDATES = 0;
    DELETE FROM instructor WHERE salary BETWEEN 13000 AND 15000;
    SET SQL_SAFE_UPDATES = 1;
    

    If I'm honest, I can't say I've ever made a habit of running in safe mode. Still, I'm not entirely comfortable with this answer since it just assumes you should go change your database config every time you run into a problem.

    So, your second query is closer to the mark, but hits another problem: MySQL applies a few restrictions to subqueries, and one of them is that you can't modify a table while selecting from it in a subquery.

    Quoting from the MySQL manual, Restrictions on Subqueries:

    In general, you cannot modify a table and select from the same table in a subquery. For example, this limitation applies to statements of the following forms:

    DELETE FROM t WHERE ... (SELECT ... FROM t ...);
    UPDATE t ... WHERE col = (SELECT ... FROM t ...);
    {INSERT|REPLACE} INTO t (SELECT ... FROM t ...);
    

    Exception: The preceding prohibition does not apply if you are using a subquery for the modified table in the FROM clause. Example:

    UPDATE t ... WHERE col = (SELECT * FROM (SELECT ... FROM t...) AS _t ...);
    

    Here the result from the subquery in the FROM clause is stored as a temporary table, so the relevant rows in t have already been selected by the time the update to t takes place.

    That last bit is your answer. Select target IDs in a temporary table, then delete by referencing the IDs in that table:

    DELETE FROM instructor WHERE id IN (
      SELECT temp.id FROM (
        SELECT id FROM instructor WHERE salary BETWEEN 13000 AND 15000
      ) AS temp
    );
    

    SQLFiddle demo.

    0 讨论(0)
  • 2020-12-12 13:42

    You can trick MySQL into thinking you are actually specifying a primary key column. This allows you to "override" safe mode.

    Assuming you have a table with an auto-incrementing numeric primary key, you could do the following:

    DELETE FROM tbl WHERE id <> 0
    
    0 讨论(0)
  • 2020-12-12 13:42

    Turning off safe mode in Mysql workbench 6.3.4.0

    Edit menu => Preferences => SQL Editor : Other section: click on "Safe updates" ... to uncheck option

    Workbench Preferences

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