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> 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>

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>

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> 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>
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>

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>
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>
三.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>
3>.delete
delete是DML,執行delete操作時,每次從表中刪除一行,並且同時將該行的的刪除操作記錄在redo和undo表空間中以便進行回滾(rollback)和重做操作,但要注意表空間要足夠大,需要手動提交(commit)操作才能生效,可以通過rollback撤消操作。 delete可根據條件刪除表中滿足條件的數據,如果不指定where子句,那么刪除表中所有記錄。 delete語句不影響表所占用的extent,高水線(high watermark)保持原位置不變。
相關筆記請參考: https://www.cnblogs.com/yinzhengjie/p/10275086.html。