码迷,mamicode.com
首页 > 数据库 > 详细

oracle sql语句

时间:2016-08-10 22:55:54      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:oracle   sql语句

 oracle sql语句

#########
#基本操作#
#########
启动数据库:
su - oracle
sqlplus / as sysdba
SQL> startup

对scott用户解锁:
SQL> conn / as sysdba
SQL> alter user scott identified by tiger account unlock;

连接到指定的数据库用户:
SQL> conn scott/tiger

实现操作系统开机数据库自动open:
vi /etc/oratab
------------------------------------------
orcl:/u01/app/oracle/product/11.2.0/db_1:Y
db01:/u01/app/oracle/product/11.2.0/db_1:N
crm:/u01/app/oracle/product/11.2.0/db_1:Y
------------------------------------------

vi /etc/rc.local
------------------------------------------
su - oracle ‘-c dbstart $ORACEL_HOME‘
------------------------------------------
###############
#基本select语句#
###############
查看scott用户下有哪些表和视图:
SQL> select * from tab;

描述一张表的结构,不看表中的数据
SQL> desc dept

查看雇员表中的所有数据:
select * from dept;

描述雇员表的结构
SQL> desc emp

查看emp表中感兴趣的列:
SQL> select ename,sal from emp;

在select中使用四则运算:null不能参与四则运算
select ename,(sal+100)*12 from emp;

为列定义别名
select ename AS first_name,sal*12 "Annual Salary" from emp;

连接操作符
select ename,job,ename||‘ is a ‘||job detail from emp;

压缩重复行
select distinct deptno,job from emp;

将缓冲区中的命令保存为脚本
SQL> save p1_1.sql

查看sql脚本内容
SQL> get p1_1.sql

运行sql脚本
SQL> @p1_1.sql

##############
#限制和排列数据#
##############
工资高于1500的销售员?
查询10部门的雇员和20部门工资小与2000的雇员?
查询有奖金的雇员?

使用rownum伪列限制查询返回的行的数量:
SQL> select * from emp where rownum<6;
SQL> select * from emp where sal between 2000 and 3000;

select
to_char(sysdate,‘yyyy‘) curr_year,
to_char(to_date(‘05‘,‘yy‘),‘yyyy‘) yy05,
to_char(to_date(‘99‘,‘yy‘),‘yyyy‘) yy99,
to_char(to_date(‘05‘,‘rr‘),‘yyyy‘) rr05,
to_char(to_date(‘99‘,‘rr‘),‘yyyy‘) rr99
from dual;

select ename,
       job,
       sal,
case job when ‘CLERK‘ then sal*1.05
         when ‘ANALYST‘ then sal*1.1
else sal end raise_sal
from emp
order by job;

select ename,
       job,
       sal,
decode(job,‘CLERK‘,sal*1.05,‘ANALYST‘,sal*1.1,sal) raise_sal
from emp order by job;

#########
#多表连接#
#########
笛卡尔连结:
select ename,loc from emp cross join dept;
自然连接:等值连接
select ename,loc from emp natural join dept;
两张表有多个同名的列:
select ename,loc from emp join dept using (deptno);
两张没有同名的列:
select ename,loc from emp e join dept d on (e.deptno=d.deptno);
右外连接
select ename,loc from emp right outer join dept using (deptno);
左外连接
select ename,loc from emp left outer join dept using (deptno);
全外连接
select ename,loc from emp full outer join dept using (deptno);

10部门的最大工资
查找重复的工资
80年 81年 82年 87年都有多少新员工

1980  1981  1982  1987
----  ----  ----  ----
   1    10     1     2

工资高于BLAKE的?
工资最低的人?
低于10部门最低工资的人?
高于30部门最高工资的人?
工资相同的人?
blake的工资是smith的几倍?
每个部门工资最高的人?
每个部门工资最高的前2个人?
工资最高的前5行?
工资6~10名?
随机从表中取出3行数据?

取每一个部门工资最高的前2个人
方法1:
select deptno,ename, sal
from emp
where sal in (select max(sal) from emp group by deptno) or
sal in (select max(sal)
       from (select sal,deptno
             from emp where sal not in
                      (select max(sal) from emp group by deptno)) group by deptno)
order by 1;

方法2:
select ename,deptno,sal from emp e where (select count(*) from emp where sal>e.sal and deptno=e.deptno)<2;

select count(*) from emp where sal>800 and deptno=20;

方法3:
select * from (select ename,deptno,sal,rank () over (partition by deptno order by sal desc) Ord from emp) where ord<=2;

select ename,deptno,sal,row_number () over (partition by deptno order by sal desc) Ord from emp;

select * from (select rownum rn,a.* from (select ename,sal from emp order by sal desc) a) where rn between 6 and 10;

select * from (select * from emp order by dbms_random.value()) where rownum<=3;

查询雇员的姓名,工资,税,(1级不缴税,2-->2% ,3-->3%,4-->4%,5-->5%)
select
  e.ename,
  e.sal,
  (sal*decode(s.grade,1,0,2,0.02,3,0.03,4,0.04,5,0.05,0)) tax
from emp e,salgrade s
where e.sal between s.losal and s.hisal;

部门总工资和部门上缴个税总和
select deptno,sum(sal),sum(tax)
from
(select
  e.sal,
  (sal*decode(s.grade,1,0,2,0.02,3,0.03,4,0.04,5,0.05,0)) tax,
  deptno
from emp e,salgrade s
where e.sal between s.losal and s.hisal)
group by deptno;

比WARD奖金低的人?
select ename,comm from emp where NVL(comm,0)<(select comm from emp where ename=‘WARD‘);

select ename,comm from emp where comm<(select comm from emp where ename=‘WARD‘) or comm is null;

奖金最高的前两名雇员?
select * from (select ename,comm from emp order by comm desc nulls last) where rownum<=2;

select * from (select ename,comm from emp where comm is not null order by comm desc) where rownum<=2;

工资高于本部门平均工资的人?

使用替代变量进行分页查询
select * from (select rownum rn,a.* from (select * from emp order by sal desc) a)
where rn between &p*5-4 and &p*5;


set PAUSE on
show pagesize
host pwd
spool 0728am.txt append
spool off

insert into (select empno,ename,hiredate from emp where hiredate<=sysdate with check option)
values (2,‘jerry‘,to_date(‘20160729‘,‘yyyymmdd‘));

ins10.sql
--------------------------------
insert into (select * from emp where deptno=10 with check option)
values (&empno,‘&ename‘,‘&job‘,&mgr,‘&hiredate‘,&sal,&comm,&deptno);
--------------------------------

数据源:emp
目标表:copy_emp
create table copy_emp as select * from emp where 1=0;

matched--> 目标表中的主键值在数据源中被找到
not matched --> 数据源中主键在目标表中不存在

merge into copy_emp c
using emp e
on (c.empno=e.empno)
when matched then
update set
c.ename=e.ename,
c.job=e.job,
c.mgr=e.mgr,
c.hiredate=e.hiredate,
c.sal=e.sal,
c.comm=e.comm,
c.deptno=e.deptno
when not matched then
insert values
(e.empno,
e.ename,
e.job,
e.mgr,
e.hiredate,
e.sal,
e.comm,
e.deptno);

commit;

select ename,sal from emp where sal>(select sal from emp where ename=‘BLAKE‘);

1. scott的工资时blake的几倍?
select (select sal from emp where ename=‘SCOTT‘)/(select sal from emp where ename=‘BLAKE‘) from dual;

select a.sal/b.sal from emp a,emp b where a.ename=‘SCOTT‘ and b.ename=‘BLAKE‘;

2. 工资最高的人是谁?

3. 工资相同的人都有谁?

4. 30部门工资最高的人?

5. 每个部门工资最高的人?

6. 每个部门工资最高的前两个人?
select ename,sal,deptno
from emp e
where (select count(*) from emp where deptno=e.deptno and sal>e.sal)<2
order by 3;

7. 大于10部门最小工资的人?

8. 工资高于本部门平均工资的人?
select e.ename,e.sal,e.deptno,a.avgsal
from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) a
where e.deptno=a.deptno
and e.sal>a.avgsal;

9. 工资前5的员工?

10.工资6~10的员工?
select * from (select rownum rn,a.* from (select ename,sal from emp order by sal desc) a) where rn between 6 and 10;

11. 分页查询:每页5行

grant alter session to scott;
alter session set events ‘immediate trace name savepoints level 1‘;
SQL> show parameter background
background_dump_dest = /u01/app/oracle/diag/rdbms/mydb/mydb/trace

select COLUMN_NAME,DATA_DEFAULT from user_tab_columns where table_name=‘E01‘

create table t03 (x number,y date default sysdate);
select column_name,data_default from user_tab_columns where table_name=‘T03‘;
user_tables
user_catalog
user_objects
-----------------------------------------------------------------------------------------
键表时直接启用约束,列级别启用约束,约束产用系统命名
create table e01 (id number primary key,
                  name varchar2(12) not null,
                  email varchar2(30) unique);

键表时直接启用约束,列级别启用约束,约束产用户命名
create table e01 (id number constraint pk_e01_id primary key,
                  name varchar2(12) constraint nn_e01_name not null,
                  email varchar2(30) constraint uk_e01_mail unique);

添加列的同时带有约束
create table e03 (id number constraint fk_e03_id references e01);

外键的级联操作:
alter table e03 drop constraint fk_e03_id;
alter table e03 add constraint fk_e03_id foreign key (id) references e01 on delete set null;
alter table e03 add constraint fk_e03_id foreign key (id) references e01 on delete cascade;

create table t04 (x number not null,y date default sysdate);

select CONSTRAINT_NAME,CONSTRAINT_TYPE,SEARCH_CONDITION
from user_constraints where table_name=‘T04‘;

create table t04 (x number constraint nn_t04_x not null,y date default sysdate);

create table t04 (x number constraint nn_t04_x not null,y date default sysdate constraint uk_t04_y unique);

create table t04 (x number constraint pk_t04_x primary key,y date default sysdate constraint uk_t04_y unique);

create table t05 (x number constraint fk_t05_x references t04,y number constraint ck_t04_y check(y>100));

add constraint:
create table d as select * from dept;
create table e as select * from emp;

alter table d add constraint pk_d_id primary key (deptno);

alter table e modify (empno number(4) constraint nn_e_id not null);
alter table e add constraint uk_e_name unique (ename);
alter table e add constraint fk_e_deptno foreign key (deptno) references d;
alter table e add constraint ck_e_sal check (sal>=1000 and sal is not null);

@/u01/app/oracle/product/11.2.0/db_1/rdbms/admin/utlexcpt.sql
alter table e add constraint ck_e_sal check (sal>=1000 and sal is not null) exceptions into exceptions;

drop constraint:
alter table e drop constraint XXXXXXXXXX;
alter table d drop constraint PK_D_ID cascade;

查看约束状态:
select CONSTRAINT_NAME,STATUS,VALIDATED from user_constraints where table_name=‘E01‘;

alter table e modify constraint CK_E_SAL disable;
alter table e modify constraint CK_E_SAL enable;

ENABLED  VALIDATED
ENABLED  NOT VALIDATED
DISABLED NOT VALIDATED
DISABLED VALIDATED

alter table e modify constraint CK_E_SAL disable validate;
-----------------------------------------------------------
alter table e modify constraint UK_E_NAME disable;
insert into e (empno,ename) values (2,‘SCOTT‘);
alter table e modify constraint CK_E_SAL enable novalidate;
create index i_e_name on e (ename);
-----------------------------------------------------------

select distinct privilege from dba_sys_privs;

--with admin option 权限回收无级联
--with grant option 权限回收有级联

SQL> select * from session_privs;

PRIVILEGE
----------------------------------------
CREATE SESSION
UNLIMITED TABLESPACE
CREATE TABLE
CREATE CLUSTER
CREATE SEQUENCE
CREATE PROCEDURE
CREATE TRIGGER
CREATE TYPE
CREATE OPERATOR
CREATE INDEXTYPE

conn / as sysdba
grant create view to scott;

create or replace view vu30 as select empno,ename,sal,deptno from emp where deptno=30;

create or replace view vu30 as select empno employee_id,ename first_name,sal salary,deptno department_id from emp where deptno=30;

create or replace view vu30 (employee_id,first_name,salary,department_id) as select empno,ename ,sal ,deptno  from emp where deptno=30;

create or replace force view vu30 as select empno,ename,sal,deptno from e01 where deptno=30;

create or replace force view vu30 as select empno,ename,sal,deptno from e01 where deptno=30 with check option;

create or replace force view vu30
as select rownum rn,empno,ename,sal,deptno
from e01 where deptno=30 with check option;
-----------------------------------------------------------------------------
create sequence seq_empno
start with 7935
increment by 1
minvalue 7935
maxvalue 9999
cache 50
nocycle;

select seq_empno.currval,seq_empno.nextval from dual;

alter sequence seq_empno increment by 5;
alter sequence seq_empno minvalue 7936;
alter sequence seq_empno maxvalue 8888;
alter sequence seq_empno cache 100;
alter sequence seq_empno cycle;

insert into emp (empno) values (seq_empno.nextval);
drop sequence seq_empno;
-------------------------------------------------------
7839:AAAVREAAEAAAACXAAI

AAAVRE      AAE     AAAACX     AAI
OOOOOO      FFF     BBBBBB     RRR

OOOOOO --> object_id
FFF --> file_id
BBBBBB --> block_id
RRR --> row number

A  -   Z    a    -   z   0   -   9   +   /
0  -   25   26   -   51  52  -   61  62  63
VRE = 21*64*64+17*64+4 = 87108
2*64+23 = 151

create index i_emp_name on emp (ename);
set autot trace exp
select * from emp where ename=‘SCOTT‘;

create index i_emp_name_f on emp (upper(ename));
select * from emp where upper(ename)=‘SCOTT‘;

drop index I_EMP_NAME_F;

create synonym e01 for scott.e01;
create public synonym e01 for scott.e01;
---------------------------------------------------------------------------
create user smith identified by smith;

SQL> select distinct privilege from dba_sys_privs;

SQL> grant create session to smith;
SQL> conn smith/smith
SQL> select * from session_privs;

create table t01 (x int);

SQL> grant select on scott.e01 to smith;
SQL> grant update (comm) on scott.e01 to smith;
SQL> grant delete on scott.e01 to smith;
SQL> grant insert on scott.e01 to smith;

select table_name from dict where table_name like ‘%ROLE%‘;

create role r1;
grant create session,create table to r1;

create role r2;
grant create view to r2;
grant delete on scott.e01 to r2;

create role r3;
grant create procedure to r3;
grant update (sal) on scott.e01 to r3;

grant r3 to r1;

create user tom identified by tom;
grant r1,r2 to tom;

grant create sequence to tom;
grant select on scott.e01 to tom;
grant insert on scott.e01 to tom;
grant update (comm) on scott.e01 to tom;
-------------------------------------------------
查看用户被授予的系统权限:
SQL> select privilege from dba_sys_privs where GRANTEE=‘TOM‘;

查看用户被授予的对象权限:
col GRANTEE for a15
col PRIVILEGE for a20
col owner for a15
SQL> SELECT GRANTEE,PRIVILEGE,OWNER,TABLE_NAME FROM DBA_TAB_PRIVS WHERE GRANTEE=‘TOM‘;

查看用户被授予的列级别的对象权限:
SQL> SELECT OWNER,TABLE_NAME,COLUMN_NAME,PRIVILEGE FROM DBA_COL_PRIVS where GRANTEE=‘TOM‘;

用户被授予的角色:
SELECT * FROM DBA_ROLE_PRIVS WHERE GRANTEE=‘TOM‘;

角色被授予的角色: R1,R2,R3
SELECT * FROM ROLE_ROLE_PRIVS WHERE ROLE=‘R1‘;
SELECT * FROM ROLE_ROLE_PRIVS WHERE ROLE=‘R2‘;

角色被授予的系统权限:
select * from ROLE_SYS_PRIVS WHERE ROLE=‘R1‘;
select * from ROLE_SYS_PRIVS WHERE ROLE=‘R2‘;
select * from ROLE_SYS_PRIVS WHERE ROLE=‘R3‘;

角色被授予的对象权限:
select * from ROLE_TAB_PRIVS WHERE ROLE=‘R1‘;
select * from ROLE_TAB_PRIVS WHERE ROLE=‘R2‘;
select * from ROLE_TAB_PRIVS WHERE ROLE=‘R3‘;

select * from v$timezone_names;

修改用户口令:
alter user tom identified by oracle;

SQL> password
Changing password for TOM
Old password: ******
New password: ***
Retype new password: ***
Password changed

SQL> revoke r2 from tom;

级联授权:
dba --> user A --> user B
系统权限级联授权:with admin option 权限回收无级联
grant CREATE SEQUENCE to tom with admin option;

对象权限级联授权:with grant option 权限回收有级联
grant insert on scott.e01 to tom with grant option;
---------------------------------------------------------
union会压缩重复值:
select * from e01
union
select * from emp;

union all没有去重效果
select * from e01
union all
select * from emp;

select * from e01
intersect
select * from emp;

select * from e01
minus
select * from emp;

select * from emp
minus
select * from e01;

select * from e01
union all
select dept.*,null,null,null,null,null from dept;
---------------------------------------------------
时间戳
SQL> create table t01 (x int,y timestamp);
SQL> insert into t01 values (1,current_timestamp);
SQL> alter table t01 modify (y timestamp(9));
全球化时间戳:
SQL> create table t02 (x int,y timestamp with time zone);
本地时间戳:
SQL> create table t03 (x int,y timestamp with local time zone);

SQL> select sessiontimezone from dual;
SESSIONTIMEZONE
----------------
+08:00

SQL> select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘),to_char(current_date,‘yyyy-mm-dd hh24:mi:ss‘) from dual;

TO_CHAR(SYSDATE,‘YY TO_CHAR(CURRENT_DAT
------------------- -------------------
2015-12-22 13:41:00 2015-12-22 13:41:00

SQL> alter session set time_zone=‘-8:00‘;

SQL> select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘),to_char(current_date,‘yyyy-mm-dd hh24:mi:ss‘) from dual;

TO_CHAR(SYSDATE,‘YY TO_CHAR(CURRENT_DAT
------------------- -------------------
2015-12-22 13:43:38 2015-12-21 21:43:38

select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘),to_char(current_date,‘yyyy-mm-dd hh24:mi:ss‘),current_timestamp from dual;

TO_CHAR(SYSDATE,‘YY TO_CHAR(CURRENT_DAT CURRENT_TIMESTAMP
------------------- ------------------- ---------------------------------------------------------------------------
2015-12-22 13:46:26 2015-12-21 21:46:26 21-DEC-15 09.46.26.500401 PM -08:00

select
to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘),
to_char(current_date,‘yyyy-mm-dd hh24:mi:ss‘),
current_timestamp,
localtimestamp
from dual;

SQL> select dbtimezone,sessiontimezone from dual;

SQL> select extract(month from sysdate) from dual;
SQL> select extract(year from sysdate) from dual;

SQL> select from_tz(timestamp ‘2015-12-22 13:58:00‘,‘+08:00‘) from dual;

SQL> select * from v$timezone_names;
SQL> select tz_offset(‘US/Samoa‘) from dual;

select sysdate+to_yminterval(‘02-06‘) from dual;

select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss‘),
to_char(sysdate+to_dsinterval(‘5 02:10:18‘),‘yyyy-mm-dd hh24:mi:ss‘)
from dual;
--------------------------------------------------------------------------
roll(a,b,c) --> n+1种聚集运算的结果

group by a
group by a,b
group by a,b,c
total

cube(a,b,c) --> 2的n次方种聚集运算的结果
group by a
group by b
group by c
group by a,b
group by a,c
group by b,c
group by a,b,c
total

select deptno,job,sum(sal),grouping(deptno),grouping(job)
from emp group by rollup(deptno,job);

col deptno for a15
select decode(GROUPING(DEPTNO)||GROUPING(JOB),‘01‘,‘subtotal ‘||deptno,‘11‘,‘total ‘,deptno) deptno,job,sum(sal) from emp group by rollup(deptno,job);

select deptno,job,mgr,sum(sal)
from emp group by grouping sets ((deptno,job),(job,mgr));
---------------------------------------------------------------------------
分级查询(爬树):
select level,lpad(ename,length(ename)+level*2-2,‘ ‘) ename
from emp start with empno=7839 connect by prior empno=mgr;

修改爬树的起点: start with
select level,lpad(ename,length(ename)+level*2-2,‘ ‘) ename
from emp start with ename=‘JONES‘ connect by prior empno=mgr;

修改爬树的方向:connect by prior 父键在前向下爬,子键在前向上爬
select level,lpad(ename,length(ename)+level*2-2,‘ ‘) ename
from emp start with ename=‘JONES‘ connect by prior mgr=empno;

剪枝:
剪枝条件出现在where子句,剪一个节点
select level,lpad(ename,length(ename)+level*2-2,‘ ‘) ename
from emp
where ename<>‘BLAKE‘
start with empno=7839 connect by prior empno=mgr;

剪枝条件出现在connect by prior子句,剪一个派系
select level,lpad(ename,length(ename)+level*2-2,‘ ‘) ename
from emp
start with empno=7839 connect by prior empno=mgr and ename<>‘BLAKE‘;
------------------------------------------------------------------------
drop table e01 purge;
drop table e02 purge;
create table e01 as select empno,ename,sal from emp where 1=0;
create table e02 as select ename,sal,comm,deptno from emp where 1=0;

insert all
into e01 values (empno,ename,sal)
into e02 values (ename,sal,comm,deptno)
select empno,ename,sal,comm,deptno from emp;

带条件的insert all:
insert all
when deptno=10 then
into e01 values (empno,ename,sal)
when sal>2000 then
into e02 values (ename,sal,comm,deptno)
select empno,ename,sal,comm,deptno from emp;

带条件的insert first:
insert first
when deptno=10 then
into e01 values (empno,ename,sal)
when sal>2000 then
into e02 values (ename,sal,comm,deptno)
select empno,ename,sal,comm,deptno from emp;

旋转插入:
创建一张表:销售元数据
create table sales_source_data (employee_id number,week_id number,sales_mon number,sales_tue number,sales_wed number,sales_thur number,sales_fri number);

insert into sales_source_data values (178,1,3500,2200,4300,1500,5000);
insert into sales_source_data values (179,2,2800,3300,1000,800,4400);

创建一张表:销售信息表
create table sales_info (employee_id number,week number,sales number);

insert into sales_info select * from
(select employee_id,week_id week,sum(decode(WEEK_ID,1,SALES_MON,2,SALES_mon)) sales
from sales_source_data group by employee_id,week_id
union all
select employee_id,week_id week,sum(decode(WEEK_ID,1,SALES_tue,2,SALES_tue)) sales
from sales_source_data group by employee_id,week_id
union all
select employee_id,week_id week,sum(decode(WEEK_ID,1,SALES_wed,2,SALES_wed)) sales
from sales_source_data group by employee_id,week_id
union all
select employee_id,week_id week,sum(decode(WEEK_ID,1,SALES_thur,2,SALES_thur)) sales
from sales_source_data group by employee_id,week_id
union all
select employee_id,week_id week,sum(decode(WEEK_ID,1,SALES_fri,2,SALES_fri)) sales
from sales_source_data group by employee_id,week_id);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
insert all
into sales_info values (employee_id,week_id,SALES_mon)
into sales_info values (employee_id,week_id,SALES_tue)
into sales_info values (employee_id,week_id,SALES_wed)
into sales_info values (employee_id,week_id,SALES_thur)
into sales_info values (employee_id,week_id,SALES_fri)
select * from sales_source_data;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
create table sales_info (employee_id number,week_id number,day_id varchar2(4),sales number);

insert all
into sales_info values (employee_id,week_id,‘MON‘,SALES_mon)
into sales_info values (employee_id,week_id,‘TUE‘,SALES_tue)
into sales_info values (employee_id,week_id,‘WED‘,SALES_wed)
into sales_info values (employee_id,week_id,‘THUR‘,SALES_thur)
into sales_info values (employee_id,week_id,‘FRI‘,SALES_fri)
select * from sales_source_data;

外部表:
vi /home/oracle/1.txt
----------------------------------------------------------
7369,SMITH,CLERK,7902,1980/12/17:00:00:00,852,,20
7499,ALLEN,SALESMAN,7698,1981/02/20:00:00:00,1673,300,30
7521,WARD,SALESMAN,7698,1981/02/22:00:00:00,1251,500,30
7566,JONES,MANAGER,7839,1981/04/02:00:00:00,2980,,20
7654,MARTIN,SALESMAN,7698,1981/09/28:00:00:00,1290,1400,30
7698,BLAKE,MANAGER,7839,1981/05/01:00:00:00,2900,,30
----------------------------------------------------------

vi /home/oracle/2.txt
----------------------------------------------------------
7782,CLARK,MANAGER,7839,1981/06/09:00:00:00,2450,,10
7839,KING,PRESIDENT,,1981/11/17:00:00:00,5000,,10
7844,TURNER,SALESMAN,7698,1981/09/08:00:00:00,1500,0,30
----------------------------------------------------------

创建逻辑目录并授权:
conn / as sysdba
CREATE DIRECTORY mydir AS ‘/home/oracle‘;
GRANT READ,WRITE ON DIRECTORY mydir TO SCOTT;

创建外部表:
conn scott/tiger
CREATE TABLE scott.refemp
(emp_id number(4),
ename varchar2(12),
job varchar2(12) ,
mgr_id number(4) ,
hiredate date,
salary number(8),
comm number(8),
dept_id number(2))
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER
DEFAULT DIRECTORY mydir
ACCESS PARAMETERS(RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ‘,‘
(emp_id char,
ename char,
job char,
mgr_id char,
hiredate char date_format date mask "yyyy/mm/dd:hh24:mi:ss",
salary char,
comm char,
dept_id char))
LOCATION(‘1.txt‘,‘2.txt‘));

select * from e01 a where exists (select 1 from e01 where a.rowid!=e01.rowid and e01.empno=a.empno);

select * from e01 a where rowid in (select max(rowid) from e01 where e01.empno=a.empno);

delete e01 where rowid not in (select max(rowid) from e01 group by empno,ename,job,hiredate,job,sal,comm,deptno);

找到重复的行
select * from e01 a where exists (select 1 from e01 e where a.rowid!=e.rowid and e.empno=a.empno);

查找重复行的rowid 方法1:
select rowid from e01 a where a.rowid!= (select max(rowid) from e01 e where e.empno=a.empno and e.ename=a.ename);
查找重复行的rowid 方法2:
select rowid from e01 a where rowid not in (select max(rowid) from e01 group by empno,ename);

找到不重复的行
select * from e01 a where not exists (select 1 from e01 e where a.rowid!=e.rowid and e.empno=a.empno);

去掉重复的行:
select * from e01 a where rowid in (select max(rowid) from e01 e where e.empno=a.empno);



本文出自 “梁小明的博客” 博客,请务必保留此出处http://7038006.blog.51cto.com/7028006/1836679

oracle sql语句

标签:oracle   sql语句

原文地址:http://7038006.blog.51cto.com/7028006/1836679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!