In this tutorial, you will learn how to use MySQL DELETE
statement to delete data from a single table.
1. MySQL DELETE statement presentation
To delete data from a table, use the MySQL DELETE
statement. The following illustrates the DELETE
syntax of the statement:
DELETE FROM table_name
WHERE condition;
In the above query statement -
- First, specify the deleted table data (
table_name
). - Secondly, to specify the conditions of use in the
WHERE
delete clause rows. If the line matching condition, these rows will be deleted.
Please note that
WHERE
clause is optional. If you omit theWHERE
clause, theDELETE
statement will delete all the rows in the table.
In addition to delete data from the table, the DELETE
statement returns the number of rows deleted.
To use a single DELETE
statement to delete data from multiple tables, the next read a tutorial introduction DELETE JOIN statement .
To delete all the rows in the table, without the need to know how many rows are deleted, you should use TRUNCATE TABLE statement to obtain better execution performance.
For a foreign key constraint table, when deleting rows from the parent table, the sub-table rows by using the ON DELETE CASCADE automatically delete option.
2. MySQL DELETE examples
We will use the sample database (yiibaidb) in the employees
table presentation.
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | emp_id | int(11) | NO | PRI | NULL | auto_increment | | emp_name | varchar(255) | NO | | NULL | | | performance | int(11) | YES | MUL | NULL | | | salary | float | YES | | NULL | | +-------------+--------------+------+-----+---------+----------------+ 4 rows in set
Please note that once you delete data, it will disappear forever. Therefore, the implementation of
DELETE
prior statements, you should back up the database , just in case you want to recover deleted or data.
Suppose you want to delete officeNumber
to 4
employees, use the DELETE
statements and WHERE
clauses as the following query:
DELETE FROM employees
WHERE
officeCode = 4;
To delete employees
all the rows in the table, use without a WHERE clause DELETE
statement as follows:
DELETE FROM employees;
After executing the above query, employees
all rows in the table is deleted.
MySQL DELETE and LIMIT clause
If you want to limit the number of rows to be deleted, use the LIMIT clause, as follows:
DELETE FROM table
LIMIT row_count;
Note that the line does not specify the order in the table, so when you use LIMIT
when clause should always use the ORDER BY
clause, or delete records may not be what you expect.
DELETE FROM table_name
ORDER BY c1, c2, ... LIMIT row_count;
Consider the example database (yiibaidb) in the customers
table, which table is structured as follows:
mysql> desc customers;
+------------------------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------------+---------------+------+-----+---------+-------+ | customerNumber | int(11) | NO | PRI | NULL | | | customerName | varchar(50) | NO | | NULL | | | contactLastName | varchar(50) | NO | | NULL | | | contactFirstName | varchar(50) | NO | | NULL | | | phone | varchar(50) | NO | | NULL | | | addressLine1 | varchar(50) | NO | | NULL | | | addressLine2 | varchar(50) | YES | | NULL | | | city | varchar(50) | NO | | NULL | | | state | varchar(50) | YES | | NULL | | | postalCode | varchar(15) | YES | | NULL | | | country | varchar(50) | NO | | NULL | | | salesRepEmployeeNumber | int(11) | YES | MUL | NULL | | | creditLimit | decimal(10,2) | YES | | NULL | | +------------------------+---------------+------+-----+---------+-------+ 13 rows in set
For example, the following statement by customer name alphabetically sorted customer, and delete the first 10
customers:
DELETE FROM customers
ORDER BY customerName
LIMIT 10;
Similarly, the following DELETE
statement selects France ( France customers), and in ascending order by credit ( creditLimit
) to sort and delete the first 5
customers:
DELETE FROM customers
WHERE country = 'France' ORDER BY creditLimit LIMIT 5;
In this tutorial, you've learned how to use MySQL DELETE
statement to delete data from the table.