How can MySQL effectively prevent database deletion and running away?

How can MySQL effectively prevent database deletion and running away?

You must have heard that some developers, due to personal mistakes, did not add a where clause when deleting or updating statements, resulting in confusion in the entire table data.

MySQL safe mode: MySQL will report an error when it finds that the delete or update statement does not have a where or limit condition. The entire SQL statement will not be executed, effectively preventing the accidental deletion of tables.

Safe Mode Settings

Check the status in mysql using the following command:

 show variables like 'sql_safe_updates';

insert image description here

The default state is OFF, just set the state to ON:

  • set sql_safe_updates=1; //Open
  • set sql_safe_updates=0; //Close

After setting to ON

  • Update statement : If there is no index available for the column in the where condition and there is no limit restriction, the update will be rejected. If the where condition is a constant and there is no limit, the update will be rejected.
  • Delete statement: ①The where condition is a constant, ②or the where condition is empty, ③or the column in the where condition has no index available and no limit restriction, the deletion is rejected.

test

Test in safe mode

1. Update and delete without where clause

delete from t_user

delete from t_user
> 1175 - You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
> Time: 0.001s

update t_user set name='123'

update t_user set name='123'
> 1175 - You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
> Time: 0.001s

2. Delete of non-index keys

delete from t_user where name='123'

delete from t_user where name='123'
> 1175 - You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column
> Time: 0.007s

If the where condition of delete is not an index key, you must add a limit.

delete from t_user where name='123' limit 1

delete from t_user where name='123' limit 1
> Affected rows: 0
> Time: 0.002s

3. Delete index key

delete from t_user where group_id='123'

delete from t_user where group_id='123'
> Affected rows: 0
> Time: 0s

Summarize

If sql_safe_updates=1 is set, the update statement must meet one of the following conditions to be executed successfully

  • Use the where clause, and the column in the where clause must be the prefix index column
  • Using limit
  • Use the where clause and limit at the same time (the column in the where clause does not need to be an index column)

The delete statement must meet one of the following conditions to be executed successfully

  • Use the where clause, and the column in the where clause must be the prefix index column
  • The execution can be successful only if the where clause and limit (the column in the where clause does not need to be an index column) are used at the same time.

This is the end of this article about how to effectively prevent MySQL from deleting databases. For more information about preventing MySQL from deleting databases, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL commonly used SQL and commands from entry to deleting database and running away
  • In-depth analysis of MySQL from deleting the database to running away_Advanced (I) - Data Integrity
  • Deleting the database and running away? How to use xtraback to back up the MySQL database

<<:  Detailed explanation of the setting of background-image attribute in HTML

>>:  Detailed explanation of tinyMCE usage and experience

Recommend

JavaScript uses canvas to draw coordinates and lines

This article shares the specific code of using ca...

Simple example of limit parameter of mysql paging

Two parameters of Mysql paging select * from user...

A brief discussion on size units in CSS

The compatibility of browsers is getting better a...

7 useful new TypeScript features

Table of contents 1. Optional Chaining 2. Null va...

JavaScript color viewer

This article example shares the specific code of ...

How to handle the tcp_mark_head_lost error reported by the Linux system

Problem Description Recently, a host reported the...

Linux CentOS 6.5 Uninstall, tar and install MySQL tutorial

Uninstall the system-provided MySQL 1. Check whet...

Linux kernel device driver character device driver notes

/******************** * Character device driver**...

Distinguishing between Linux hard links and soft links

In Linux, there are two types of file connections...

Docker cleaning killer/Docker overlay file takes up too much disk space

[Looking at all the migration files on the Intern...

How to solve "Unable to start mysql service error 1069"

Today, when I was on the road, a colleague sent m...

JavaScript built-in date and time formatting time example code

1. Basic knowledge (methods of date objects) 😜 ge...

How to build a standardized vmware image for kubernetes under rancher

When learning kubernetes, we need to practice in ...

In-depth understanding of JavaScript callback functions

Table of contents Preface Quick Review: JavaScrip...

How to use async and await correctly in JS loops

Table of contents Overview (Loop Mode - Common) D...