DELETE statement is used to delete records, the syntax is as follows: (similar to "UPDATE" syntax)
DELETE [IGNORE] FROM 表名 WHERE 条件1, 条件2, ..... ORDER BY ...... LIMIT ......; -- 使用 IGNORE 关键字时,当存在外键约束组织我们删除记录,那么则会忽略删除该条数据 -- 使用 WHERE 子句删除条件范围内的记录;如果不使用 WHERE 子句,则是删除全表范围 -- 使用 ORDER BY 关键字,将被删除的记录进行排序以后,产出符合条件的一些数据 -- 使用 LIMIT 关键字依然是分页的意思,
" DELETE" statement execution order: FROM ---> WHERE ---> ORDER BY ---> LIMIT ---> DELETE
Delete records of employees with more than 20 years of service in 10 departments (this is relatively simple, only involves one table)
DELETE FROM t_emp WHERE deptno = 10 AND DATEDIFF(NOW(), hiredate)/365 >= 20;
Delete the employee records with the highest salary in the 20 departments (in addition to using the WHERE clause to filter out the employees in the 20 departments, you also need to sort them in descending order according to salary and delete the highest records)
DELETE FROM t_emp WHERE deptno = 20 ORDER BY sal+IFNULL(comm,0) DESC LIMIT 1;
The syntax of the "DELETE" statement is still very simple. After having the foundation of the "UPDATE" statement, it will be easier to understand when looking at the "DELETE" statement, because these clauses are not very different. many.
Because the efficiency of correlated subqueries is very low, we can use table joins to improve the deletion efficiency of DELETE statements.
The inner join syntax of "DELETE" SQL statement is as follows:
DELETE 表1, ...... FROM 表1 JOIN 表2 ON 条件 WHERE 条件1, 条件2, ...... ORDER BY ...... LIMIT ...... -- 需要注意的是,这里的 "WHERE"、"ORDER BY"、"LIMIT" 子句都是可选条件
Delete the SALES department and all employee records in this department
Analysis: Delete records from two tables
Analysis: Use a DELETE statement to delete records from two tables. You can use table connection (refer to the syntax above)
Analysis: Belong to The employee records of the SALES department are unknown. You need to first use the conditional query to filter out the employee records of the SALES department
DELETE e, d FROM t_emp e JOIN t_dept d ON e.deptno=d.deptno WHERE d.dname="SALES";
DELETE e FROM t_emp e JOIN (SELECT deptno, AVG(sal) AS avg FROM t_emp GROUP BY deptno) t ON e.deptno=t.deptno AND e.sal < t.avg;
DELETE e FROM t_emp e JOIN (SELECT empno FROM t_emp WHERE ename="KING") t ON e.mgr=t.empno OR e.empno=t.empno;
DELETE 表1, ...... FROM 表1 [LEFT | RIGHT] JOIN 表2 ON 条件 WHERE 条件1, 条件2, ...... ORDER BY ...... LIMIT ...... -- 需要注意的是,这里的 "WHERE"、"ORDER BY"、"LIMIT" 子句都是可选条件 -- 除了 在连接表 的时候选择 "LEFT JOIN" 与 "RIGHT JOIN" 的区别之外,其他的与内连接的语法一样
DELETE e FROM t_emp e LEFT JOIN t_dept d ON e.deptno=d.deptno WHERE d.dname="SALES" OR e.deptno IS NULL
TRUNCATE TABLE 表名; -- 需要注意的是,"TRUNCATE" 语句一次只能清空一张数据表,不能够一次性的清空多张数据表。
Function | |
---|---|
Delete database and table | drop database name; drop table name; |
Delete records in the table | delete from weibo_user where username="xiaomu"; Note: If the where condition is not added to the delete statement, all records in the table will be deleted |
Modify and update statements, the original value will also be overwritten (delete) when updating | Update set is used with where to change certain records Note: If there is no where clause in the update set statement to specify the change Condition, all values of this field in the database will be updated |
Delete field: alter table table name drop field name; | Delete Primary key: alter table table name drop primary key; Update table name: alter table table name rename to new table name; |
The above is the detailed content of How to delete MySQL database data. For more information, please follow other related articles on the PHP Chinese website!