Latest web development tutorials

SQL DELETE statement

DELETE statement to delete records in the table is used.


SQL DELETE statement

DELETE statement to delete rows in a table.

SQL DELETE syntax

DELETE FROM table_name
WHERE some_column = some_value ;

lamp Note that SQL DELETE statement WHERE clause!
WHERE clause specifies which record or records which need to be removed. If you omit the WHERE clause, all records will be deleted!


The demo database

In this tutorial, we will use w3big sample database.

The following is a selected "Websites" table data:

+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | https://www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | https://www.taobao.com/   | 13    | CN      |
| 3  | 本教程      | http://www.w3big.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | https://www.facebook.com/ | 3     | USA     |
|  6 | 百度         | https://www.baidu.com/    |     4 | CN      |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+


SQL DELETE examples

Suppose we "Websites" list to remove from the site called "Baidu" and the country is CN's website.

We use the following SQL statement:

Examples

DELETE FROM Websites WHERE name = 'Baidu' AND country = 'CN';

Execute the above SQL, then read the "Websites" list, the data is as follows:


Delete all data

You can delete the table without conditions, delete all the rows in the table. This means that the table structure, attributes, indexes will remain unchanged:

DELETE FROM table_name;

or

DELETE * FROM table_name;

NOTE: When youdelete a record to be extra careful! Because you can not be repeated!