回顧sql語句中的各種串連

來源:互聯網
上載者:User

1. 內串連(InnerJoin
內串連是最常見的一種串連,它頁被稱為普通串連,而E.FCodd最早稱之為自然串連。
下面是ANSI SQL-92標準
select *
from    t_institution i
inner join t_teller t
on i.inst_no = t.inst_no
where i.inst_no = "5801"
其中inner可以省略。
等價於早期的串連文法
select *
from t_institution i, t_teller t
where i.inst_no = t.inst_no
and i.inst_no = "5801"

2. 外串連
2.1 左外串連(Left Outer Jion)
select *
from    t_institution i
left outer join t_teller t
on i.inst_no = t.inst_no
其中outer可以省略。
2.2 右外串連(Rigt Outer Jion)
select *
from    t_institution i
right outer join t_teller t
on i.inst_no = t.inst_no
2.3 全外串連(Full Outer)
全外串連返回參與串連的兩個資料集合中的全部資料,無論它們是否具有與之相匹配的行。在功能上,它等價於對這兩個資料集合分別進行左外串連和右外串連,然後再使用消去重複行的並操作將上述兩個結果集合并為一個結果集。
在現實生活中,參照完整性條件約束可以減少對於全外串連的使用,一般情況下左外串連就足夠了。在資料庫中沒有利用清晰、規範的約束來防範錯誤資料情況下,全外串連就變得非常有用了,你可以使用它來清理資料庫中的資料。
select *
from    t_institution i
full outer join t_teller t
on i.inst_no = t.inst_no
2.4 外串連與條件配合使用
當在內串連查詢中加入條件是,無論是將它加入到join子句,還是加入到where子句,其效果是完全一樣的,但對於外串連情況就不同了。當把條件加入到join子句時,SQL Server、Informix會返回外串連表的全部行,然後使用指定的條件返回第二個表的行。如果將條件放到where子句中,SQL Server將會首先進行串連操作,然後使用where子句對串連後的行進行篩選。下面的兩個查詢展示了條件放置位子對執行結果的影響:
條件在join子句
select *
from    t_institution i
left outer join t_teller t
on i.inst_no = t.inst_no
and i.inst_no = “5801”
結果是:
inst_no      inst_name              inst_no      teller_no    teller_name
5801         天河區                 5801         0001         tom
5801         天河區                 5801         0002         david
5802         越秀區
5803         白雲區
條件在where子句
select *
from    t_institution i
left outer join t_teller t
on i.inst_no = t.inst_no
where i.inst_no = “5801”
結果是:
inst_no      inst_name              inst_no      teller_no    teller_name
5801         天河區                 5801         0001         tom
5801         天河區                 5801         0002         david

3. 自身串連
自身串連是指同一個表自己與自己進行串連。這種一元串連通常用於從自反關係(也稱作遞迴關係)中抽取資料。例如人力資源資料庫中僱員與老闆的關係。
下面例子是在機構表中尋找本機構和上級機構的資訊。
select s.inst_no superior_inst, s.inst_name sup_inst_name, i.inst_no, i.inst_name
from t_institution i
join t_institution s
on i.superior_inst = s.inst_no

結果是:
superior_inst sup_inst_name          inst_no      inst_name
800             廣州市                 5801         天河區
800             廣州市                 5802         越秀區
800             廣州市                 5803         白雲區

4. 交叉(無限制) 串連
交叉串連用於對兩個源表進行純關係代數的乘運算。它不使用串連條件來限制結果集合,而是將分別來自兩個資料來源中的行以所有可能的方式進行組合。資料集合中一的每個行都要與資料集合二中的每一個行分別組成一個新的行。例如,如果第一個資料來源中有5個行,而第二個資料來源中有4個行,那麼在它們之間進行交叉串連就會產生20個行。人們將這種類型的結果集稱為笛卡爾乘積。
大多數交叉串連都是由於錯誤操作而造成的;但是它們卻非常適合向資料庫中填充例子資料,或者預先建立一些空行以便為程式執行期間所要填充的資料保留空間。
select *
from    t_institution i
cross join t_teller t

在交叉串連中沒有on條件子句


感謝 http://www.cnblogs.com/xionglee/articles/1445754.html

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.