MySQL数据库之mysql delete语句删除数据
小标 2019-01-04 来源 : 阅读 2013 评论 0

摘要:本文主要向大家介绍了MySQL数据库之mysql delete语句删除数据 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

本文主要向大家介绍了MySQL数据库之mysql delete语句删除数据 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。


Summary: in this tutorial, you will learn how to use the MySQL DELETE statement to remove data from one ore more database tables.

To remove data from a table, you use the MySQL DELETEstatement. The MySQL DELETE statement allows you to remove records from not only one table but also multiple tables using a single DELETE statement.

MySQL delete from one table

To remove data from a single table, you use the following DELETE statement:

DELETE FROM table    [WHERE conditions] [ORDER BY ...] [LIMIT rows]

Followed the DELETE FROM clause is the table name that you want to delete records.

The WHERE clause specifies which rows you want to delete. If a record meets the WHERE condition, it is deleted permanently from the table. If you omit the WHERE clause, all records in the table are deleted.

The DELETE statement returns the number of rows deleted specified by the ROW_COUNT() function.

Notice that the ROW_COUNT() function returns the number of rows inserted, updated or deleted by the last INSERT, UPDATE or DELETE statement

MySQL delete from a table examples

Suppose you want to remove employees whose officeNumberis 4, you use the DELETE statement with the WHEREclause as the following query:

DELETE FROM employees  WHERE officeCode = 4

To delete all employee records from the employees table, you use the DELETE statement without the WHERE clause as follows:

DELETE FROM employees

All employee records in the employees table were deleted.

MySQL delete from multiple tables

To delete records from multiple tables, you use one of the following DELETE statements:

DELETE table_1, table_2,... FROM table-refs    [WHERE conditions] DELETE FROM table_1, table_2,... USING table-refs [WHERE conditions]

Let’s examine each statement in greater detail:

Both DELETE statements delete records from multiple tables i.e., table_1, table_2,… specified after the DELETE keyword.

The first DELETE statement uses the FROM clause while the second one use the USING clause.

The WHERE clause is used to determine which record to be deleted.

Both statements return the number of rows deleted from the multiple tables.

MySQL delete from multiple tables example

Let’s take a look at the offices and employees tables in the MySQL sample database.


Suppose one office is closed and you want to remove all employee records in the employees table associated with that office and also the office itself in the offices table, you can use the second form of the MySQL DELETEstatement

The following query removes the office record with the code is 1 in the offices table and also all employee records associated with the office 1 in the employees table:

DELETE employees,     offices  FROM employees,   offices  WHERE employees.officeCode = offices.officeCode  AND        offices.officeCode = 1

You can verify the changes by using the following SELECT statements to query data from both employees table and offices table.

SELECT * FROM employees  WHERE officeCode = 1; SELECT * FROM offices WHERE officeCode = 1;

You can achieve the same effect by using the second form of the MySQL DELETE statement as follows:

DELETE FROM employees, offices  USING employees, offices  WHERE employees.officeCode = offices.officeCode  AND        offices.officeCode = 1

In this tutorial, you have learned various forms of the MySQL DELETE statement to delete records from one or more tables.


本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程