SQL语法基础之DELETE语句

                                   作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。 

一.查看帮助信息

1>.查看DELETE的帮助信息

mysql> ? DELETE
Name: 'DELETE'
Description:
Syntax:
DELETE is a DML statement that removes rows from a table. A DELETE statement can start with a WITH clause to define common table
expressions accessible within the DELETE. See
http://dev.mysql.com/doc/refman/8.0/en/with.html. Single-Table Syntax DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[PARTITION (partition_name [, partition_name] ...)]
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count] The DELETE statement deletes rows from tbl_name and returns the number
of deleted rows. To check the number of deleted rows, call the
ROW_COUNT() function described in
http://dev.mysql.com/doc/refman/8.0/en/information-functions.html. Main Clauses The conditions in the optional WHERE clause identify which rows to
delete. With no WHERE clause, all rows are deleted. where_condition is an expression that evaluates to true for each row to
be deleted. It is specified as described in
http://dev.mysql.com/doc/refman/8.0/en/select.html. If the ORDER BY clause is specified, the rows are deleted in the order
that is specified. The LIMIT clause places a limit on the number of
rows that can be deleted. These clauses apply to single-table deletes,
but not multi-table deletes. Multiple-Table Syntax DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
tbl_name[.*] [, tbl_name[.*]] ...
FROM table_references
[WHERE where_condition] DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
FROM tbl_name[.*] [, tbl_name[.*]] ...
USING table_references
[WHERE where_condition] Privileges You need the DELETE privilege on a table to delete rows from it. You
need only the SELECT privilege for any columns that are only read, such
as those named in the WHERE clause. Performance When you do not need to know the number of deleted rows, the TRUNCATE
TABLE statement is a faster way to empty a table than a DELETE
statement with no WHERE clause. Unlike DELETE, TRUNCATE TABLE cannot be
used within a transaction or if you have a lock on the table. See [HELP
TRUNCATE TABLE] and [HELP LOCK]. The speed of delete operations may also be affected by factors
discussed in
http://dev.mysql.com/doc/refman/8.0/en/delete-optimization.html. To ensure that a given DELETE statement does not take too much time,
the MySQL-specific LIMIT row_count clause for DELETE specifies the
maximum number of rows to be deleted. If the number of rows to delete
is larger than the limit, repeat the DELETE statement until the number
of affected rows is less than the LIMIT value. Subqueries You cannot delete from a table and select from the same table in a
subquery. Partitioned Tables DELETE supports explicit partition selection using the PARTITION
option, which takes a list of the comma-separated names of one or more
partitions or subpartitions (or both) from which to select rows to be
dropped. Partitions not included in the list are ignored. Given a
partitioned table t with a partition named p0, executing the statement
DELETE FROM t PARTITION (p0) has the same effect on the table as
executing ALTER TABLE t TRUNCATE PARTITION (p0); in both cases, all
rows in partition p0 are dropped. PARTITION can be used along with a WHERE condition, in which case the
condition is tested only on rows in the listed partitions. For example,
DELETE FROM t PARTITION (p0) WHERE c < 5 deletes rows only from
partition p0 for which the condition c < 5 is true; rows in any other
partitions are not checked and thus not affected by the DELETE. The PARTITION option can also be used in multiple-table DELETE
statements. You can use up to one such option per table named in the
FROM option. For more information and examples, see
http://dev.mysql.com/doc/refman/8.0/en/partitioning-selection.html. URL: http://dev.mysql.com/doc/refman/8.0/en/delete.html mysql>

2>.相关关键字说明

  DELETE语句用于删除表中已经存储在的整行数据。查看该语句的帮助信息,如第1步所示。下面是对应的关键字说明

    • Tbl_name关键词代表删除数据的目标表

    • Where子句代表被删除数据的满足条件,如果没有where子句则代
表所有表数据都删除 • Order by子句代表删除数据的顺序 • Limit子句代表被删除数据的行数限制

二.Delete案例

1>.DELETE 单表删除举例

mysql> select * from students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | 尹正杰 | boy | 1 |
| 2 | 耿雨星 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
| 5 | 肖凤 | girl | 4 |
+-----+-----------+--------+---------+
5 rows in set (0.00 sec) mysql>
mysql> DELETE FROM students;
Query OK, 5 rows affected (0.00 sec) mysql>
mysql> select * from students;
Empty set (0.00 sec) mysql>

删除整张表数据(mysql> DELETE FROM students;)

mysql> select * from students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | 尹正杰 | boy | 1 |
| 2 | 耿雨星 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
| 5 | 肖凤 | girl | 4 |
+-----+-----------+--------+---------+
5 rows in set (0.00 sec) mysql>
mysql>
mysql> DELETE FROM students WHERE sid = 1 ;
Query OK, 1 row affected (0.00 sec) mysql>
mysql> select * from students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 2 | 耿雨星 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
| 5 | 肖凤 | girl | 4 |
+-----+-----------+--------+---------+
4 rows in set (0.00 sec) mysql>

使用WHERE语句过滤需要删除掉数据(mysql> DELETE FROM students WHERE sid = 1 ;)

mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | 尹正杰 | boy | 1 |
| 2 | 耿雨星 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
| 5 | 肖凤 | girl | 4 |
+-----+-----------+--------+---------+
5 rows in set (0.00 sec) mysql>
mysql>
mysql> DELETE FROM students ORDER BY sid;
Query OK, 5 rows affected (0.00 sec) mysql>
mysql> SELECT * FROM students;
Empty set (0.00 sec) mysql>

按照sid的值进行升序删除(mysql> DELETE FROM students ORDER BY sid; )

mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | 尹正杰 | boy | 1 |
| 2 | 耿雨星 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
| 5 | 肖凤 | girl | 4 |
+-----+-----------+--------+---------+
5 rows in set (0.00 sec) mysql>
mysql> DELETE FROM students ORDER BY sid LIMIT 1 ;
Query OK, 1 row affected (0.00 sec) mysql>
mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 2 | 耿雨星 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
| 5 | 肖凤 | girl | 4 |
+-----+-----------+--------+---------+
4 rows in set (0.00 sec) mysql>
mysql> DELETE FROM students ORDER BY sid DESC LIMIT 1 ;
Query OK, 1 row affected (0.00 sec) mysql>
mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 2 | 耿雨星 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
+-----+-----------+--------+---------+
3 rows in set (0.00 sec) mysql>

只删除先找到的一行(mysql> DELETE FROM students ORDER BY sid LIMIT 1 ;)

mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | 尹正杰 | boy | 1 |
| 2 | 耿雨星 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
| 5 | 肖凤 | girl | 4 |
+-----+-----------+--------+---------+
rows in set (0.00 sec) mysql>
mysql> DELETE FROM students WHERE dept_id = 1 ORDER BY sid DESC LIMIT 1;
Query OK, 1 row affected (0.00 sec) mysql>
mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | 尹正杰 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
| 5 | 肖凤 | girl | 4 |
+-----+-----------+--------+---------+
rows in set (0.00 sec) mysql>

Delete语句中的order by子句决定了删除数据的顺序,配合limit子 句后在某些情况下也非常有用,比如删除最老的一条记录(mysql> DELETE FROM students WHERE dept_id = 1 ORDER BY sid DESC LIMIT 1;)

2>.DELETE 多表删除语句语法

• 同样,被删除的表不能出现在查询子句的子查询中

• Low_priority关键词表示删除语句需要等待其他链接的读此表操作结束后再执行,只作用在MyISAM, MEMORY, and MERGE存储引擎

• Quick关键词是在使用myisam存储引擎时,删除操作不会合并删除表的索引叶节点,这样会在一定程度上加快删除的速度

• ignore关键词代表会忽略删除过程中的一些错误
mysql> SELECT * FROM Dept;
+----+-----------+
| id | demt_name |
+----+-----------+
| 1 | 运维 |
| 2 | 开发 |
| 3 | 测试 |
| 4 | 产品 |
| 5 | 运营 |
| 6 | 法务 |
+----+-----------+
6 rows in set (0.00 sec) mysql>
mysql>
mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | 尹正杰 | boy | 1 |
| 2 | 耿雨星 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
| 5 | 肖凤 | girl | 4 |
+-----+-----------+--------+---------+
5 rows in set (0.00 sec) mysql>
mysql> DELETE Dept,students FROM Dept INNER JOIN students WHERE Dept.id =students.sid;
Query OK, 10 rows affected (0.00 sec) mysql>
mysql> SELECT * FROM Dept;
+----+-----------+
| id | demt_name |
+----+-----------+
| 6 | 法务 |
+----+-----------+
1 row in set (0.00 sec) mysql>
mysql> SELECT * FROM students; Empty set (0.00 sec) mysql>

从一个或多个表中删除满足条件的数据,其中的 table_references代表了多个表的join操作(mysql> DELETE Dept,students FROM Dept INNER JOIN students WHERE Dept.id =students.sid;)

mysql> SELECT * FROM students;
+-----+-----------+--------+---------+
| sid | sname | gender | dept_id |
+-----+-----------+--------+---------+
| 1 | 尹正杰 | boy | 1 |
| 2 | 耿雨星 | boy | 1 |
| 3 | 鲜惠珊 | girl | 2 |
| 4 | 白羽 | boy | 3 |
| 5 | 肖凤 | girl | 4 |
+-----+-----------+--------+---------+
5 rows in set (0.00 sec) mysql>
mysql> SELECT * FROM Dept;
+----+-----------+
| id | demt_name |
+----+-----------+
| 1 | 运维 |
| 2 | 开发 |
| 3 | 测试 |
| 4 | 产品 |
| 5 | 运营 |
| 6 | 法务 |
+----+-----------+
6 rows in set (0.00 sec) mysql>
mysql> DELETE FROM students,Dept USING students INNER JOIN Dept WHERE students.sid = Dept.id;
Query OK, 10 rows affected (0.00 sec) mysql>
mysql> SELECT * FROM students; Empty set (0.00 sec) mysql>
mysql> SELECT * FROM Dept; +----+-----------+
| id | demt_name |
+----+-----------+
| 6 | 法务 |
+----+-----------+
1 row in set (0.00 sec) mysql>

除了上面的那种写法外,还可以用这种方式写也能达到相同的效果,如果有多张表继续使用INNER JOIN关键字继续跟上相应的表即可(mysql> DELETE FROM students,Dept USING students INNER JOIN Dept WHERE students.sid = Dept.id;)

3>.DELETE别名使用方式

mysql> select * from teacher;
+------+-------------+------------+
| t_id | course_name | teacher_id |
+------+-------------+------------+
| 7 | Chinese | 6 |
| 8 | English | 7 |
| 9 | Physic | 8 |
+------+-------------+------------+
3 rows in set (0.00 sec) mysql> select * from student;
+--------+--------------+
| stu_id | stu_name |
+--------+--------------+
| 6 | 火影忍者 |
| 7 | 火影忍者 |
| 9 | 沙和尚 |
+--------+--------------+
3 rows in set (0.00 sec) mysql>
mysql> DELETE t1,t2 FROM teacher AS t1 INNER JOIN student AS t2 WHERE t1.t_id =t2.stu_id;
Query OK, 4 rows affected (0.01 sec) mysql>
mysql> select * from student; +--------+--------------+
| stu_id | stu_name |
+--------+--------------+
| 6 | 火影忍者 |
+--------+--------------+
1 row in set (0.00 sec) mysql>
mysql> select * from teacher; +------+-------------+------------+
| t_id | course_name | teacher_id |
+------+-------------+------------+
| 8 | English | 7 |
+------+-------------+------------+
1 row in set (0.00 sec) mysql>
mysql> select * from teacher;
+------+-------------+------------+
| t_id | course_name | teacher_id |
+------+-------------+------------+
| 7 | Chinese | 6 |
| 8 | English | 7 |
| 9 | Physic | 8 |
+------+-------------+------------+
3 rows in set (0.00 sec) mysql>
mysql> select * from student;
+--------+--------------+
| stu_id | stu_name |
+--------+--------------+
| 6 | 火影忍者 |
| 7 | 火影忍者 |
| 9 | 沙和尚 |
+--------+--------------+
3 rows in set (0.00 sec) mysql>
mysql> DELETE FROM t1,t2 USING teacher AS t1 INNER JOIN student AS t2 WHERE t1.t_id = t2.stu_id;
Query OK, 4 rows affected (0.00 sec) mysql>
mysql> select * from student; +--------+--------------+
| stu_id | stu_name |
+--------+--------------+
| 6 | 火影忍者 |
+--------+--------------+
1 row in set (0.00 sec) mysql>
mysql> select * from teacher; +------+-------------+------------+
| t_id | course_name | teacher_id |
+------+-------------+------------+
| 8 | English | 7 |
+------+-------------+------------+
1 row in set (0.00 sec) mysql>

另一种写法(mysql> DELETE FROM t1,t2 USING teacher AS t1 INNER JOIN student AS t2 WHERE t1.t_id = t2.stu_id;)

三.DROP、TRUNCATE和DELETE的区别

1>.drop

  drop是DDL,会隐式提交,所以,不能回滚,不会触发触发器。

  drop语句删除表结构及所有数据,并将表所占用的空间全部释放。

  drop语句将删除表的结构所依赖的约束,触发器,索引,依赖于该表的存储过程/函数将保留,但是变为invalid状态。

  相关笔记请参考:https://www.cnblogs.com/yinzhengjie/p/10331914.html

2>.truncate

  truncate是DDL,会隐式提交,所以,不能回滚,不会触发触发器。

  truncate会删除表中所有记录,并且将重新设置高水线和所有的索引,缺省情况下将空间释放到minextents个extent,除非使用reuse storage,。不会记录日志,所以执行速度很快,但不能通过rollback撤消操作(如果一不小心把一个表truncate掉,也是可以恢复的,只是不能通过rollback来恢复)。

  对于外键(foreignkey )约束引用的表,不能使用 truncate table,而应使用不带 where 子句的 delete 语句。

  truncate table不能用于参与了索引视图的表。  
mysql> SHOW TABLES;
+-----------------------+
| Tables_in_yinzhengjie |
+-----------------------+
| course |
| score_graph |
| student |
| teacher |
| view_teacher |
+-----------------------+
5 rows in set (0.00 sec) mysql>
mysql> SELECT * FROM student;
+----+-----------+------------+
| id | name | teacher_id |
+----+-----------+------------+
| 1 | 尹正杰 | 1 |
| 2 | 尹正杰 | 1 |
+----+-----------+------------+
2 rows in set (0.00 sec) mysql>
mysql> TRUNCATE TABLE student;
Query OK, 0 rows affected (0.01 sec) mysql>
mysql> SELECT * FROM student;
Empty set (0.00 sec) mysql>
mysql>

TAUNCATE案例展示(mysql> TRUNCATE TABLE student;)

3>.delete

  delete是DML,执行delete操作时,每次从表中删除一行,并且同时将该行的的删除操作记录在redo和undo表空间中以便进行回滚(rollback)和重做操作,但要注意表空间要足够大,需要手动提交(commit)操作才能生效,可以通过rollback撤消操作。

  delete可根据条件删除表中满足条件的数据,如果不指定where子句,那么删除表中所有记录。

  delete语句不影响表所占用的extent,高水线(high watermark)保持原位置不变。

  相关笔记请参考: https://www.cnblogs.com/yinzhengjie/p/10275086.html

SQL语法基础之DELETE语句的更多相关文章

  1. SQL语法基础之ALTER语句

    SQL语法基础之ALTER语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看ALTER的帮助信息 mysql> ? ALTER Many help items fo ...

  2. SQL语法基础之CREATE语句

    SQL语法基础之CREATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.使用“?”来查看MySQL命令的帮助信息 mysql> ? CR ...

  3. SQL语法基础之DROP语句

    SQL语法基础之DROP语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看DROP帮助信息 mysql> ? DROP Many help items for yo ...

  4. SQL语法基础之INSEART语句

    SQL语法基础之INSEART语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.查看INSERT方法的帮助信息 mysql> ? INSERT ...

  5. SQL语法基础之UPDATE语句

    SQL语法基础之UPDATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看UPDATE语句的帮助信息 1>.查看UPDATE的帮助信息 mysql> ? ...

  6. SQL语法基础之SELECT

    SQL语法基础之SELECT 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SELECT查看帮助信息 1>.查看SELECT命令的帮助信息 mysql> ? SEL ...

  7. SQL语法基础之高级应用

    SQL语法基础之高级应用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.存储过程与函数 1>.CREATE PROCEDURE 用来创建存储过程 mysql> ? ...

  8. SQL——语法基础篇(上)

    用数据库的方式思考SQL是如何执行的 虽然 SQL 是声明式语言,我们可以像使用英语一样使用它,不过在 RDBMS(关系型数据库管理系统)中,SQL 的实现方式还是有差别的.今天我们就从数据库的角度来 ...

  9. Sql Server系列:Delete语句

    数据的删除将删除表的部分或全部记录,删除时可以指定删除条件从而删除一条或多条记录.如果不指定删除条件,DELETE语句将删除表中全部的记录,清空数据表. 1 DELETE语法 [ WITH <c ...

随机推荐

  1. ALGO-19 审美课

      算法训练 审美课   时间限制:1.0s   内存限制:256.0MB      问题描述 <审美的历程>课上有n位学生,帅老师展示了m幅画,其中有些是梵高的作品,另外的都出自五岁小朋 ...

  2. HAOI2018 简要题解

    这套题是 dy, wearry 出的.学长好强啊,可惜都 \(wc\) 退役了.. 话说 wearry 真的是一个计数神仙..就没看到他计不出来的题...每次考他模拟赛总有一两道毒瘤计数TAT 上午的 ...

  3. Redhat 用代理连外网

    设置 /etc/yum.conf 添加proxy=http://web-proxy.corp.xx.com:8080 /etc/yum.repos.d/rhel-source.repo 里面改成ena ...

  4. 洛谷 P4705 玩游戏 解题报告

    P4705 玩游戏 题意:给长为\(n\)的\(\{a_i\}\)和长为\(m\)的\(\{b_i\}\),设 \[ f(x)=\sum_{k\ge 0}\sum_{i=1}^n\sum_{j=1}^ ...

  5. iptables(4)规则编写

    /etc/sysconfig/iptables # Generated by iptables-save v1.4.7 on Tue Mar 20 15:05:33 2018*filter:INPUT ...

  6. 小Y的炮

    [存代码] #include<bits/stdc++.h> #define N 1000001 #define MAXN 100001 using namespace std; int n ...

  7. N球M盒

    N球,M盒,由于球是否相同,盒是否相同,盒是否可以为空,共2^3=8种: 1.球同,盒同,盒不可以为空Pm(N)--这符号表示部分数为m的N-分拆的个数,m是P的下标,为了好看我将大写的M弄成小写 2 ...

  8. 洛谷P2261 余数求和

    整除分块的小应用. 考虑到 k % x = k - (k / x) * x 所以把 x = 1...n 加起来就是 k * n - (k / i) * i i = 1...k(注意这里是k) 对于这个 ...

  9. 【洛谷P2585】三色二叉树

    题目大意:给定一个二叉树,可以染红绿黄三种颜色,要求父节点和子节点的颜色不同,且如果一个节点有两个子节点,那么两个子节点之间的颜色也不同.求最多和最少有多少个节点会被染成绿色. 题解:加深了对二叉树的 ...

  10. C#两个实体之间相同属性的映射

    public static R Mapping<R, T>(T model) { R result = Activator.CreateInstance<R>(); forea ...