SQL语法基础之DELETE语句
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语句的更多相关文章
- SQL语法基础之ALTER语句
SQL语法基础之ALTER语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看ALTER的帮助信息 mysql> ? ALTER Many help items fo ...
- SQL语法基础之CREATE语句
SQL语法基础之CREATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.使用“?”来查看MySQL命令的帮助信息 mysql> ? CR ...
- SQL语法基础之DROP语句
SQL语法基础之DROP语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看DROP帮助信息 mysql> ? DROP Many help items for yo ...
- SQL语法基础之INSEART语句
SQL语法基础之INSEART语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看帮助信息 1>.查看INSERT方法的帮助信息 mysql> ? INSERT ...
- SQL语法基础之UPDATE语句
SQL语法基础之UPDATE语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查看UPDATE语句的帮助信息 1>.查看UPDATE的帮助信息 mysql> ? ...
- SQL语法基础之SELECT
SQL语法基础之SELECT 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SELECT查看帮助信息 1>.查看SELECT命令的帮助信息 mysql> ? SEL ...
- SQL语法基础之高级应用
SQL语法基础之高级应用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.存储过程与函数 1>.CREATE PROCEDURE 用来创建存储过程 mysql> ? ...
- SQL——语法基础篇(上)
用数据库的方式思考SQL是如何执行的 虽然 SQL 是声明式语言,我们可以像使用英语一样使用它,不过在 RDBMS(关系型数据库管理系统)中,SQL 的实现方式还是有差别的.今天我们就从数据库的角度来 ...
- Sql Server系列:Delete语句
数据的删除将删除表的部分或全部记录,删除时可以指定删除条件从而删除一条或多条记录.如果不指定删除条件,DELETE语句将删除表中全部的记录,清空数据表. 1 DELETE语法 [ WITH <c ...
随机推荐
- [Codeforces261D]Maxim and Increasing Subsequence——树状数组+DP
题目链接: Codeforces261D 题目大意:$k$次询问,每次给出一个长度为$n$的序列$b$及$b$中的最大值$maxb$,构造出序列$a$为$t$个序列$b$连接而成,求$a$的最长上升子 ...
- BZOJ1565[NOI2009]植物大战僵尸——最大权闭合子图+拓扑排序
题目描述 Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其中Plants防守,而Zombies进攻.该款游戏包含多 ...
- Codeforces543 B. Destroying Roads
传送门:>Here< 题意:给出一张无向图(边权为1),并给出两对起点和终点以及距离:s1,t1,l1; s2,t2,l2; 要求删除尽量多的边,使得dis(s1,t1)<=l1, ...
- Ikki's Story IV - Panda's Trick POJ - 3207(水2 - sat 在圈内 还是 在圈外)
题意: 就是一个圈上有n个点,给出m对个点,这m对个点,每一对都有一条边,合理安排这些边在圈内或圈外,能否不相交 解析: 我手残 我手残 我手残 写一下情况 只能是一个在圈外 一个在圈内 即一个1一个 ...
- hdu 6253 (bfs打表)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6253 题意: 马可以往一个方向走两步,然后转个弯走一步,这样算一次动作,求问马n次动作后,能到达多少个点, ...
- POI中不推荐的方法与其替代的方法
不推荐getCellType(),推荐getCellTypeEnum() if(tcell.getCellTypeEnum() == CellType.NUMERIC){ System.out.pri ...
- django.db.utils.DataError: (1406, "Data too long for column 'gender' at row 1")
报错现象 在使用 django 创建 超级用户的时候提示报错 Password (again): ytyt521521 Traceback (most recent call last): File ...
- DNA Evolution CodeForces - 828E(树状数组)
题中有两种操作,第一种把某个位置的字母修改,第二种操作查询与[L, R]内与给出字符串循环起来以后对应位置的字母相同的个数.给出的字符串最大长度是10. 用一个四维树状数组表示 cnt[ATCG的编号 ...
- 20165223 实验三 敏捷开发与XP实践
目录 一.实验报告封面 二.具体实验内容 (一)敏捷开发与XP实践-1 代码规范格式化 (二)敏捷开发与XP实践-2 添加搭档项目 (三)敏捷开发与XP实践-3 搭档代码重构 (四)敏捷开发与XP实践 ...
- poj2431(优先队列+贪心)
题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...