How to Delete Data from a MySQL Table

08/03/2021

Contents

In this article, you will learn how to delete data from a MySQL table.

Deleting data from a MySQL table

Deleting data from a MySQL table is a common operation in database management.

Connect to MySQL

Before we can delete data from a MySQL table, we need to connect to the MySQL server using a client like MySQL Workbench, phpMyAdmin or through command line.

Identify the table to delete data from

Once you are connected to the MySQL server, identify the table that you want to delete data from. Use the following command to view all tables in the current database:

SHOW TABLES;

Backup the data

Before deleting data, it is important to backup the data to avoid losing any valuable information. This can be done using the mysqldump command.

Use the DELETE statement

To delete data from a MySQL table, use the DELETE statement. The DELETE statement allows you to delete specific rows or all rows from a table.

DELETE FROM table_name WHERE condition;

The table_name is the name of the table from which you want to delete data. The WHERE clause is used to specify the condition that must be met in order for the rows to be deleted. If you omit the WHERE clause, all rows in the table will be deleted.

Example

Delete a single row
DELETE FROM users WHERE user_id = 123;

This will delete the row from the users table where the user_id is 123.

Delete multiple rows
DELETE FROM users WHERE user_type = 'inactive';

This will delete all rows from the users table where the user_type is ‘inactive’.

Delete all rows
DELETE FROM users;

This will delete all rows from the users table.

Confirm the data has been deleted

Once you have executed the DELETE statement, confirm that the data has been deleted by querying the table.