SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL Delete - SQL基础教程 - 笔下光年
网站首页
SQL Delete
In this tutorial you will learn how to delete records from a database table using SQL. ## Deleting Data from Tables Just as you insert records into a table with the INSERT statement, you can delete records from a table as well with the `DELETE` statement. ## Syntax The `DELETE` statement is used to remove one or more rows from a table. ```sql DELETE FROM table_name WHERE condition; ``` <div class="callout callout-danger mb-3">Warning: The WHERE clause in the DELETE statement specifies which record or records should be deleted. It is however optional, but if you omit or forget the WHERE clause, all the records will be deleted permanently from the table.</div> Let's delete some records from the persons table that we've created in create table chapter. Suppose that our persons table currently has the following records: ```sql +----+--------------------+------------+-------------+ | id | name | birth_date | phone | +----+--------------------+------------+-------------+ | 1 | Peter Wilson | 1990-07-15 | 0711-020361 | | 2 | Carrie Simpson | 1995-05-01 | 0251-031259 | | 3 | Victoria Ashworth | 1996-10-17 | 0695-346721 | | 4 | George Bailey | 1993-03-05 | 0897-034214 | | 5 | Norman Bates | 1999-08-25 | 0522-556721 | +----+--------------------+------------+-------------+ ``` ## Delete Records Based on Conditions The following statement will delete the rows from persons table where id is greater than 3. ```sql DELETE FROM persons WHERE id > 3; ``` After executing the query, the persons table will look something like this: ```sql +----+--------------------+------------+-------------+ | id | name | birth_date | phone | +----+--------------------+------------+-------------+ | 1 | Peter Wilson | 1990-07-15 | 0711-020361 | | 2 | Carrie Simpson | 1995-05-01 | 0251-031259 | | 3 | Victoria Ashworth | 1996-10-17 | 0695-346721 | +----+--------------------+------------+-------------+ ``` <div class="callout callout-info mb-3">Note: See the tutorial on SQL WHERE clause to know how to form complex query based on multiple conditions while deleting the records from the tables.</div> ## Delete All Data Similarly, as mentioned above if you do not specify the WHERE clause in the DELETE statement all the rows from the table will be deleted. However, the target table itself won't be deleted that means the table structure, attributes, and indexes will remain intact. The following statement will remove all the records from the persons table: ```sql DELETE FROM persons; ``` Now, if you try to select the records from the persons table you'll get an empty result set.
上一篇:
SQL Update
下一篇:
SQL Truncate Table