`

oracle常用经典sql查询

阅读更多

oracle常用经典sql查询

 

1、删除重复记录

delete from wr_recordformtable a

where rowid!=

(select max(rowid)

from wr_recordformtable t

where

t.pid=a.pid and t.tablename=a.tablename

)

2、获得视图执行的时间,精确获得时间

create table  test_view (viewname varchar2(32),d integer,cn integer);

create or replace procedure ppp is

  cursor c1 is

    select object_name

      from user_objects o

     where o.object_type = 'VIEW' and o.status = 'VALID';

  sqlstr varchar2(1000);

  d1     timestamp;

  d2     timestamp;

  d      integer;

  num    integer;

  dstr   varchar2(100);

 

begin

  delete from test_view;

  commit;

  for rr in c1 loop

    begin

      d1     := systimestamp;

      sqlstr := 'select count(*) from ' || rr.object_name;

      execute immediate sqlstr

        into num;

      d2 := systimestamp;

   

      dstr := to_char((d2 - d1), 'hh24:mi:ssx');

      dstr := substr(dstr, 9, length(dstr) - 9);

      d    := to_number(substr(dstr, 1, 2)) * 3600 * 1000 +

              to_number(substr(dstr, 4, 2)) * 60 * 1000 +

              to_number(substr(dstr, 7, 2)) * 1000 +

              to_number(substr(dstr, 10, 3));

      dbms_output.put_line(dstr || '--' || d);

   

      insert into test_view values (rr.object_name, d, num);

      commit;

    exception

      when others then

        dbms_output.put_line(sqlstr || ':' || sqlcode || ':' || sqlerrm);

     

    end;

 

  end loop;

3、返回rownum在4-10之间的数据

 select rownum,month,sell from sale where rownum<10

 minus

  select rownum,month,sell from sale where rownum<5;

 

4、查询当前用户下所有表的记录数

select 'select '''||tname||''',count(*) from '||tname||';' from tab where tabtype='TABLE';

5、快速编译所有视图

----当在把数据库倒入到新的服务器上后(数据库重建),需要将视图重新编译一遍,

----因为该表空间视图到其它表空间的表的连接会出现问题,可以利用PL/SQL的语言特性,快速编译。

 

SQL> SPOOL ON.SQL

SQL> SELECT'ALTER VIEW '||TNAME||' COMPILE;' FROM TAB;

SQL> SPOOL OFF

然后执行ON.SQL即可。

SQL> @ON.SQL

 

6、 如何删除表中的列?

alter table 表1 drop column 列1;

7、查询primary key和forgen key的关系表

select

a.owner 外键拥有者,

a.table_name 外键表,

c.column_name 外键列,

b.owner 主键拥有者,

b.table_name 主键表,

d.column_name 主键列

from

user_constraints a,

user_constraints b,

user_cons_columns c,

user_cons_columns d

where

    a.r_constraint_name=b.constraint_name

and a.constraint_type='R'

and b.constraint_type='P'

and a.r_owner=b.owner

and a.constraint_name=c.constraint_name

and b.constraint_name=d.constraint_name

and a.owner=c.owner

and a.table_name=c.table_name

and b.owner=d.owner

and b.table_name=d.table_name

/     

8、查询重复记录:

法一: 用Group by语句 此查找很快的

select count(num), max(name) from student --查找表中num列重复的,列出重复的记录数,并列出他的name属性

group by num

having count(num) >1 --按num分组后找出表中num列重复,即出现次数大于一次

 

delete from student(上面Select的)

这样的话就把所有重复的都删除了。-----慎重

 

法二:当表比较大(例如10万条以上)时,这个方法的效率之差令人无法忍受,需要另想办法:

---- 执行下面SQL语句后就可以显示所有DRAWING和DSNO相同且重复的记录

SELECT * FROM EM5_PIPE_divFAB

WHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_divFAB D --D相当于First,Second

WHERE EM5_PIPE_divFAB.DRAWING=D.DRAWING AND

EM5_PIPE_divFAB.DSNO=D.DSNO);

 

9、Oracle中如何实现某一字段自动增加1?

 

 

实现方法:

建立一个最小为1,最大为999999999的一个序列号会自动循环的序列

 

create sequence 序列名

increment by 1

start with 1

maxvalue 999999999

cycle;

 

当向表中插入数据时,SQL语句写法如下:

 

SQL> insert into 表名 values(序列名.nextval,列1值,列2值);

 

10、如何查看什么时间有哪些数据库对象结构被修改过?

 SELECT OBJECT_NAME,                        --对象名

      OBJECT_TYPE,                        --对象类型

      TO_CHAR(CREATED,       'YYYY-Mon-DD HH24:MI') CREATE_TIME, --创建时间

      TO_CHAR(LAST_DDL_TIME, 'YYYY-Mon-DD HH24:MI') MOD_TIME,   --修改时间

      TIMESTAMP,                         --时间戳

      STATUS                           --状态

   FROM   USER_OBJECTS

   WHERE  to_char(LAST_DDL_TIME,'yyyymmdd')>'20020101';

 11、获得一个表占的空间


 

 

 
select s.*,bytes/1024/1024 from user_segments s
一般blob占的空间比较大,通过如下表,可以获得LOBSEGMENT是哪个表的那个字段

select * from user_lobs

 


本文地址:http://read.newbooks.com.cn/info/171535.html

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fafa211/archive/2009/02/20/3914663.aspx

分享到:
评论

相关推荐

    oracle常用经典sql查询.rar

    本压缩包“oracle常用经典sql查询.rar”显然是一份关于如何在Oracle环境中高效运用SQL查询的教程资料,适合数据库管理员、开发人员以及对Oracle数据库感兴趣的用户学习。 首先,我们来探讨SQL的基础概念。SQL,全称...

    oracle常用经典sql查询.pdf

    ### Oracle常用经典SQL查询知识点详解 #### 一、查看表空间的名称及大小 通过以下SQL语句可以查询Oracle数据库中各个表空间的名称及其占用的空间大小(单位为MB): ```sql select t.tablespace_name, round(sum...

    oracle常用经典SQL查询

    oracle常用经典SQL查询

    oracle常用经典SQL查询方案

    根据给定的文件信息,以下是对“oracle常用经典SQL查询方案”的详细解析,涉及的知识点主要包括Oracle数据库中的数据字典视图、空间管理、对象管理、版本查询、数据库模式、性能监控等方面。 ### 1. 查询表空间大小...

    oracle常用经典sql查询.doc

    oracle常用经典sql查询

    oracle常用经典sql查询.

    本资料主要聚焦在Oracle中的经典SQL查询,对于初学者来说,掌握这些查询技巧将极大提升数据库操作效率。 一、SELECT语句基础 SELECT语句是SQL中最基本的查询工具,用于从表中检索数据。在Oracle中,你可以通过指定...

    oracle常用经典sql查询功能代码

    从提供的文件内容中,我们可以提取有关Oracle SQL查询功能的重要知识点。 1. 查看数据库空间信息 - 可以通过查询数据库对象来了解表空间的名称、大小、使用情况以及数据文件和物理文件的详细信息。 - Oracle...

Global site tag (gtag.js) - Google Analytics