SQL删除指定范围内的记录
对于有经验的人来说,这可能是一个非常简单的问题,但我只是想知道删除 SQL 表中特定范围内的数百条记录的最安全方法。
例如,我需要删除 ID 介于 79 和 79 之间的行。 296:
我担心的是,如果我说删除具有 ID(> 79 且 < 296)的所有内容,那么它可能会真正擦除整个表。
This is probably a very simple question for somebody with experience, but I just wanted to know the safest way to delete a couple of hundred records in an SQL table that fall between a specific range.
For example I need to delete rows with an ID between 79 & 296:
My worry is if I say delete everything with an ID (>79 AND < 296) then it may literally wipe the whole table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您使用 Sql Server
注意:
Between
语句包含在内,因此79
和296
行也将被删除编辑后:您现在澄清了您想要:
ID (>79 AND <296)
所以使用这个:
删除来自表,其中 id > 79并且id< 296
If you use Sql Server
Note : the
between
statement is inclusive, so rows79
and296
will also be deletedAfter your edit : you now clarified that you want :
ID (>79 AND < 296)
So use this :
delete from Table where id > 79 and id < 296
您给出了条件 ID(>79 且 <296),那么答案是:
这与:
如果
id
是整数相同。所有人都回答:
这与:
注意区别。
You gave a condition ID (>79 and < 296) then the answer is:
this is the same as:
if
id
is an integer.All answered:
this is the same as:
Mind the difference.
您也可以将删除更改为 select *
并测试您的选择,
所选记录将与删除的记录相同,
如果您不确定,您也可以将语句包装在开始/回滚中 - 测试语句,然后如果全部都是很好的删除回滚,
例如
SELECT * FROM table
WHERE id BETWEEN 79 AND 296
将显示与 where 匹配的所有记录
如果它们是您“真正”想要删除的内容,则使用
DELETE FROM table
WHERE id BETWEEN 79 AND 296
您还可以创建一个触发器 / 来捕获删除并将它们放入历史表中,
这样如果您错误地删除了某些内容,您总是可以将其恢复
(保持您的历史表记录不超过 6 个月或其他)业务规则说)
you can also just change your delete to a select *
and test your selection
the records selected will be the same as the ones deleted
you can also wrap your statement in a begin / rollback if you are not sure - test the statement then if all is good remove rollback
for example
SELECT * FROM table
WHERE id BETWEEN 79 AND 296
will show all the records matching the where
if they are the wants you 'really' want to delete then use
DELETE FROM table
WHERE id BETWEEN 79 AND 296
You can also create a trigger / which catches deletes and puts them into a history table
so if you delete something by mistake you can always get it back
(keep your history table records no older than say 6 months or whatever business rules say)
如果您在 SQL Server 中按如下方式编写,则不会有擦除数据库表的危险,除非该表中的所有值实际上都在这些值之间:
If you write it as the following in SQL server then there would be no danger of wiping the database table unless all of the values in that table happen to actually be between those values:
这种情况不会发生,因为您将有一个 where 子句。发生的情况是,如果您有类似
delete * from Table1 where id Between 70 and 1296
的语句,sql 查询处理器要做的第一件事就是扫描表并查找其中的记录范围,然后应用删除。That wont happen because you will have a where clause. What happens is that, if you have a statement like
delete * from Table1 where id between 70 and 1296
, the first thing that sql query processor will do is to scan the table and look for those records in that range and then apply a delete.您可以使用这种方式,因为 id 不可能在所有情况下都是连续的。
You can use this way because id can not be sequential in all cases.