Chapter 3 Relational Model Table of Contents 1. Structure of Relational Databases 2. Relational Algebra 3. Tuple Relational Calculus 4. Domain Relational Calculus Chapter 3-1 1
1. Structure of Relational Databases z Basic Structure z Database Schema z Query Language Chapter 3-2 Basic Structure z Relational Database ii aq1 9aÑI Y ée: Entity- Relationshipa = 9aI Iñ z Table (Relation) Schema: Set of Attribute (Column, Field) Instance: Set of Record (Row, Tuple) z Domain î Attribute½ µ ÿñe ¾V õ: Customer(aq - %} 20, a - } 3, ) A table T of n attributes and D i : domain of i th attributes T D 1 D 2 D n-1 D n Chapter 3-3 2
Database Schema - ER Diagram account-number branch-city balance branch-name assets Account Account- Branch Branch Depositor Loan- Branch Customer Borrower Loan customer-name customer-city account-number street balance Chapter 3-4 Database Schema - Sample z Four Sample Schema - Entity Customer = (customer-name, street, customer-city) Branch = (branch-name, branch-city, assets) Account = (branch-name, account-number, balance) Loan = (branch-name, loan-number, amount) z Two Sample Schema - Relationship Depositor = (customer-name, account-number) Borrower = (customer-name, loan-number) Chapter 3-5 3
Database Schema - Design Issue z Single Relation Deposit + Customer Account-Info Account-Info = (branch-name, account-number, balance, customer-name, street, customer-city) z Problem Repetition of Information Incomplete Tuples z Advantage? Chapter 3-6 Formal Query Languages z Query Language Language in which a user requests information from DB Higher than programming languages z Two Categories of Query Language Procedural: éi ñi Ý Example: Relational Algebra Nonprocedural: Relational Calculus i z Types of Query Languages Relational Algebra, Tuple Calculus, Domain Calculus Chapter 3-7 4
2. Relational Algebra z  Procedural Query Language E a E Relation1 r)i EÙ µi é Relation1 Ê z Fundamental Operations Unary Operation: Select, Project, Rename Binary Operations: Cartesian Product, Union, Set Difference z Additional Operations Set Intersection, Join, Division, Assignment Chapter 3-8 Select z Notation: σ P (r) P: Predicate (Sequence of Comparison/Logical Operators) Operators:,, =,, <,, >, r: Relation Loan 9a½ Perryridge ýe á? σ branch-name = Perryridge (Loan) Perryridge ý½ $1200 a í I? σ branch-name = Perryridge amount > 1200 (Loan) Chapter 3-9 5
Project z Notation: Π A1, A2,, Ar (r) Ai: Attribute of Relation r s Loan 9a½ ±y u%- í õn? Π loan-number, amount (Loan) Loan 9a½ í õna $3000 a e ±ye u%- í õn? Π loan-number, amount (σ amount > 3000 (Loan)) Composition of Relational Operations Chapter 3-10 Union z Notation: r s r and s are compatible relations. Attribute Æi Attribute type Æi (Domain: Equal) -vé ] E Í ÍÑE aq? Π customer-name (borrower) Π customer-name (depositor) Chapter 3-11 6
Set Difference z Notation: r - s r and s are compatible relations. Attribute Æi Attribute type Æi (Domain: Equal) -v½ õõ- Ea í - 7- Í Í ÑE aq? Π customer-name (borrower) - Π customer-name (depositor) Chapter 3-12 Cartesian Product z Notation: r s é relatione É Attribute : re attribute + se attribute õaé : re õaé se õaé Perryridge ý½ í - ÍÑE aq? Loan = (branch-name, loan-number, amount) Borrower = (customer-name, loan-number) Π customer-name (σ loan.loan-number = borrower.loan-number ( σ branch-name = Perryridge (Loan Borrower))) Chapter 3-13 7
Rename z Notation: ρ x(a1, A2,, An) (E) Expression EE éi x½ Mæ (Ai: Attribute list of x) Find the largest account balance in the bank. Π balance (account) - Π account.balance (σ account.balance < d.balance (account ρ d (account))) Smith- ÆiI ±É ü ] ½ ]ée Í Í? Π customer.customer-name (σ customer. street = smith-addr.street customer.customer-city = smith-addr.city (customer ρ smith-addr(street, city) (Π street, customer-city (σ customer-name = Smith (customer))))) Chapter 3-14 Formal Definition of Relational Algebra z Basic Expression A relation in the database A constant relation z General Relational Algebra Expression E 1 E 2 (E 1 and E 2 : Relational algebra expressions) E 1 - E 2 E 1 E 2 σ P (E 1 ), P: a predicate on attribute in E 1 Π S (E 1 ), S: a list consisting of some attributes in E 1 ρ x (E 1 ), x: a new name for the result of E 1 Chapter 3-15 8
Additional Operations z Set Intersection Operation z Natural Join Operation z Division Operation z Assignment Operation Chapter 3-16 Set Intersection z Notation: r s r and s are compatible relations. Attribute Æi Attribute type Æi (Domain: Equal) r s = r - (r - s) Find all customers who have both a loan and an account Π customer-name (borrower) Π customer-name (depositor) Chapter 3-17 9
Natural Join z Notation r s = Π R S (σ r.a1 = s.a1 r.a2 = s.a2 r.an = s.an (r s)) If R S =, then r s = r s Theta Join: r θ s = σ θ (r s) Harrison ½ Í u Ía õõe -v? Π branch-name (σ customer-city= Harrison (customer account depositor)) Find all customers who have both a loan and an account Π customer-name (borrower depositor) Chapter 3-18 Division z Notation: r ø s For All ½ í µe½ r(a, B) ø s(b) Brooklyn ½ ñei Í -v½ ±yi í± Í Π customer-name, branch-name (depositor account) ø Π branch-name (σ branch-city= Brooklyn (branch)) z Note r ø s = Π R-S (r) - Π R-S ((Π R-S (r) s) - Π R-S,S (r)) Chapter 3-19 10
Assignment z Notation: r E Relational algebra expression EE éi õae r ½ Mæ âži Ê1 ÙÙ E Ê)I qmm 9 : r ø s temp1 Π R-S (r) temp2 Π R-S ((temp1 s) - Π R-S,S (r)) result = temp1 - temp2 Chapter 3-20 3. Tuple Relational Calculus z Tuple Calculus½ QueryE Iñ Ê: {t P(t)} t: tuple variable, P: predicate Eå: Pi %)I E Í tupleñe ¾V t[a] or t[1]: Component of a tuple variable $1200a í I ±ye -v, ±yu%, í õn? {t t loan t[amount] > 1200} Chapter 3-21 11
Existential Quantifier: z Notation: t r (Q(t)) Relation r½ ]a Qi ¹^E tuple tí a? s $1200a í I ±ye ±yu%? {t s loan(t[loan-number] = s[loan-number] s[amount] > 1200} Perryridge ý½ í I ÍE aq? {t s borrower(t[customer-name] = s[customer-name] u loan(u[loan-number] = s[loan-number] u[branch-name] = Perryridge ))} Chapter 3-22 Universal Quantifier: z Notation: t r (Q(t)) Relation re Í tupleña ]a Qi ¹^? Brooklyn ½ ñei Í ý½ õõi Í? {t u branch(u[branch-city] = Brooklyn s depositor(t[customer-name] = s[customer-name] w account(w[account-no] = s[account-no] w[branch-name] = u[branch-name])))} Chapter 3-23 12
Formal Definition z Expression Form {t P(t)}, P: formula A formula is built up out of atoms. z Atom s r (s: tuple variable, r: relation) s[x] Θ u[y] (s, u: tuple variable, x: attribute, Θ: <,, =,, >, ) s[x] Θ c (s: tuple variable, x: attribute, c: constant) Chapter 3-24 Formal Definition - ±z z Formulai ~E ÉF An atom is a formula. P 1 : formula, then so are P 1 and (P 1 ). P 1, P 2 : formulae, then so are P 1 P 2, P 1 P 2, P 1 P 2 P 1 (s): formula and r: relation, then s r (P 1 (s)) and s r (P 1 (s)) are formulae. z Equivalence Rule P 1 P 2 ( P 1 P 2 ) t r (P 1 (t)) t r ( P 1 (t)) P 1 P 2 P 1 P 2 Chapter 3-25 13
Safety of Expression z Problem of Tuple Relational Calculus Infinite Relation Example: {t (t loan)} z Concept of Domain: dom(p) Set of all values that appear explicitly in P, or Set of all values that appear in one or more relations whose names appear in P. Example: dom(t loan t[amount] > 1200) {1200} loan Chapter 3-26 Concept of Safe Expression z {t P(t)}í safei ]a All values that appear in the result are values from dom(p) s {t t loan t[amount] > 1200} safe {t (t loan)} safe z Expressive Power of Languages The tuple relational calculus restricted to safe expression is equivalent in expressive power to the relational algebra. Chapter 3-27 14
4. Domain Relational Calculus z Notation: {<x 1, x 2,, x n > P(x 1, x 2,, x n )} x i : domain variable P: formula composed of atoms z Formal Definition of Domain Relational Calculus Tuple relational calculus- ]E Æi aý Atom: domain variable½ ía E Constant: domain constant Notational Shorthand a, b, c (P(a, b, c)) = a ( b ( c (P(a, b, c)))) Chapter 3-28 Example Queries z $1200a í I ±ye -v, ±yu%, õn? {< b, l, a > < b, l, a > loan a > 1200} z $1200a í I ±ye ±yu%? {< l > a, b (< b, l, a > loan a > 1200)} z Perryridge ý½ í I Í ü í õn? {< c, a > l (< c, l > borrower b (< b, l, a > loan b = Perryridge ))} z Brooklyn E Í ý½ õõi Í? {< c > x, y, z (< x, y, z > branch y = Brooklyn a, b (< x, a, b > account < c, a > depositor ))} Chapter 3-29 15
Expressive Power of Languages All three of the following are equivalent: z The relational algebra z The tuple relational calculus restricted to safe expressions z The domain relational calculus restricted to safe expressions Chapter 3-30 16