Detailed explanation of how to limit the update/delete range using the mysql parameter sql_safe_updates

Detailed explanation of how to limit the update/delete range using the mysql parameter sql_safe_updates

Preface

As you all know, we have encountered many cases in MySQL operation and maintenance where data was mistakenly updated or deleted due to incorrect update/delete conditions. To avoid similar problems, you can use the sql_safe_updates parameter to restrict update/delete. When this parameter is set to on, it can prevent the entire table from being updated or deleted due to program bugs or DBA manual errors. Without further ado, let’s take a look at the detailed introduction.

There are a few things to note when setting this parameter:

a. Before setting, you need to confirm that all updates and deletes in the program comply with the restrictions of sql_safe_updates, otherwise the program will report an error.

b. 5.0 and 5.1 are both session-level, 5.6 is global&session-level; for lower-version databases, you can only set sql_safe_updates=on; for higher-version databases, you can directly set global set sql_safe_updates=on , and after the setting is completed, let the program reconnect to take effect.

Restriction Specifications:

Example table structure:

CREATE TABLE `delay_monitor` (
 `id` int(11) NOT NULL,
 `Ftime` datetime DEFAULT NULL,
 `Fgtid` varchar(128) NOT NULL DEFAULT '',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin​

1. Update

a. Error conditions: without where, with where but no index, where condition is a constant

Without where: update delay_monitor set Ftime=now();

With where and no index: update delay_monitor set Ftime=now() where Fgtid='test';

Where the condition is a constant: update delay_monitor set Ftime=now() where 1;

b. Execution conditions: with where and index, without where + limit, with where and no index + limit, with where and index + limit, where condition is constant + limit

With where and index: update delay_monitor set Ftime=now() where id=2;

Without where + with limit: update delay_monitor set Ftime=now() limit 1;

With where no index + limit: update delay_monitor set Ftime=now() where Fgtid='test' limit 1;

With where index + limit: update delay_monitor set Ftime=now() where id =2 limit1;

Where the condition is constant + limit: update delay_monitor set Ftime=now() where 1 limit 1;

2. delete

Compared with update, delete has stricter restrictions; if the where condition is a constant or empty, it will not be executed.

a. Error conditions: without where, with where but no index, without where + with limit, where condition is a constant, where condition is a constant + limit

Without where: delete delay_monitor set Ftime=now();

With where and no index: delete delay_monitor set Ftime=now() where Fgtid='test';

Without where + with limit: delete delay_monitor set Ftime=now() limit 1;

Where the condition is a constant: delete delay_monitor set Ftime=now() where 1;

where condition is constant + limit: delete delay_monitor set Ftime=now() where 1 limit 1;

b. Execution conditions: with where and index, with where without index + limit, with where with index + limit

With where and index: delete delay_monitor set Ftime=now() where id=2;

With where no index + limit: delete delay_monitor set Ftime=now() where Fgtid='test' limit 1;

With where index + limit: delete delay_monitor set Ftime=now() where id =2 limit1;

The summary is as follows: key means all, const means constant

operate no where where key where nokey limit where nokey+limit where key+limit where const where const+limit
delete NO YES NO NO YES YES NO NO
update NO YES NO YES YES YES NO YES

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the use of the MySQL parameter sql_safe_updates in the production environment

<<:  Analyze the selection problem of storing time and date types in MySQL

>>:  Detailed explanation of using top command to analyze Linux system performance

Recommend

How to use stored procedures in MySQL to quickly generate 1 million records

Preface When testing, in order to test the projec...

MySQL 5.7 Common Data Types

——Notes from "MySQL in Simple Terms (Second ...

How to use Docker to build enterprise-level custom images

Preface Before leaving get off work, the author r...

Analyze the problem of pulling down the Oracle 11g image configuration in Docker

1. Pull the image docker pull registry.cn-hangzho...

Mysql join query syntax and examples

Connection query: It is the result of connecting ...

MySQL million-level data paging query optimization solution

When there are tens of thousands of records in th...

Docker image export, import and copy example analysis

The first solution is to push the image to a publ...

WeChat applet realizes multi-line text scrolling effect

This article example shares the specific code for...

CSS realizes the layout method of fixed left and adaptive right

1. Floating layout 1. Let the fixed width div flo...

How to start a Java program in docker

Create a simple Spring boot web project Use the i...

Using better-scroll component in Vue to realize horizontal scrolling function

About Recently, in the process of learning Vue, I...

Detailed steps to upgrade mysql8.0.11 to mysql8.0.17 under win2008

Upgrade background: In order to solve the vulnera...