Oracle的操作经验

采用Oracle进行sql语句

建表并设置主键,主键自增,某一字段唯一性约束等

<---如果表存在则删除--->

declare
num number;
begin
select count(1) into num from all_tables where TABLE_NAME = 'standard' and OWNER='SCOTT';
if num=1 then
execute immediate 'drop table EMP';
end if;
end;

<--创建表-->

create table standard(id number(10),
std_um varchar2(50) not null unique,
zhname varchar2(40) not null,
version varchar2(10) not null,
keys varchar2(50) not null,
release_date date,
impl_date date,
ackage_path varchar2(100) not null,
constraint pk_id primary key(id))

<--先删除已有的seq_id 然后创建序列seq_id -->
drop sequence seq_id;
create sequence seq_id start with 1 increment by 1;



<--oracle 的分页查询-->

oracle的分页查询需要借助于数据库内置的ROWNUM字段进行,一般可以采用嵌套进行,比较经典常用的如下:

select t2.* from (select rownum rn ,t1.* from (select rownum r, s.* from standard s order by std_um desc) t1 where rownum<=6) t2 where rn>=2;

这是一个两层嵌套的三次查询,一点关于嵌套查询的思考是:嵌套查询的顺序是,从内向外查,所以我们在书写嵌套查询的时候,可以从内向外写,这样逻辑比较清晰,当内层查询完成后,将查询结果当做次外层查询的新表如t1,此时,条件中的rownum就是t1表中的一个字段,也是内层查询出来的结果,在逻辑上正确的理解应该是rownum<=6,这个rownum是t1字段,以此推理,最外层的哪个嵌套条件就是rn>=2,rn就是次内层查出来的结果集作为了表t2的字段。

同时注意,在第一次使用条件是不能采用>或者>=,必须是<或者<=

原文地址:https://www.cnblogs.com/xiaoyao-001/p/8319826.html