提个oracle数据库删除数据的新手问题

zx870811130 2012-08-21 01:21:15
我要删除UserInfo表的第5行到第10行之间的数据
...全文
196 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fantingftt 2012-09-15
  • 打赏
  • 举报
回复
但是我有点没有看明白,为什么需要加那个主键条件?
DannyHau 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

delete from UserInfo where rownum between 5 and 10;
你试一下,看对不?
[/Quote]

rownum不能那样用,必须嵌套表。
ruihuahan 2012-08-22
  • 打赏
  • 举报
回复
通常表内行的保存顺序是不定的,可能随意插入到有空位的数据这个表的数据块中。
表行的排序需要指定 order by。
linwaterbin 2012-08-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
引用 2 楼 的回复:

delete UserInfo
where exists (select 1 from (select rownum rn,UserInfo.主键列 from UserInfo) t
where t.主键列=UserInfo.主键列 and t.rn between 5 and 10);

感谢楼上两位的回答,都可以实现,我还想请问下这个select 1 fr……
[/Quote]
23:54:02 hr@ORCL (^ω^) select 1 from t1;

1
----------
1
1
1
1
1
1
1
1
1
1
1
1
1
1

已选择14行。

已用时间: 00: 00: 00.04
23:54:07 hr@ORCL (^ω^) select * from t1;

EMPNO ENAME
---------- ----------------------------------------
7369 SMITH
7499 ALLEN
7654 MARTIN
7698 BLAKE
7782 CLARK
7839 KING
7876 ADAMS
7934 MILLER
7521 WARD
7566 JONES
7788 SCOTT
7844 TURNER
7900 JAMES
7902 FORD

已选择14行。

已用时间: 00: 00: 00.05

这里的1只是个整数,全表扫描的代号而已。
23:54:16 hr@ORCL (^ω^) select 2 from t1;

2
----------
2
2
2
2
2
2
2
2
2
2
2
2
2
2

已选择14行。

已用时间: 00: 00: 00.01
linwaterbin 2012-08-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
和select * 一样的,没什么用意,就是写起来方便,我的个人习惯,此处用来判断记录存在的
[/Quote]
这个和select完全不一样!!!!
select *会去数据字典找metadata 但是select 1 却不会
二者,起码在执行计划上就有区别了。
下面用一个15万左右的表简单测试一下:

23:42:53 hr@ORCL (^ω^) select 1 from test;

已选择151359行。

已用时间: 00: 00: 02.35
执行计划
----------------------------------------------------------
Plan hash value: 1357081020

------------------------------------------------------------------
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 151K| 473 (2)| 00:00:06 |
| 1 | TABLE ACCESS FULL| TEST | 151K| 473 (2)| 00:00:06 |
------------------------------------------------------------------


统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
12046 consistent gets
0 physical reads
0 redo size
2038657 bytes sent via SQL*Net to client
111375 bytes received via SQL*Net from client
10092 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
151359 rows processed


23:45:00 hr@ORCL (^ω^) select * from test;

已选择151359行。

已用时间: 00: 00: 10.52
执行计划
----------------------------------------------------------
Plan hash value: 1357081020

--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 151K| 13M| 479 (3)| 00:00:06 |
| 1 | TABLE ACCESS FULL| TEST | 151K| 13M| 479 (3)| 00:00:06 |
--------------------------------------------------------------------------


统计信息
----------------------------------------------------------
0 recursive calls
0 db block gets
12046 consistent gets
0 physical reads
0 redo size
16255363 bytes sent via SQL*Net to client
111375 bytes received via SQL*Net from client
10092 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
151359 rows processed

很明显:
1)从执行时间上:select 1 是2秒;select *是10秒
2)从cpu代价上:select 1 是473*2;而select *是479*2
3)从i/o开销上:select 1和select *上是一样的
当然了,这个测试只是小范围测试,数据量和测试数据不够多,但也可大体证明了我在上面提到的:select *会去查找数据字典,而select 1不会。
iqlife 2012-08-21
  • 打赏
  • 举报
回复
我要删除UserInfo表的第5行到第10行之间的数据

还有这种删除数据的需求啊
雅冰石 2012-08-21
  • 打赏
  • 举报
回复
delete from UserInfo where rownum between 5 and 10;
你试一下,看对不?
window_xp 2012-08-21
  • 打赏
  • 举报
回复
这个是正解啊
[Quote=引用 1 楼 的回复:]

SQL code

--默认排序的删除
delete UserInfo
where exists (select 1 from (select rownum rn,UserInfo.主键列 from UserInfo) t
where t.主键列=UserInfo.主键列 and t.rn between 5 and 10);



SQL code

--指定排……
[/Quote]
人生无悔 2012-08-21
  • 打赏
  • 举报
回复
和select * 一样的,没什么用意,就是写起来方便,我的个人习惯,此处用来判断记录存在的
zx870811130 2012-08-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

delete UserInfo
where exists (select 1 from (select rownum rn,UserInfo.主键列 from UserInfo) t
where t.主键列=UserInfo.主键列 and t.rn between 5 and 10);
[/Quote]
感谢楼上两位的回答,都可以实现,我还想请问下这个select 1 from XXX的用意是什么?
ORAClE SE 2012-08-21
  • 打赏
  • 举报
回复
delete UserInfo
where exists (select 1 from (select rownum rn,UserInfo.主键列 from UserInfo) t
where t.主键列=UserInfo.主键列 and t.rn between 5 and 10);



人生无悔 2012-08-21
  • 打赏
  • 举报
回复

--默认排序的删除
delete UserInfo
where exists (select 1 from (select rownum rn,UserInfo.主键列 from UserInfo) t
where t.主键列=UserInfo.主键列 and t.rn between 5 and 10);



--指定排序的删除
delete UserInfo
where exists (select 1 from (select row_number() over(order by 排序列) rn,UserInfo.主键列 from UserInfo) t
where t.主键列=UserInfo.主键列 and t.rn between 5 and 10);


只有记事本用,没法测,不知是否有错,自已测下,应没错,呵呵

17,378

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧