CMSC424, Spring 2005 CMSC424: Database Design Lecture 4.

Slides:



Advertisements
Similar presentations
Chapter 3: Relational Model
Advertisements

CS 319: Theory of Databases: C4
Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
CMSC424, Spring 2005 CMSC424: Database Design Lecture 5.
Chapter 2 Relational Model (part II) Hankz Hankui Zhuo
Instructor: Amol Deshpande  Data Models ◦ Conceptual representation of the data  Data Retrieval ◦ How to ask questions of the database.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
Relational Algebra Lecture 2. Relational Model Basic Notions Fundamental Relational Algebra Operations Additional Relational Algebra Operations Extended.
CMSC424: Database Design Instructor: Amol Deshpande
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
Chapter 3: Relational Model
Review of the Entity-Relationship Model Slides courtesy of Amol Deshpande material from ch. 2 of Korth & Silberschatz Database System Concepts,
CMSC424: Database Design Instructor: Amol Deshpande
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Relational Algebra Lecture 2. Relational Model Basic Notions Fundamental Relational Algebra Operations Additional Relational Algebra Operations Extended.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Midterm 2 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Relational Algebra. Relational Query Languages n Query = “retrieval program” n Language examples: ù Theoretical : 1. Relational Algebra 2. Relational.
CMSC424, Spring 2005 CMSC424: Database Design Lecture 3.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. Relational Model Database Management Systems I Alex Coman, Winter 2006.
Relational Model. 2 Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations Extended Relational-Algebra-Operations.
Relational Algebra.
Chapters 2 & 6 The Relational Model. 2  A tabular data structure  Tables (relations) with unique names  rows (tuples/entities/records)  columns (attributes/fields)
International Computer Institute, Izmir, Turkey SQL Asst.Prof.Dr.İlker Kocabaş UBİ502 at
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational.
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com 1 Chapter 2: Relational.
Relational Algebra Instructor: Mohamed Eltabakh 1.
Practice I SQL. Step 1: Create Database Create a database named Bank in SQL Server 2005 according to the following schema diagram.
Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
ICOM 5016 – Introduction to Database Systems Lecture 5b Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
CIS552Relational Model1 Structure of Relational Database Relational Algebra Extended Relational-Algebra-Operations Modification of the Database.
3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational Calculus Domain Relational.
Midterm 2 Revision Prof. Sin-Min Lee Department of Mathematics and Computer Science San Jose State University.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional Relational-Algebra-Operations.
Chapter 2: Relational Model. 2.2Unite International CollegeDatabase Management Systems Chapter 2: Relational Model Structure of Relational Databases Fundamental.
Chapter 2: Relational Model II Relational Algebra basics Relational Algebra basics.
Computing & Information Sciences Kansas State University Friday, 26 Jan 2008CIS 560: Database System Concepts Lecture 2 of 42 Friday, 29 August 2008 William.
2.1 Chapter 2: Relational Model. 2.2 Chapter 2: Relational Model Structure of Relational Databases Fundamental Relational-Algebra-Operations Additional.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 2 RELATIONAL MODEL 2.1.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
Midterm 2 Revision Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Database System Concepts, 5 th Ed. Bin Mu at Tongji University Chapter 2: Relational Model.
Computing & Information Sciences Kansas State University Wednesday, 03 Sep 2008CIS 560: Database System Concepts Lecture 3 of 42 Wednesday, 03 September.
Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
International Computer Institute, Izmir, Turkey Relational Model Asst.Prof.Dr.İlker Kocabaş UBİ502 at
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
Database Systems. DataBase System Haichang Gao, Software School, Xidian University 2 Major Content & Grade  Introduction*  The Relational Model** 
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan Chapter 2: Relational Model.
International Computer Institute, Izmir, Turkey Relational Model Asst.Prof.Dr.İlker Kocabaş UBİ502 at
Database Design and the
Midlands State University Topic: Relational DB Model
Relational Algebra.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
הקדמה היתרון במודל הטבלאי: המודל כקבוצה של יחסים (טבלאות)
Chapter 2: Relational Model
Introduction to Database
Relational Model.
Relational Model B.Ramamurthy 5/28/2019 B.Ramamurthy.
Presentation transcript:

CMSC424, Spring 2005 CMSC424: Database Design Lecture 4

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

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

CMSC424, Spring 2005 Bank Database: Schema Account bnameacct_nobalance Depositor cnameacct_no Customer cnamecstreetccity Branch bnamebcityassets Borrower cnamelno Loan bnamelnoamt

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 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

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!!

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

CMSC424, Spring 2005 Schema Diagram for the Banking Enterprise

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:

CMSC424, Spring 2005 Relational Algebra Basic Operators 1.select ( σ ) 2.project (  ) 3.union (  ) 4.set difference ( – ) 5.cartesian product (  ) 6.rename ( ρ ) Relational Operator Relation

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

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)

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:

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

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)

CMSC424, Spring 2005 Set Difference ( – ) bnamelnoamount Downtown Redwood Perry Downtown Perry L-17 L-23 L-15 L-14 L 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

CMSC424, Spring 2005 Set Difference ( – ) bnamelnoamount Downtown Redwood Perry Downtown Perry L-17 L-23 L-15 L-14 L 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 – = bname Downtown Perry

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

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

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 …

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 =

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:

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 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

CMSC424, Spring 2005 lnoamt L-17 L-23 L-15 L-14 L-93 L-11 L 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

CMSC424, Spring 2005 lnoamtlno2amt2 L-17 … L-17 L-23 … L-23 … 1000 … … 2000 … L-17 L-23 … L-16 L-17 L-23 … L-16 … … … 1300 … 3. Take the cartesian product of 1 and 2 Temp 3  Temp 1  Temp 2 Example Query in RA

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…

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

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

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

CMSC424, Spring 2005 Natural Join Idea: combines ρ, , σ ABCD αααβαααβ EBD ‘a’ ‘b’ ‘c’ ααββααββ r s ABCDE αααββαααββ ‘a’ ‘b’ ‘c’ = Relation 1 Relation 2 Notation: π cname,acct_no,lno (σ cname=cname2 (depositor  ρ t(cname2,lno) (borrower))) ≡ depositor borrower

CMSC424, Spring 2005 Division AB αααβγγγγδδαααβγγγγδδ 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

CMSC424, Spring 2005 Division AB αααβγγγγδδαααβγγγγδδ 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 

CMSC424, Spring 2005 ABCDE αααββγγγαααββγγγ aaaaaaaaaaaaaaaa αγγγγγγβαγγγγγγβ aabababbaabababb DE abab 1111 r s ABC αγαγ aaaa γγγγ = t ? Division A More Complex Example