Presentation is loading. Please wait.

Presentation is loading. Please wait.

中央大學。范錚強 1 資料庫查詢:關係數學 ──Relational Algebra and Calculus 國立中央大學 資訊管理系 范錚強、楊書成 2008.12.

Similar presentations


Presentation on theme: "中央大學。范錚強 1 資料庫查詢:關係數學 ──Relational Algebra and Calculus 國立中央大學 資訊管理系 范錚強、楊書成 2008.12."— Presentation transcript:

1 中央大學。范錚強 1 資料庫查詢:關係數學 ──Relational Algebra and Calculus 國立中央大學 資訊管理系 范錚強、楊書成 2008.12

2 中央大學。范錚強 2 關係數學: algebra & calculus 兩種正規的數學 Relational algebra Operational Procedural language (at a SET level) 一歩歩描述計算結果的步驟 Relational calculus 邏輯的一部份 Declarative Non-procedural language 只描述想要的結果 SQL 的數學表示法  SQL 的重要基礎

3 中央大學。范錚強 3 Relational algebra (1) 5 個基本的運算子 (operators) Selection ( σ ) - select rows from relation Projection (Π) - retain only wanted columns from relation Cross-product (X) - combine 2 relations Set-difference ( - ) - tuples in r1, but not in r2 Union ( ∪ ) - tuples in r1 and/or in r2

4 中央大學。范錚強 4 Relational algebra (2) Hypothesized relation schema S (sid, sname, age) C (cid, cname) R (sid, cid) 語法範例 σ age>20 (S1) Π sname, age (S1) Π sname, age (σ age>20 (S1)) S1 ∪ S2, S1 - S2, S1 X R

5 中央大學。范錚強 5 Relational algebra (3) sidsnameage s001Henry23 s002Jason18 s003Vivian21 s004Susan19 S1 sidsnameage s001Henry23 s003Vivian21 σ age>20 (S1) snameage Henry23 Jason18 Vivian21 Susan19 Π sname, age (S1) snameage Henry23 Vivian21 Π sname, age (σ age>20 (S1))

6 中央大學。范錚強 6 Relational algebra (4) sidsnameage s001Henry23 s002Jason18 s003Vivian21 s004Susan19 S1 sidsnameage s002Jason18 s004Susan19 s006Candy22 S2 sidsnameage s001Henry23 s002Jason18 s003Vivian21 s004Susan19 s006Candy22 S1 ∪ S2 sidsnameage s001Henry23 s003Vivian21 S1 - S2

7 中央大學。范錚強 7 Relational algebra (5) sidsnameage s001Henry23 s002Jason18 s003Vivian21 s004Susan19 S1 sidcid s002c101 s003c082 R (sid)snameage(sid)cid s001Henry23s002c101 s001Henry23s003c082 s002Jason18s002c101 s002Jason18s003c082 s003Vivian21s002c101 s003Vivian21s003c082 s004Susan19s002c101 s004Susan19s003c082 S1 X R

8 中央大學。范錚強 8 Relational algebra (6) 使用 union 及 set-difference 時,兩個 relation 必須是 union-compatible 欄位數相同 相對應的欄位具有相同的資料型態 使用 cross-product 時,發生欄位名稱衝突 使用 renaming(ρ) 運算子 ρ(C(1  sid1, 5  sid2), S1 X R)

9 中央大學。范錚強 9 Relational algebra (7) 其他運算子 Intersection (∩) - tuples in r1 and in r2 S1∩S2 = S1 - (S1 - S2) Condition join (∞ c ) - cross-product followed by a selection S1 ∞ S1.sid<R.sid R = σ S1.sid<R.sid (S1X R) Equijoin - special case of condition join 利用兩個 relation 中欄位相等之條件進行合併 相同欄位只會出現一次 S1∞ S1.sid=R.sid R Natural join 利用兩個 relation 中所有相同名稱欄位相等之條件進行合併 S1∞R Division ( / )

10 中央大學。范錚強 10 Relational algebra (8) sidsnameage s001Henry23 s002Jason18 s003Vivian21 s004Susan19 S1 sidsnameage s002Jason18 s004Susan19 s006Candy22 S2 sidsnameage s002Jason18 s004Susan19 S1 ∩ S2

11 中央大學。范錚強 11 Relational algebra (9) sidsnameage s001Henry23 s002Jason18 s003Vivian21 s004Susan19 S1 sidcid s002c101 s003c082 R (sid)snameage(sid)cid s001Henry23s002c101 s001Henry23s003c082 s002Jason18s003c082 S1∞ S1.sid<R.sid R (sid)snameagecid s002Jason18c101 s003Vivian21c082 S1∞ S1.sid=R.sid R (sid)snameagecid s002Jason18c101 s003Vivian21c082 S1∞R

12 中央大學。范錚強 12 Relational calculus (1) Tuple relational calculus (TRC) General form: {t|Ψ(t)} {tuple variable|TRC formula} Tuple vairable = set of rows from relations Domain relational calculus (DRC) General form: {a, b, c, …|Ψ(a, b, c, …)} { predicate ( domain variables)|DRC formula} Domain vaiable = set of columns from relations QBE: Query by Example { predicate | formula } true

13 中央大學。范錚強 13 Relational calculus (2) 運算子 (operators), =, ≤, ≥, ≠ ¬, ∧, ∨ 資格限制 (quantifiers) ∃ - exist ( 有一個 ) ∀ - for all ( 每一個 ) ¬ ∃ - not exist ¬ ∀ - not for all

14 中央大學。范錚強 14 Relational calculus (3) Hypothesized relation schema S (sid, did, sname) R (sid, cid, grade) 列出所有資管系學生的 sid, did, sname TRC - {t|S(t) ∧ t[2]=“43”} DRC - {abc|S(abc) ∧ b=“43”} 列出所有資管系學生的 sid 及 sname TRC - {t| ∃ uS(u) ∧ u[2]=“43” ∧ t[1]=u[1] ∧ t[2]=u[3]} DRC - {ab| ∃ cS(acb) ∧ c=“43”}

15 中央大學。范錚強 15 Relational calculus (4) 列出曾經被當的學生 sid, did, sname TRC - {t|S(t) ∧∃ uR(u) ∧ t[1]=u[1] ∧ u[3]<“70”} DRC - {abc|S(abc) ∧∃ d ∃ eR(ade) ∧ e<“70”} or {abc|S(abc) ∧∃ eR(a . e) ∧ e<“70”} SQL – select * from S where sid in (select sid from R where grade<“70”) or select S.sid, S.did, S.sname from S, R where S.sid = R.sid and R.grade<“70”

16 中央大學。范錚強 16 For all 下的排除條款 在大部分需要 For all 的查詢(例如:每一門課都當掉) 中,我們需要排除一些不需要的東西 只要是我們檢驗的學生的課,我們才需要檢查是否及格 如果不是該學生,就不需要檢查 For 每一筆學生成績,如果是該位同學 (If A) ,都是不及格 的成績 (then B)  For 每一筆學生成績,如果不是是該位同 學 (not A ) ,不需檢查,否則需要不及格 (or B) A B ¬A ¬B If A then B  (¬A ∨ B)

17 中央大學。范錚強 17 Relational calculus (5) 列出所有課都被當的學生 sid, did, sname TRC - {t|S(t) ∧∀ uR(u) ∧ (t[1]≠u[1] ∨ u[3]<“70”)} or {t|S(t) ∧ ¬ ∃ uR(u) ∧ (t[1] =u[1] ∧ u[3] ≥“70”)} SQL - select * from S where not exists (select * from R where S.sid = sid and grade>=“70”) SQL 有 exist 和 not exist ,沒有 for all A B ¬A ¬B ¬( ¬ A ∨ B) = (A ∧ ¬B)

18 中央大學。范錚強 18 Relational calculus (6) Hypothesized relation schema S (sid, did, sname) R (sid, tid) T (tid, tname, did) 列出被所有老師教過的學生 sid, did, sname {t|S(t) ∧∀ uT(u) ∧∃ vR(v) ∧ t[1]=v[1] ∧ u[1]=v[2]} 列出學生,修過所有資管系老師的課 {t|S(t) ∧∀ uT(u) ∧ (u[3]≠”43” ∨ ( ∃ vR(v) ∧ t[1]=v[1] ∧ u[1]=v[2]))}


Download ppt "中央大學。范錚強 1 資料庫查詢:關係數學 ──Relational Algebra and Calculus 國立中央大學 資訊管理系 范錚強、楊書成 2008.12."

Similar presentations


Ads by Google