Download presentation
Presentation is loading. Please wait.
1
CMSC424, Spring 2005 CMSC424: Database Design Lecture 4
2
CMSC424, Spring 2005 Review: Relational Data Model Key Abstraction: Relation Given sets: R = {1, 2, 3}, S = {3, 4} R S = { (1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4) } A relation on R, S is any subset ( ) of R S (e.g: { (1, 4), (3, 4)}) Mathematical relations Account Branches Accounts Balances { (Downtown, A-101,500), (Brighton, A-201, 900), (Brighton, A-217, 500) } Database relations Given attribute domains Branches = { Downtown, Brighton, … } Accounts = { A-101, A-201, A-217, … } Balances = R bnameacct_nobalance Downtown Brighton A-101 A-201 A-217 500 900 500
3
CMSC424, Spring 2005 Review: Terms and Definitions 1.Tables = Relations 2.Columns = Attributes 3.Rows = Tuples 4.Relation Schema (or Schema) A list of attributes and their domains We will require the domains to be atomic E.g. account(account-number, branch-name, balance) 5.Relation Instance A particular instantiation of a relation with actual values Will change with time
4
CMSC424, Spring 2005 Bank Database: Schema Account bnameacct_nobalance Depositor cnameacct_no Customer cnamecstreetccity Branch bnamebcityassets Borrower cnamelno Loan bnamelnoamt
5
CMSC424, Spring 2005 Bank Database: An Instance Account bnameacct_nobalance Downtown Mianus Perry R.H. Brighton Redwood Brighton A-101 A-215 A-102 A-305 A-201 A-222 A-217 500 700 400 350 900 700 750 Depositor cnameacct_no Johnson Smith Hayes Turner Johnson Jones Lindsay A-101 A-215 A-102 A-305 A-201 A-217 A-222 Customer cnamecstreetccity Jones Smith Hayes Curry Lindsay Turner Williams Adams Johnson Glenn Brooks Green Main North Main North Park Putnam Nassau Spring Alma Sand Hill Senator Walnut Harrison Rye Harrison Rye Pittsfield Stanford Princeton Pittsfield Palo Alto Woodside Brooklyn Stanford Branch bnamebcityassets Downtown Redwood Perry Mianus R.H. Pownel N. Town Brighton Brooklyn Palo Alto Horseneck Bennington Rye Brooklyn 9M 2.1M 1.7M 0.4M 8M 0.3M 3.7M 7.1M Borrower cnamelno Jones Smith Hayes Jackson Curry Smith Williams Adams L-17 L-23 L-15 L-14 L-93 L-11 L-17 L-16 Loan bnamelnoamt Downtown Redwood Perry Downtown Mianus R.H. Perry L-17 L-23 L-15 L-14 L-93 L-11 L-16 1000 2000 1500 500 900 1300
6
CMSC424, Spring 2005 Review: Keys and Relations 1. Superkeys set of attributes of table for which every row has distinct set of values 2. Candidate keys “minimal” superkeys 3. Primary keys DBA-chosen candidate keys As in the E/R Model: Act as Integrity Constraints i.e., guard against illegal/invalid instance of given schema e.g., Branch = (bname, bcity, assets) bnamebcityassets Brighton Brooklyn Boston 5M 3M Invalid!!
7
CMSC424, Spring 2005 More on Keys Determining Primary Keys If relation schema derived from E-R diagrams, we can determine the primary keys using the original entity and relationship sets Otherwise, same way we do it for E-R diagrams Find candidate keys (minimal sets of attributes that can uniquely identify a tuple) Designate one of them to be primary key Foreign Keys If a relation schema includes the primary key of another relation schema, that attribute is called the foreign key
8
CMSC424, Spring 2005 Schema Diagram for the Banking Enterprise
9
CMSC424, Spring 2005 Relational Query Languages Theoretical QL’s give semantics to Practical QL’s Recall: Query = “Retrieval Program” Theoretical: 1.Relational Algebra 2.Relational Calculus a.Tuple Relational Calculus (TRC) b.Domain Relational Calculus (DRC) Practical: 1.SQL (originally: SEQUEL from System R) 2.Quel (used in Ingres) 3.Datalog (Prolog-like – used in research lab systems) Language Examples:
10
CMSC424, Spring 2005 Relational Algebra Basic Operators 1.select ( σ ) 2.project ( ) 3.union ( ) 4.set difference ( – ) 5.cartesian product ( ) 6.rename ( ρ ) Relational Operator Relation
11
CMSC424, Spring 2005 Select ( σ ) Notation: σ predicate (Relation) Relation: Can be name of table, or another query Predicate: 1. Simple attribute 1 = attribute 2 attribute = constant value (also: ≠,, ≤, ≥) 2. Complex predicate AND predicate predicate OR predicate NOT predicate
12
CMSC424, Spring 2005 Select ( σ ) Examples: bnamebcityassets Downtown Brighton Brooklyn 9M 7.1M bnamebcityassets DowntownBrooklyn9M σ bcity = “Brooklyn” (branch) = σ assets > 8M (σ bcity = “Brooklyn” ( branch )) = Notation: σ predicate (Relation)
13
CMSC424, Spring 2005 Project ( ) Notation: A1, …, An (Relation) Each A i an attribute Idea: selects columns (vs. σ which selects rows) cstreet, ccity (customer) = cstreetccity Main North Park Putnam Nassau Spring Alma Sand Hill Senator Walnut Harrison Rye Pittsfield Stanford Princeton Pittsfield Palo Alto Woodside Brooklyn Stanford Examples:
14
CMSC424, Spring 2005 Project ( ) Examples: Notation: A1, …, An (Relation) Each A i an attribute Idea: selects columns (vs. σ which selects rows) bcity ( σ assets > 5M (branch) ) = bcity Brooklyn Horseneck
15
CMSC424, Spring 2005 Union ( ) Notation: Relation 1 Relation 2 Example: ( cname (depositor)) ( cname (borrower)) = cname Johnson Smith Hayes Turner Jones Lindsay Jackson Curry Williams Adams R S valid only if: 1.R, S have same number of columns (arity) 2.R, S corresponding columns have same domain (compatibility)
16
CMSC424, Spring 2005 Set Difference ( – ) bnamelnoamount Downtown Redwood Perry Downtown Perry L-17 L-23 L-15 L-14 L-16 1000 2000 1500 1300 Notation: Relation 1 - Relation 2 R - S valid only if: 1.R, S have same number of columns (arity) 2.R, S corresponding columns have same domain (compatibility) Example: ( bname ( σ amount ≥ 1000 (loan))) – ( bname ( σ balance < 800 (account))) = bnameacct_nobalance Mianus Brighton Redwood Brighton A-215 A-201 A-222 A-217 700 900 700 750
17
CMSC424, Spring 2005 Set Difference ( – ) bnamelnoamount Downtown Redwood Perry Downtown Perry L-17 L-23 L-15 L-14 L-16 1000 2000 1500 1300 Notation: Relation 1 - Relation 2 R - S valid only if: 1.R, S have same number of columns (arity) 2.R, S corresponding columns have same domain (compatibility) Example: ( bname ( σ amount ≥ 1000 (loan))) – ( bname ( σ balance < 800 (account))) = bnameacct_nobalance Mianus Brighton Redwood Brighton A-215 A-201 A-222 A-217 700 900 700 750 – = bname Downtown Perry
18
CMSC424, Spring 2005 Cartesian Product ( ) Notation: Relation 1 Relation 2 Example: R S like cross product for mathematical relations: every tuple of R appended to every tuple of S depositor borrower = depositor. cname acct_noborrower. cname lno Johnson Smith … A-101 A-215 … Jones Smith Hayes Jackson Curry Smith Williams Adams Jones … L-17 L-23 L-15 L-14 L-93 L-11 L-17 L-16 L-17 … How many tuples in the result? A: 56
19
CMSC424, Spring 2005 Rename ( ρ ) Notation: identifier (Relation) renames a relation, or Notation: identifier0 (identifier1, …, identifiern) (Relation) renames relation and columns of n-column relation Use: massage relations to make , – valid, or more readable
20
CMSC424, Spring 2005 Rename ( ρ ) Notation: identifier0 (identifier1, …, identifiern) (Relation) renames relation and columns of n-column relation Example: res (dcname, acctno, bcname, lno) (depositor borrower) = depositor. cname acct_noborrower. cname lno Johnson Smith … A-101 A-215 … Jones Smith Hayes Jackson Curry Smith Williams Adams Jones … L-17 L-23 L-15 L-14 L-93 L-11 L-17 L-16 L-17 …
21
CMSC424, Spring 2005 Rename ( ρ ) Notation: identifier0 (identifier1, …, identifiern) (Relation) renames relation and columns of n-column relation Example: res (dcname, acctno, bcname, lno) (depositor borrower) = dcnameacctnobcnamelno Johnson Smith … A-101 A-215 … Jones Smith Hayes Jackson Curry Smith Williams Adams Jones … L-17 L-23 L-15 L-14 L-93 L-11 L-17 L-16 L-17 … res =
22
CMSC424, Spring 2005 Example Query in RA Determine lno’s for loans that are for an amount that is larger than the amt of some other loan. (i.e. lno’s for all non-minimal loans) Temp 1 … Temp 2 … Temp 1 … … Can do in steps:
23
CMSC424, Spring 2005 Bank Database: An Instance Account bnameacct_nobalance Downtown Mianus Perry R.H. Brighton Redwood Brighton A-101 A-215 A-102 A-305 A-201 A-222 A-217 500 700 400 350 900 700 750 Depositor cnameacct_no Johnson Smith Hayes Turner Johnson Jones Lindsay A-101 A-215 A-102 A-305 A-201 A-217 A-222 Customer cnamecstreetccity Jones Smith Hayes Curry Lindsay Turner Williams Adams Johnson Glenn Brooks Green Main North Main North Park Putnam Nassau Spring Alma Sand Hill Senator Walnut Harrison Rye Harrison Rye Pittsfield Stanford Princeton Pittsfield Palo Alto Woodside Brooklyn Stanford Branch bnamebcityassets Downtown Redwood Perry Mianus R.H. Pownel N. Town Brighton Brooklyn Palo Alto Horseneck Bennington Rye Brooklyn 9M 2.1M 1.7M 0.4M 8M 0.3M 3.7M 7.1M Borrower cnamelno Jones Smith Hayes Jackson Curry Smith Williams Adams L-17 L-23 L-15 L-14 L-93 L-11 L-17 L-16 Loan bnamelnoamt Downtown Redwood Perry Downtown Mianus R.H. Perry L-17 L-23 L-15 L-14 L-93 L-11 L-16 1000 2000 1500 500 900 1300
24
CMSC424, Spring 2005 lnoamt L-17 L-23 L-15 L-14 L-93 L-11 L-16 1000 2000 1500 500 900 1300 Example Query in RA 1. Find the base data we need Temp 1 lno,amt (loan) 2. Make a copy of (1) Temp 2 ρ Temp2 (lno2,amt2) (Temp 1 ) lno2amt2 L-17 L-23 L-15 L-14 L-93 L-11 L-16 1000 2000 1500 500 900 1300
25
CMSC424, Spring 2005 lnoamtlno2amt2 L-17 … L-17 L-23 … L-23 … 1000 … 1000 2000 … 2000 … L-17 L-23 … L-16 L-17 L-23 … L-16 … 1000 2000 … 1300 1000 2000 … 1300 … 3. Take the cartesian product of 1 and 2 Temp 3 Temp 1 Temp 2 Example Query in RA
26
CMSC424, Spring 2005 lno ( σ amt > amt2 ( lno,amt (loan) (ρ Temp2 (lno2,amt2) ( lno,amt (loan))))) 4. Select non-minimal loans Temp 4 σ amt > amt2 (Temp 3 ) 5. Project on lno Result lno (Temp 4 ) Example Query in RA … or, if you prefer…
27
CMSC424, Spring 2005 What we learned so far… Relational Algebra Operators 1.Select 2.Project 3.Set Union 4.Set Difference 5.Cartesian Product 6.Rename These are called fundamental operations
28
CMSC424, Spring 2005 Formal Definition Basic expression A relation in the database A constant relation e.g. {(A-101, Downtown, 500), (A-215, Mianus, 700)…} Let E 1 and E 2 be two relational-algebra expressions, then the following are also: 1.σ P (E 1 ), where P is a predicate on attributes in E 1 2.p S (E 1 ) where S is a list containing some attributes in E 1 3.E 1 E 2, 4.E 1 – E 2 5.E 1 E 2 6.ρ x (E 1 ), where x is the new name for the result of E 1
29
CMSC424, Spring 2005 Relational Algebra Redundant Operators 4. Update ( ) (we’ve already been using) 2. Division ( ) 1.Natural Join ( ) 3. Outer Joins ( ) Redundant: Above can be expressed in terms of minimal RA e.g. depositor borrower = π …(σ…(depositor ρ…(borrower))) Added as convenience
30
CMSC424, Spring 2005 Natural Join Idea: combines ρ, , σ ABCD 12231223 αααβαααβ +--++--+ 10 20 10 EBD ‘a’ ‘b’ ‘c’ ααββααββ 10 20 10 r s ABCDE 1223312233 αααββαααββ +--+++--++ 20 10 ‘a’ ‘b’ ‘c’ = Relation 1 Relation 2 Notation: π cname,acct_no,lno (σ cname=cname2 (depositor ρ t(cname2,lno) (borrower))) ≡ depositor borrower
31
CMSC424, Spring 2005 Division AB αααβγγγγδδαααβγγγγδδ 12311346121231134612 B 1212 r s A αδαδ = Query: Find values for A in r which have corresponding B values for all B values in s Relation 1 Relation 2 Notation: Idea: expresses “for all” queries
32
CMSC424, Spring 2005 Division AB αααβγγγγδδαααβγγγγδδ 12311346121231134612 B 1212 r s A αδαδ = 17 3 = 5 The largest value of i such that: i 3 ≤ 17 t Relational Division The largest value of t such that: ( t s r ) Another way to look at it: and
33
CMSC424, Spring 2005 ABCDE αααββγγγαααββγγγ aaaaaaaaaaaaaaaa αγγγγγγβαγγγγγγβ aabababbaabababb 1111311111113111 DE abab 1111 r s ABC αγαγ aaaa γγγγ = t ? Division A More Complex Example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.