oracle表查询

妖精的绣舞 提交于 2019-12-07 02:23:02

2014-04-07

1.查询每个员工对应的部门

SQL>conn hr/hr;

##查询表

SQL>select object_name from user_objects where object_type='TABLE';

##查看两张表结构

SQL>desc employees;

SQL>desc departments;

##关联查询

SQL>select a.first_name || ' ' ||a.last_name name,employee_id,b.department_name  from  employees a join departments b on a.department_id=b.department_id;

2.连接

等值连接:inner join

左连接:left join

右连接:right join

自连接:self join

3.查询员工对应的部门和城市

##查询表

SQL>select object_name from user_objects where object_type='TABLE';

##查看三张表结构

SQL>desc employees;

SQL>desc departments;

HR@orcl> desc locations;

##关联查询

HR@orcl> select a.employee_id,a.department_id,c.city from employees a join departments b on a.department_id=b.department_id join locations c on b.location_id=c.location_id;

##将SQL缓冲区内容保存到文件中

HR@orcl> save 1.sql;

HR@orcl> !

##编辑配置文件

[oracle@oracle53 ~]$ vim 1.sql

================================== 

rem query employee_id,employee_name,department_id,city

set linesize 500;

col name for a20;

col city for a30;

select a.employee_id,a.first_name || ' ' ||a.last_name name,a.department_id,c.cii

ty from employees a join departments b on a.department_id=b.department_id join ll

ocations c on b.location_id=c.location_id

/

====================================

[oracle@oracle53 ~]$ exit

4.所有员工对应的部门

//left [outer] join:不仅会返回满足连接条件(等值连接)的所有记录,而且还会返回不满足条件的连接操作符左边表的其他行。

HR@orcl 14:04:07-09:57:55> select a.employee_id,a.last_name ||' '||a.first_name name,b.department_name from employees a left join departments b on a.department_id=b.department_id;

##传统用例:把(+)放在行数较少的一端,只能用于左右连接

HR@orcl 14:04:07-10:34:56> select a.employee_id,a.last_name ||' '||a.first_name name,b.department_name from employees a,departments b where a.department_id=b.department_id(+);

5.所有部门对应的员工

//right [outer] join:不仅会返回满足连接条件(等值连接)的所有记录,而且还会返回不满足条件的连接操作符右边表的其他行。

select a.employee_id,a.last_name ||' '||a.first_name name,b.department_name from employees a right join departments b on a.department_id=b.department_id;

##传统用例

HR@orcl> select a.employee_id,a.last_name ||' '||a.first_name name,b.department_name from employees a,departments b where a.department_id(+)=b.department_id;

6.查询员工对应的上司

HR@orcl> select a.employee_id,a.last_name ||' '||a.first_name name,b.employee_id,b.last_name||' '||b.first_name mname  from employees a join employees b on a.manager_id=b.employee_id;

7.全连接

HR@orcl> select a.employee_id,a.last_name||' '||a.first_name name,b.department_name from employees a full join departments b on a.department_id=b.department_id;

8.数据分组

组函数又称为多行函数,把多行当作一个整体进行处理。

avg(),sum(),max(),min(),distinct(),group by,having

having:用来限制分组显示结果

group by:对查询结果进行分组统计

9.组函数使用原则:

1)distinct表示值不重复;

2)所有组函数忽略空值,为了用一个值代替空值,用nvl,nvl2,coalesce函数;

----重点强调--------

3)使用group by来分组时,oracle服务器隐式以升序排序结果集

##统计每个部门的平均工资

HR@orcl> select department_id,round(avg(salary),2) from employees group by department_id;

##统计每个部门的最小工资,最大工资

HR@orcl> select department_id,min(salary),max(salary) from employees group by department_id;

##统计每个部门有多少员工

HR@orcl> select department_id,count(employee_id) from employees group by department_id;

##统计每个部门的绩效 ---先处理空值,再求平均值

HR@orcl 14:04:07-11:28:03> select department_id,avg(nvl(commission_pct,0)) from employees group by department_id;

------having--------

sql>select department_id,avg(nvl(commission_pct,0)) from employees group by department_id having avg(nvl(commission_pct,0))>0.2;

10.子查询

子查询是指嵌入在其他的SQL语句中的SELECT语句,也称为嵌套查询。

//单行子查询结果只返回一行,如果用等于只能用单行子查询

##雇员编号为141工作类型,查询所有从事这个工作类型的雇员

SQL> select last_name|| ' '||first_name employee_name,job_id from employees where job_id= (select job_id from employees where employee_id=141);

//多行子查询

SQL> select last_name|| ' '||first_name employee_name,job_id from employees where job_id in (select job_id from employees in employee_id (141,142));

in:等于列表中的任何成员

any:比较子查询返回的每个值

all:比较子查询返回的全部值


SQL> select employee_id,last_name,salary from employees where salary < any (select salary from employees where job_id='IT_PROG') and job_id !='IT_PROG';



SQL> select employee_id,last_name,salary from employees where salary < all (select salary from employees where job_id='IT_PROG') and job_id !='IT_PROG';

11.行记录标识

rownum:行记录在表中的逻辑地址

rowid:行记录在表中的物理地址,十个字节

SQL> select employee_id,last_name,salary,rownum,rowid from employees where salary < all (select salary from employees where job_id='IT_PROG') and job_id !='IT_PROG';

EMPLOYEE_ID LAST_NAME  SALARY     ROWNUM ROWID

----------- ------------------------- ---------- ---------- ------------------

185 Bull    4100  1 AAAR5pAAFAAAADPABV

192 Bell    4000  2 AAAR5pAAFAAAADPABc

193 Everett    3900  3 AAAR5pAAFAAAADPABd

12.数据类型

//字符类型:char(),varchar2(),colb,blob,bfile

varcha2()可变,数据库字符集

//数值类型:number()----oracle 9i数据库引擎支持的

binary_integer,binary_float,binary_double

//时间类型:date.timestamp

HR@orcl 14:04:07-03:25:18> create table d(d timestamp);

HR@orcl 14:04:07-03:25:47> insert into d values(sysdate);

HR@orcl 14:04:07-03:26:42> select * from d;

-----------------------------------------------------------

07-APR-14 03.26.42.000000 PM ---默认精度为6位

-----------------------------------------------------------

//扩展:

oracle:移动表空间中的数据文件

alter tablespace tbs_01 offline;       -----脱机表空间

host mv /tmp/datafile1.dbf /tmp/tbs/datafile1.dbf  -----移动数据文件

alter tablespace tbs_01 rename datafile '/tmp/datafile1.dbf' to '/tmp/tbs/datafile1.dbf'    -----对数据文件进行重命名,也就时告知数据库数据文件的位置

select a.ts#,a.name,b.name from v$tablespace a join v$datafile b on a.ts#=b.ts#; -----查询表空间与数据文件的对应

alter tablespace tbs_01 online;    ------重新联机

///

语法:SELECT 语句1 [UNION|UNION ALL|INTERSECT|MINUS] SELECT 语句2


UNION

UNION操作符用于获取两个结果集的并集。当使用该操作符时,会自动出掉结果集中的重复行,并且会以第一列的结果进行排序。


SELECT ENAME,SAL,JOB FROM EMP WHERE SAL > 2500

union 

SELECT ENAME,SAL,JOB FROM EMP WHERE JOB='MANAGER';


UNION ALL

UNION ALL操作符用于获取两个结果集的并集,但与UNION操作符不同,该操作符不会取消重复值,而且也不会以任何列进行排序。

SELECT ENAME,SAL,JOB FROM EMP WHERE SAL > 2500

UNION ALL

SELECT ENAME,SAL,JOB FROM EMP WHERE JOB='MANAGER';



INTERSECT

INTERSECT操作符用于获取两个结果集的交集,当使用该操作符时,只会显示同时存在于两个结果集中的数据,并且会以第一列进行排序。

SELECT ENAME,SAL,JOB FROM EMP WHERE SAL > 2500

INTERSECT

SELECT ENAME,SAL,JOB FROM EMP WHERE JOB='MANAGER';


MINUS

把集合{x∣x∈A,且x∉B}叫做A与B的差集,记作A-B

MINUS操作符用于获取两个结果集的差集,当使用该操作符时,只会显示在第一个结果集中存在,在第二个结果集中不存在的数据,并且会以第一列进行排序。


SELECT ENAME,SAL,JOB FROM EMP WHERE SAL > 2500

MINUS

SELECT ENAME,SAL,JOB FROM EMP WHERE JOB='MANAGER';


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!