Oracle维护必会的基础SQL

1.查询当前数据库谁在运行什么sql
SELECT  OSUSER, USERNAME, SQL_TEXT FROM V$SESSION A, V$SQLTEXT B    WHERE A.SQL_ADDRESS =B.ADDRESS ORDER BY ADDRESS, PIECE;

2.如何查询和删除重复记录?
   DELETE FROM TABLE_NAME  T1
   WHERE ROWID!=(SELECT MAX(ROWID) FROM TABLE_NAME T2
  WHERE T1.COL1= T2.COL1   AND T1.COL2= T2.COL2);
 
3.如何用BBB表的数据去更新AAA表数据 (有关联的字段)
UPDATE AAA SET BNS_SNM=(SELECT BNS_SNM FROM BBB WHERE AAA.DPT_NO=BBB.DPT_NO) WHERE  EXISTS  (SELECT BNS_SNM FROM BBB WHERE AAA.DPT_NO=BBB.DPT_NO) ;
ORA-28000:the account is locked
 
4.关于Rownum
(1)仔细阅读下面三条命令,猜想其结果
Select * from users where rownum>0;
Select * from users where rownum>1;
Select * from users where rownum<2;
 
猜想     根据时间倒序查询10笔订单
select * from  ord_order  b  where rownum<11 order by create_time desc  ;
select * from (select * from ord_order b  order by create_time desc )  where rownum<11 ;
以上2条sql语句得到的数据一样吗?哪个数据是符合要求的。
 
(2)rownum的值来源原理
1 Oracle executes your query.
2 Oracle fetches the first row and calls it row number 1.
3 Have we gotten past row number meets the criteria? If no, then Oracle discards the row, If yes, then Oracle return the row.
4 Oracle fetches the next row and advances the row number (to 2, and then to 3, and then to 4, and so forth).
5 Go to step
 
(3). 总结:因为ROWNUM是对结果集加的一 个伪列,即先查到结果集之后再加上去的一个列 (强调:先要有结果集)。简单的说rownum是对符合条件结果的序列号。它总是从1开始排起的。所以你选出的结果不可能没有1,而有其他大于1的值。