oracle表查詢

2014-04-07sql

1.查詢每一個員工對應的部門數據庫

SQL>conn hr/hr;vim

##查詢表服務器

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

##查看兩張表結構函數

SQL>desc employees;spa

SQL>desc departments;排序

##關聯查詢ci

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;rem

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';

相關文章
相關標籤/搜索