–rowid 和 rownum 都是僞列
–rowid了解為記錄在插入到資料庫的表中時候就存在的資料的位址(對象的位址),其實不是位址,根據位址得到的值
–如果一個表中沒有主鍵,沒有不可重複的字段,可能會出現多條一模一樣的資料,無法區分重複資料,可以根據rowid進行區分
select deptno,dname,loc,rowid from dept;
select empno,ename,rowid from emp;
insert into tb_student values(1,'張三','國文',81);
insert into tb_student values(2,'張三','數學',75);
insert into tb_student values(3,'李四','國文',81);
insert into tb_student values(4,'李四','數學',90);
insert into tb_student values(5,'王五','國文',81);
insert into tb_student values(6,'王五','數學',100);
insert into tb_student values(7,'王五','英語',90);
select * from tb_student;
–當一個表中有多條一模一樣的資料的時候,實作去重,重複資料隻保留一條
–查到要保留的資料
select id,name,course,score,min(rowid) from tb_student group by id,name,course,score;
select * from tb_student where rowid in(select min(rowid) from tb_student group by id,name,course,score);
–查到要删除的資料
select *
from tb_student
where not
rowid in
(select min(rowid) from tb_student group by id, name, course, score);
–删除這些資料
delete from tb_student
where not
rowid in
(select min(rowid) from tb_student group by id, name, course, score);
–rownum 結果集的序号 有一個結果集就有一個rownum select查到的結果集
–rownum 從1開始,每次+1
select deptno,dname,loc,rowid,rownum from dept;
select empno,ename,rownum from emp where rownum<=4;
–分頁:在oracle中使用rownum.因為rownum規律可循,控制rownum序号從1開始,每次+1,友善判斷
–查詢
–一般如果根據主鍵字段進行排序,先排序後确定rownum
–一般如果根據非主鍵字段進行排序,先确定rownum再排序
–保證一定先排序後确定rownum,在結果集的外面嵌套一層select,這個select的rownum肯定就是從1開始,根據這個有規律的,已确定的row進行判斷操作就可以
select deptno,dname,rownum num from dept order by loc;
select rownum,deptno,dname,num from (select deptno,dname,rownum num from dept order by loc);
select empno,ename,sal,comm,rownum from (select empno,ename,sal,comm,rownum num from emp order by sal desc);
–分頁需求:
–i頁數 num每頁顯示幾個
–num= 3 i=4
–每一頁要顯示的資料的rownum 第一個: rownum>=num*(i-1)+1 最後一個為: row<=num*i
select *
from (select empno, ename, sal, comm, rownum num2
from (select empno, ename, sal, comm, rownum num
from emp
order by sal desc))
where num2 >= 3 * (1 - 1) + 1
and num2 <= 3 * 1;
–select 資料 from 資料來源 where 行過濾條件 group by 分組字段 having 組過濾資訊 order by 排序
–執行流程: from where group by having select order by
–decode() |case判定函數 nvl()
–單行函數
–組函數
–子查詢
–in 和 exists
–rowid 和 rownum
–集合函數
–like