Pre-requisites:
You have to create a database table with the data in a MySQL database to check the use of Safe Update Mode in MySQL.
Open the terminal and connect with the MySQL server by executing the following command.
Run the following command to create a database named test_db.
Run the following command to select the database.
Run the following query to create a table named customers with five fields.
id INTNOT NULL PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR(50),
address TEXT,
contact_no VARCHAR(15));
Run the following INSERT query to insert four records into the customers table.
('4001', 'Laboni Sarkar', '[email protected]', '34, Dhanmondi 9/A, Dhaka.', '01844767234'),
('4002', 'Tahsin Ahmed', '[email protected]', '123/1, Jigatola, Dhaka.', '015993487812'),
('4003', 'Hasina Pervin', '[email protected]', '280, Shantibagh, Dhaka.', NULL),
('4004', 'Mehrab Ali', '[email protected]', '78, paltan, Dhaka..', '01727863459');
Checking the current value of the safe update mode:
The sql_safe_updates variable is used to enable or disable the safe update mode of MySQL. The safe update mode is disabled by default. Run the following command from the mysql prompt to check the current value of the sql_safe_updates variable.
The following output shows that the safe update mode is disabled.
Execute update and delete command after enabling the safe update mode:
Run the following commands to enable the safe update mode and check the value of the sql_safe_updates variable.
SHOW VARIABLES LIKE "sql_safe_updates";
The following output shows that the safe update mode is enabled now.
Execute UPDATE query:
Run the UPDATE command without the WHERE clause to check the safe update mode is working or not for the update operation. If the safe update mode is enabled, the error 1175 will be generated for the UPDATE query.
The following output shows an error generated for the UPDATE query without WHERE clause, and no UPDATE operation has been done.
Suppose the above UPDATE query will be executed from any graphical user interface like PHPMyAdmin where the safe update mode is disabled by default. In that case, the update operation will be done based on the user’s permission. If the user gives permission, then the contact_no field of all records of the customers table will be updated; otherwise, nothing will be done.
Execute DELETE query:
Run the DELETE command without WHERE clause to check the safe update mode is working or not for the delete operation. If the safe update mode is enabled, the error 1175 will be generated for the DELETE query like the UPDATE query.
The following output shows an error generated for the DELETE query without WHERE clause, and no DELETE operation has been done.
Suppose the above DELETE query will be executed from any graphical user interface like PHPMyAdmin where the safe update mode is disabled by default. In that case, the delete operation will be done based on the user’s permission, like the update query. If the user gives permission, then all records of the customers table will be deleted; otherwise, nothing will be done.
Execute update and delete command after disabling the safe update mode:
Run the following commands to disable the safe update mode and check the value of the sql_safe_updates variable.
SHOW VARIABLES LIKE "sql_safe_updates";
The following output shows that the safe update mode is disabled now.
Like the previous UPDATE command, run the following UPDATE command without WHERE clause to check the update operation works or not after disabling the safe update mode.
The following output shows that the update operation has been completed without any error, and four rows of the customers table have been affected.
Run the following SELECT query to check the current content of the customers table.
The following output shows that the string value has updated all address field values, ‘Dhaka’.
Like the previous DELETE command, run the following DELETE command without WHERE clause to check the delete operation works or not after disabling the safe update mode.
The following output shows that the delete operation has been completed without error.
Run the following SELECT query to check the current content of the customers table.
The following output shows that all records of the customers table have been deleted.
Conclusion:
The safe update mode of MySQL is an essential feature to prevent unwanted updates or delete operations by mistake. The way of enabling and disabling this mode by setting the required variable has been shown in this tutorial. How the update and delete operations without the WHERE clause work based on the safe update mode have also been shown here by using a table.