Delete MySQL table data

In this tutorial, you will learn how to use MySQL DELETEstatement to delete data from a single table.

1. MySQL DELETE statement presentation

To delete data from a table, use the MySQL DELETEstatement. The following illustrates the DELETEsyntax of the statement:

DELETE FROM table_name
WHERE condition;
SQL

In the above query statement -

  • First, specify the deleted table data ( table_name).
  • Secondly, to specify the conditions of use in the WHEREdelete clause rows. If the line matching condition, these rows will be deleted.

Please note that WHEREclause is optional. If you omit the WHEREclause, the DELETEstatement will delete all the rows in the table.

In addition to delete data from the table, the DELETEstatement returns the number of rows deleted.

To use a single DELETEstatement 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 employeestable 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 
SQL

Please note that once you delete data, it will disappear forever. Therefore, the implementation of DELETEprior statements, you should back up the database , just in case you want to recover deleted or data.

Suppose you want to delete officeNumberto 4employees, use the DELETEstatements and WHEREclauses as the following query:

DELETE FROM employees 
WHERE
    officeCode = 4; 
SQL

To delete employeesall the rows in the table, use without a WHERE clause DELETEstatement as follows:

DELETE FROM employees;
SQL

After executing the above query, employeesall 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; 
SQL

Note that the line does not specify the order in the table, so when you use LIMITwhen clause should always use the ORDER BYclause, or delete records may not be what you expect.

DELETE FROM table_name
ORDER BY c1, c2, ... LIMIT row_count; 
SQL

Consider the example database (yiibaidb) in the customerstable, 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 
SQL

For example, the following statement by customer name alphabetically sorted customer, and delete the first 10customers:

DELETE FROM customers
ORDER BY customerName
LIMIT 10; 
SQL

Similarly, the following DELETEstatement selects France ( France customers), and in ascending order by credit ( creditLimit) to sort and delete the first 5customers:

DELETE FROM customers
WHERE country = 'France' ORDER BY creditLimit LIMIT 5; 
SQL

In this tutorial, you've learned how to use MySQL DELETEstatement to delete data from the table.



Guess you like

Origin www.cnblogs.com/borter/p/12452684.html