博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL入门-DML数据操作语言--delete
阅读量:6649 次
发布时间:2019-06-25

本文共 4149 字,大约阅读时间需要 13 分钟。

1.help delete

mysql> help delete;Name: 'DELETE'Description:Syntax:DELETE is a DML statement that removes rows from a table.Single-Table SyntaxDELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name    [PARTITION (partition_name,...)]    [WHERE where_condition]    [ORDER BY ...]    [LIMIT row_count]The DELETE statement deletes rows from tbl_name and returns the numberof deleted rows. To check the number of deleted rows, call theROW_COUNT() function described inhttp://dev.mysql.com/doc/refman/5.6/en/information-functions.html.Main ClausesThe conditions in the optional WHERE clause identify which rows todelete. With no WHERE clause, all rows are deleted.where_condition is an expression that evaluates to true for each row tobe deleted. It is specified as described inhttp://dev.mysql.com/doc/refman/5.6/en/select.html.If the ORDER BY clause is specified, the rows are deleted in the orderthat is specified. The LIMIT clause places a limit on the number ofrows that can be deleted. These clauses apply to single-table deletes,but not multi-table deletes.Multiple-Table SyntaxDELETE [LOW_PRIORITY] [QUICK] [IGNORE]    tbl_name[.*] [, tbl_name[.*]] ...    FROM table_references    [WHERE where_condition]Or:DELETE [LOW_PRIORITY] [QUICK] [IGNORE]    FROM tbl_name[.*] [, tbl_name[.*]] ...    USING table_references    [WHERE where_condition]PrivilegesYou need the DELETE privilege on a table to delete rows from it. Youneed only the SELECT privilege for any columns that are only read, suchas those named in the WHERE clause.PerformanceWhen you do not need to know the number of deleted rows, the TRUNCATETABLE statement is a faster way to empty a table than a DELETEstatement with no WHERE clause. Unlike DELETE, TRUNCATE TABLE cannot beused within a transaction or if you have a lock on the table. See [HELPTRUNCATE TABLE] and [HELP LOCK].The speed of delete operations may also be affected by factorsdiscussed in http://dev.mysql.com/doc/refman/5.6/en/delete-speed.html.To ensure that a given DELETE statement does not take too much time,the MySQL-specific LIMIT row_count clause for DELETE specifies themaximum number of rows to be deleted. If the number of rows to deleteis larger than the limit, repeat the DELETE statement until the numberof affected rows is less than the LIMIT value.SubqueriesYou cannot delete from a table and select from the same table in asubquery.Partitioned TablesBeginning with MySQL 5.6.2, DELETE supports explicit partitionselection using the PARTITION option, which takes a comma-separatedlist of the names of one or more partitions or subpartitions (or both)from which to select rows to be dropped. Partitions not included in thelist are ignored. Given a partitioned table t with a partition namedp0, executing the statement DELETE FROM t PARTITION (p0) has the sameeffect 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 thecondition is tested only on rows in the listed partitions. For example,DELETE FROM t PARTITION (p0) WHERE c < 5 deletes rows only frompartition p0 for which the condition c < 5 is true; rows in any otherpartitions are not checked and thus not affected by the DELETE.The PARTITION option can also be used in multiple-table DELETEstatements. You can use up to one such option per table named in theFROM option.See http://dev.mysql.com/doc/refman/5.6/en/partitioning-selection.html,for more information and examples.URL: http://dev.mysql.com/doc/refman/5.6/en/delete.html
View Code

2.使用delete语句

delete from anyuxweb.t2 where id=2; select * from anyuxweb.t2;

 3.delete 语句后面要跟着where子句,否则这个表很危险

--删除数据不使用where子句,会将表清空delete from anyuxweb.t2;--查看当前表select * from anyuxweb.t2;

4. 伪删除

数据表实现伪删除的原理是将数据添加一个状态列(如列名status)。

--添加状态列alter table anyuxweb.t2 add status int;--更新status状态值update anyuxweb.t2 set status=1;select * from anyuxweb.t2 where status =1;insert into anyuxweb.t2(id,name) select * from anyuxweb.t1;select * from anyuxweb.t2; update anyuxweb.t2 set status=1; select * from anyuxweb.t2;
此时使用status状态查询过滤即可

 

转载于:https://www.cnblogs.com/anyux/p/8127065.html

你可能感兴趣的文章
keepalived
查看>>
《Linux菜鸟入门2》系统定时延时任务
查看>>
局域网共享故障的分析与排除
查看>>
使用 sitemesh/decorator装饰器装饰jsp页面(原理及详细配置)
查看>>
听说又被 JVM 内存区域方面的面试题给虐了?看看这篇文章吧!
查看>>
运维常用的系统命令
查看>>
C++--标准库 字符串类
查看>>
Java的新项目学成在线笔记-day1(七)
查看>>
160个CRACK_ME系列 第一弹
查看>>
什么是记录型数字压力表?被应用在哪些领域?
查看>>
零基础学习Python的4个方法
查看>>
Clouds
查看>>
好程序员带你认识HTML5中的WebSocket
查看>>
PDA是什么功能有哪些
查看>>
一文了解 SaCa DataViz 企业版和标准版的区别
查看>>
CentOS 5的KVM安装使用说明
查看>>
php warning: php startup: in unknown on line 0
查看>>
【CentOS 7.1】配置防火墙 iptables
查看>>
二十七、单张图片上传预览
查看>>
一例千万级pv高性能高并发网站架构
查看>>