MySQL
DELETE
The DELETE statement in MySQL is used to remove one or more rows from a table. This operation is crucial when you need to remove outdated or incorrect records from your database. For instance, in a company’s database, you may want to delete an employee’s record when they leave the company or remove a branch that has been closed. The DELETE statement can be used in conjunction with conditions to target specific rows for deletion, ensuring that only the intended data is removed.
Example Scenario:
You have tables for company, employees, departments, and branches, and you need to delete specific records based on certain conditions, such as removing an employee who has resigned.
Steps to Use the DELETE Operation:
- Basic Syntax of DELETE
- The basic format of the DELETE statement is as follows:
DELETE FROM table_name WHERE condition;
- Explanation:
table_namerefers to the table from which you want to delete rows.conditionspecifies which rows should be deleted. Without a condition, all rows in the table will be deleted.
- The basic format of the DELETE statement is as follows:
- Example 1: DELETE from
employeesTable- Suppose the
employeestable has the following structure:CREATE TABLE employees ( employee_id INT AUTO_INCREMENT, first_name VARCHAR(50), last_name VARCHAR(50), department_id INT, salary DECIMAL(10, 2), hire_date DATE, PRIMARY KEY (employee_id) );
- To delete an employee with
employee_id = 5,use the following statement:DELETE FROM employees WHERE employee_id = 5;
- Explanation:
- This deletes the employee with the ID 5 from the
employeestable.
- This deletes the employee with the ID 5 from the
- Suppose the
- Example 2: DELETE with Multiple Conditions
- You can use multiple conditions in the WHERE clause to target specific rows for deletion. For example, to delete an employee from the
employeestable who is in department2and has a salary less than 50,000:DELETE FROM employees WHERE department_id = 2 AND salary < 50000;
- Explanation:
- This deletes all employees from department 2 who have a salary less than 50,000.
- You can use multiple conditions in the WHERE clause to target specific rows for deletion. For example, to delete an employee from the
- Example 3: DELETE from
departmentsTable- Suppose the
departmentstable has the following structure:CREATE TABLE departments ( department_id INT AUTO_INCREMENT, department_name VARCHAR(50), branch_id INT, PRIMARY KEY (department_id) );
- To delete a department with
department_id = 3:DELETE FROM departments WHERE department_id = 3;
- Explanation:
- This removes the department with the ID 3 from the
departmentstable.
- This removes the department with the ID 3 from the
- Suppose the
- Using DELETE Without a WHERE Clause
- If you use DELETE without a WHERE clause, it will delete all rows in the table:
DELETE FROM employees; - Explanation:
- This command deletes all records in the
employeestable. Use this with caution as it cannot be undone without a backup.
- This command deletes all records in the
- If you use DELETE without a WHERE clause, it will delete all rows in the table:
- Example 4: DELETE from
branchesTable- Suppose the
branchestable has the following structure:CREATE TABLE branches ( branch_id INT AUTO_INCREMENT, branch_name VARCHAR(100), location VARCHAR(100), PRIMARY KEY (branch_id) );
- To delete a branch located in 'New York':
DELETE FROM branches WHERE location = 'New York';
- Explanation:
- This removes all branches that are located in New York.
- Suppose the
Notes on Using the DELETE Operation:
- Important!
- If you omit the WHERE clause, all rows in the table will be deleted. Always ensure that you have a specific WHERE condition to avoid accidental data loss.
- Foreign Key Constraints:
- If your table has foreign key constraints, attempting to delete a row may result in an error if other rows in related tables reference the row you’re trying to delete. Use ON DELETE CASCADE to automatically delete related rows when a row is deleted
- Use with Transaction Control:
- It's recommended to use DELETE statements within transactions, especially when deleting multiple rows. This allows you to ROLLBACK in case something goes wrong:
START TRANSACTION; DELETE FROM employees WHERE employee_id = 10; COMMIT;
- It's recommended to use DELETE statements within transactions, especially when deleting multiple rows. This allows you to ROLLBACK in case something goes wrong:
- Real-World Usage in a Company Database:
- In a company database, you can use the DELETE operation to remove employees who have left the company, delete closed departments or branches, or remove other outdated information.
- Safe Deletion Practices:
- Before running a DELETE command, it’s often a good idea to first run a SELECT query to ensure that the correct rows are targeted. For example:
SELECT * FROM employees WHERE employee_id = 5;
By following these steps, beginners can learn how to effectively use the DELETE operation in MySQL to remove specific records from tables such as employees, departments, and branches. This operation is essential for maintaining an accurate and up-to-date company database.
To gain complete access, login with gmail or outlook, no need of signup, click here


Comments Not Found