oracle中join學習個人筆記


3 joins
3.2連接條件
using
相同的列連接可用using子句,且select列中不能對條件列限定表名
using子句可使用多個列,如using(a,b)
SELECT location_id, department.name, location.regional_group
FROM department JOIN location
USING (location_id);

natural join
無需使用using子句
select location_id,department.name,location.regional_group
from location natural join department
會自動匹配兩個表中所有相同的列,影響輸出


3.3連接類型
cross join 笛卡爾積
不能帶條件,如on子句

inner join 內連接
默認的連接,inner可省略

outer join 外連接
FROM table1 { LEFT | RIGHT | FULL } [OUTER] JOIN table2
left:返回table1中的所有記錄,table2對應列顯示空
right:返回table2中的所有記錄,table1對應列顯示空
full: 不匹配的記錄全部顯示
out:默認可缺省,oracle會自動在left,right,full后增加out
   
    left out join
    select d.dept_id,d.name,l.regional_group
    from department d left outer join location l
    on d.location_id=l.location_id
    早期的寫法
    select d.dept_id,d.name,l.regional_group
    from department d,location l
    where d.location_id=l.location_id(+)
    返回department表里所有記錄
   
   
    right out join
    select d.dept_id,d.name,l.regional_group
    from department d right outer join location l
    on d.location_id=l.location_id
    早期的寫法
    select d.dept_id,d.name,l.regional_group
    from department d,location l
    where d.location_id(+)=l.location_id
    返回location表里所有記錄
   
    full out join
    兩張表不匹配的記錄都顯示

相等連接與不等連接
不等連接示例
select p.name part_name,c.inv_class inv_class
from part p join inventory_class c
on p.unit_cost between c.low_cost and c.high_cost

self join
自聯接
Self outer joins
select e.lname employee, m.lname manager
from employee e left outer join employee m
on e.manager_emp_id = m.emp_id

partition outer joins
oracle10g的新特性,與lag,lap查詢有關系
select nvl(ee.emp_id, 7782), m.year, m.month, nvl(ee.expense_claim, 0)
  from (select * from months where year = 2002) m
  left outer join (select * from employee_expense where emp_id = 7782) ee
    on m.month = ee.month
   and m.year = ee.year
 order by m.month

可用partition outer joins實現
SELECT ee.emp_id, m.year, m.month, NVL(ee.expense_claim, 0)
  FROM (SELECT * FROM months WHERE year = 2002) m
  LEFT OUTER JOIN employee_expense ee PARTITION BY(ee.emp_id)
    ON m.year = ee.year
   AND m.month = ee.month
 ORDER BY ee.emp_id, m.month;
原理
首先對employee_expense按emp_id分組,一個值一組
然后每組都與months表聯接,會有多個外聯接操作


3.4joins and subqueries
聯接與子查詢
SELECT supplier_id, name
  FROM supplier s
 WHERE EXISTS (SELECT *
          FROM part p
         WHERE p.inventory_qty < 10
           AND p.supplier_id = s.supplier_id);
可用以下表聯接查詢實現          
SELECT s.supplier_id, s.name
  FROM supplier s
  JOIN part p
    ON p.supplier_id = s.supplier_id
 WHERE p.inventory_qty < 10;

3.5對聯接視圖執行DML
聚合函數 Aggregate functions, such as AVG, COUNT, MAX, MIN, SUM
分析函數 CUME_DIST
表聯接視圖如果有以下情況不能執行DML
層次查詢語句,如start with 或 connect by
group by 或 order by
Model查詢?
集合操作符,如union,intersect,minus
聚合函數
分析函數
select部分有子查詢或關聯查詢
distinct
有read only選項
有rownum偽列


鍵保留表(key-preserved table)不是表的特性,是對聯接視圖中的表而言
對此類視圖的dml是對上述表的dml

轉自http://space.itpub.net/777981/viewspace-674911


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM