Pseudo-column ROWNUM, ROWID part usage

ROWNUM is a logical value and cannot participate in the calculation

ROWID is a physical value that can participate in calculations


In the use of ROWNUM and ROWID, the search results are now formed into a result set, and ROWID and ROWNUM are aliased in the result set, and this alias is used in the outer layer to find the row you are looking for.


ROWNUM usage

ROWNUM must be aliased and cannot be used directly

Find the fifth to tenth rows of the emp table

SELECT EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO FROM(SELECT ROWNUM RN,EMP.* FROM EMP)WHERE RN>=5 AND RN<=10;

ROWID pseudocolumn

SELECT ROWID, E.* FROM EMP E; view pseudo-columns

As for the role of ROWID, since ROWID is used to uniquely identify the uniqueness of the data in the table, this effect can be used to remove duplicates

CREATE TABLE DEPT_8 AS SELECT * FROM DEPT WHERE 1=2;

INSERT INTO DEPT_8 SELECT * FROM DEPT;

INSERT INTO DEPT_8 SELECT * FROM DEPT;

COMMIT;

delete DEPT_8 where rowid not in (select max(rowid) from DEPT_8 group by DEPTNO,DNAME,LOC);

SELECT ROWID,DEPT_8.* FROM DEPT_8;

Use ROWNUM to delete the 3rd row in the table

DELETE EMP2 WHERE EMPNO=

(

SELECT EMPNO FROM (SELECT ROWNUM RN,E.* FROM EMP2 E)WHERE RN=3

);

Use ROWNUM to delete rows 5 to 7 in the table

DELETE EMP4 WHERE EMPNO IN

(

SELECT EMPNO FROM(SELECT ROWNUM RN,E.* FROM EMP4 E)WHERE ROWNUM BETWEEN 5 AND 7

);

Use ROWNUM to delete the fifth-to-last row in the table

DELETE EMP8 WHERE EMPNO=

(

SELECT EMPNO FROM(SELECT ROWNUM RN,EMP8.* FROM EMP8)WHERE RN=((SELECT COUNT(*)-4 FROM EMP8))

);

There are many duplicate rows, how to use ROWNUM and ROWID to delete the fifth-to-last row

DELETE EMP10 WHERE ROWID=

(

SELECT RID FROM(SELECT ROWID RID,ROWNUM RN,E.* FROM EMP10 E)WHERE RN=(SELECT COUNT(*)-4 FROM EMP10)

);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324966872&siteId=291194637