ORACLE database (5) ----- pseudo column

One, the definition of pseudo column

It can be understood as a field that is disguised and not easy to see, and will not show up unless it is specifically queried.
The most obvious difference with general fields: general fields can be found through SELECT *, and can also be found when looking at the table structure of a certain table, while pseudo-columns require specific queries to be able to see them 表结构中也不会有所显示.

Two, ROWNUM

ROWNUM is a continuous (natural number) serial number that starts from 1 and is automatically generated for the query results. It will continue to change with the progress of the query step until the end of the [SELECT] clause.

1 Sequence number generation time

ROWNUM是从FROM开始生成到SELECT停止变化(在第一篇中有写到SQL语句执行顺序)

SELECT T.*,ROWNUM FROM EMP T WHERE T.DEPTNO = 20;
SELECT T.*,ROWNUM FROM EMP T WHERE T.DEPTNO = 20 ORDER BY SAL DESC;

Insert picture description here
Insert picture description here

2 Restrictions on the use of ROWNUM in WHERE

ROWNUM < N — N > 1
ROWNUM <= N — N > 0
ROWNUM <> N – N <> 1
ROWNUM = N – N = 1
ROWNUM > N – N < 1
ROWNUM >= N – N <= 1
ROWNUM BETWEEN N1 AND N2 – N1 <= 1 N2 >= 1 / N1 <= 1<= N2

查询范围内至少包含第1个才能有数据

查询员工工资排在第四名到倒数第四名的员工

SELECT *
  FROM (SELECT A.*,ROWNUM RN FROM (SELECT * FROM EMP T ORDER BY SAL DESC) A) B
 WHERE B.RN BETWEEN 4 AND (SELECT COUNT(1)-3 FROM EMP) ;

3 Precautions

1 伪列前不要加T.

2 不要将ROWNUM用在GROUP BY和HAVING中

Three, ROWID

Similar to the room number, when each piece of data is stored in the database, ORACLE will automatically generate a fixed and unique 18-digit character string for it to record the physical location of the piece of data in the database.

SELECT T.*,ROWID FROM EMP T WHERE DEPTNO = 10;

result

  • ROWID can accurately remove one of the duplicate data
--尝试删掉多余数据仅保留一条   重复数据比较少 太多就不适合用这个删除
SELECT T.*,ROWID FROM CLS74 T;
DELETE FROM CLS74 WHERE ROWID <> 'AAASSbAAEAAAAIsAAD' AND SNO = 4;

1 在数据库中,直接查询目标数据的ROWID,是查询数据最快、最准确的方法,但一般不用。

2 不允许手动修改ROWID

3 在数据库中不重复

Guess you like

Origin blog.csdn.net/c727657851/article/details/115007942