CMSC424: Database Design Instructor: Amol Deshpande

Slides:



Advertisements
Similar presentations
Relational Database Design UNIT II 1. 2 Advantages of Using Database Systems Centralized control of a firm’s data Redundancy can be reduced (avoid keeping.
Advertisements

Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
IELM 511: Information System design Introduction Part I. ISD for well structured data – relational and other DBMS Part II. ISD for systems with non-uniformly.
CMSC424: Database Design Instructor: Amol Deshpande
CMSC424, Spring 2005 CMSC424: Database Design Instructor: Amol Deshpande
CMSC424, Spring 2005 CMSC424: Database Design Lecture 5.
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.
©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, Spring 2005 CMSC424: Database Design Lecture 4.
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.
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.
CMSC424: Database Design Instructor: Amol Deshpande
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.
Compe 301 ER - Model. Today DBMS Overview Data Modeling Going from conceptual requirements of a application to a concrete data model E/R Model.
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.
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
PMIT-6102 Advanced Database Systems By- Jesmin Akhter Assistant Professor, IIT, Jahangirnagar University.
Relational Model: Examples. Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account.
Entity-Relationship Model
CAS CS 460/660 Entity/Relationship Model
Shahriar Pirnia Database پايگاه داده ها Shahriar Pirnia
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.
1 Session 3 Welcome: To session 3- the first learning sequence “ Introduction to Relational Model“ Recap : In the previous learning sequences, we discussed.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 2: Relational.
1 The Relational Model By Elena Ciriani CS157A February 19, 2004 Professor Lee.
Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  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.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
CMSC424: Database Design Instructor: Amol Deshpande
3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Tuple Relational Calculus Domain Relational.
IS 230Lecture 4Slide 1 Entity Relationship to Relational Model Mapping Lecture 5.
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.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts Chapter 2: Entity-Relationship Model Entity Sets Relationship Sets Design Issues Mapping.
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.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Chapter 3: Relational Model Structure of Relational Databases Relational Algebra Extended.
International Computer Institute, Izmir, Turkey E-R Model Asst.Prof.Dr.İlker Kocabaş UBİ502 at
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.
Chapter 3: Relational Model  Structure of Relational Databases  Normal forms (chap. 7)  Reduction of an E-R Schema to Relational (Sect. 2.9)  Relational.
1 Session 3 Welcome: To session 3-the fourth learning sequence “Relational algebra “ Recap : In the previous learning sequences, we discussed the four.
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.
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan Chapter 2: Relational Model.
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)
הקדמה היתרון במודל הטבלאי: המודל כקבוצה של יחסים (טבלאות)
Relational Model.
Chapter 2: Relational Model
Relational Model B.Ramamurthy 5/28/2019 B.Ramamurthy.
Syllabus Study Introduction to database Relational algebra
Presentation transcript:

CMSC424: Database Design Instructor: Amol Deshpande

Stuff SQL Assignment Accounts Questions on E/R Model ?

Today Recap: Relational Model E/R Model to an Relational Model Remember: We still use E/R models for conceptual modeling of the database Relational Algebra Operating on the relations

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

Review: Terms and Definitions 1. Tables = Relations 2. Columns = Attributes 3. Rows = Tuples 4. Relation Schema (or Schema) 1. A list of attributes and their domains 2. We will require the domains to be atomic 3. E.g. account(account-number, branch-name, balance) 5. Relation Instance 1. A particular instantiation of a relation with actual values 2. Will change with time

So… That’s the basic relational model That’s it ? What about the constraints ? How do we represent one-to-one vs many-to-one relationships ? Many of those constraints get embedded in the schema Especially relationship cardinality constraints Others are explicitly represented using other constructs

E/R Diagrams  Relations Convert entity sets into a relational schema with the same set of attributes Customer cname ccity cstreet Customer_Schema(cname, ccity, cstreet) Branch bname bcity assets Branch_Schema(bname, bcity, assets)

E/R Diagrams  Relations Convert relationship sets also into a relational schema Remember: A relationship is completely described by primary keys of associate entities and its own attributes Depositor_Schema(cname, acct-no, access-date) Account Customer Depositor acct-no balance cname ccity cstreet access-date Well… Not quite. We can do better. It depends on the relationship cardinality Customer_Schema(cname, ccity, cstreet) Account_Schema(acct-no, balance)

E/R Diagrams  Relations Say One-to-Many Relationship from Customer to Account  Many accounts per customer Account Customer Depositor acct-no balance cname ccity cstreet access-date Customer_Schema(cname, ccity, cstreet) Account_Schema(acct-no, balance, cname, access-date) Exactly same information, fewer tables

E/R Diagrams  Relations E/RRelational Schema Entity Sets E = (a 1, …, a n ) E1E1 … a1a1 anan

E/R Diagrams  Relations E/RRelational Schema Entity Sets E = (a 1, …, a n ) Relationship Sets R = (a 1, b 1, c 1, …, c n ) a 1 : E 1 ’s key b 1 : E 2 ’s key c 1, …, c k : attributes of R Not the whole story for Relationship Sets … E1E1 … a1a1 anan E1E1 … … … R E2E2 a1a1 anan c1c1 ckck b1b1 bmbm

CMSC424, Spring 2005 E/R Diagrams  Relations Relationship CardinalityRelational Schema n:m E 1 = (a 1, …, a n ) E 2 = (b 1, …, b m ) R = (a 1, b 1, c 1, …, c n ) R E1E1 … … … R E2E2 a1a1 anan c1c1 ckck b1b1 bmbm

CMSC424, Spring 2005 E/R Diagrams  Relations Relationship CardinalityRelational Schema n:m E 1 = (a 1, …, a n ) E 2 = (b 1, …, b m ) R = (a 1, b 1, c 1, …, c n ) n:1E 1 = (a 1, …, a n, b 1, c 1, …, c n ) E 2 = (b 1, …, b m ) R R E1E1 … … … R E2E2 a1a1 anan c1c1 ckck b1b1 bmbm

CMSC424, Spring 2005 E/R Diagrams  Relations Relationship CardinalityRelational Schema n:m E 1 = (a 1, …, a n ) E 2 = (b 1, …, b m ) R = (a 1, b 1, c 1, …, c n ) n:1E 1 = (a 1, …, a n, b 1, c 1, …, c n ) E 2 = (b 1, …, b m ) 1:nE 1 = (a 1, …, a n ) E 2 = (b 1, …, b m,, a 1, c 1, …, c n ) R R R E1E1 … … … R E2E2 a1a1 anan c1c1 ckck b1b1 bmbm

CMSC424, Spring 2005 E/R Diagrams  Relations Relationship CardinalityRelational Schema n:m E 1 = (a 1, …, a n ) E 2 = (b 1, …, b m ) R = (a 1, b 1, c 1, …, c n ) n:1E 1 = (a 1, …, a n, b 1, c 1, …, c n ) E 2 = (b 1, …, b m ) 1:nE 1 = (a 1, …, a n ) E 2 = (b 1, …, b m,, a 1, c 1, …, c n ) 1:1 Treat as n:1 or 1:n R R R R E1E1 … … … R E2E2 a1a1 anan c1c1 ckck b1b1 bmbm

Translating E/R Diagrams to Relations Acct-Branch AccountBranch Borrower CustomerLoan Depositor Loan-Branch Q. How many tables does this get translated into? A. 6 (account, branch, customer, loan, depositor, borrower) acct_no balance bname bcity assets cname ccity cstreet lno amt

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

E/R Diagrams & Relations E/RRelational Schema Weak Entity Sets E 1 = (a 1, …, a n ) E 2 = (a 1, b 1, …, b m ) E1E1 … … IR E2E2 a1a1 anan b1b1 bmbm

E/R Diagrams & Relations E/RRelational Schema Multivalued Attributes Emp = (ssn, name) Emp-Phones = (ssn, phone) Emp ssnname 001 … Smith … Emp-Phones Employee ssn name phone ssnphone 001 … …

E/R Diagrams & Relations E/RRelational Schema Subclasses Method 1: E = (a 1, …, a n ) E 1 = (a 1, b 1, …, b m ) E 2 = (a 1, c 1, …, c k ) E E2E2 ISA E1E1 … … b1b1 bmbm c1c1 ckck … a1a1 anan

E/R Diagrams & Relations E/RRelational Schema Subclasses Method 1: E = (a 1, …, a n ) E 1 = (a 1, b 1, …, b m ) E 2 = (a 1, c 1, …, c k ) Method 2: E 1 = (a 1, …, a n, b 1, …, b m ) E 2 = (a 1, …, a n, c 1, …, c k ) E E2E2 ISA E1E1 … … b1b1 bmbm c1c1 ckck … a1a1 anan

E/R Diagrams & Relations Subclasses example: Method 1: Account = (acct_no, balance) SAccount = (acct_no, interest) CAccount = (acct_no, overdraft) Method 2: SAccount = (acct_no, balance, interest) CAccount = (acct_no, balance, overdraft) Q: When is method 2 not possible? A: When subclassing is partial

Today Recap: Relational Model E/R Model to an Relational Model Remember: We still use E/R models for conceptual modeling of the database Relational Algebra Operating on the relations

Keys and Relations Recall: Keys: Sets of attributes that allow us to identify entities Very loosely speaking, tuples === entities Just as in E/R Model: Superkeys, candidate keys, and primary keys

Keys Superkey set of attributes of table for which every row has distinct set of values Candidate key Minimal such set of attributes Primary key DB Chosen Candidate key Plays a very important role E.g. relations typically sorted by this

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

Keys In fact, keys are one of the primary ways to enforce constraints/structure Consider a one-to-many relationship e.g. Between customers and accounts The relational model will be: Customers(custid, custname,…) Accounts(accountid, custid, balance,…) Allows for multiple accounts per customer, but not multiple customers per account Not possible to store such information In other words, constraints will lead to less representation power Contrast with: Customers(custid, custname,…) Accounts(accountid, balance,…) CustomerHasAccounts(custid, accountid)

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

Schema Diagram for the Banking Enterprise

Next Query language for operating on the relations Theoretical: Relational Algebra Tuple Relational Calculus Domain Relational Practical: SQL (loosely based on TRC) Datalog

Next… Query language for operating on the relations Theoretical: Relational Algebra Tuple Relational Calculus Domain Relational Practical: SQL (loosely based on TRC) Datalog

Account bnameacct_nobalance Downtown Mianus Perry R.H. A-101 A-215 A-102 A Depositor cnameacct_no Johnson Smith Hayes Turner A-101 A-215 A-102 A-305 Customer cnamecstreetccity Jones Smith Hayes Curry Lindsay Turner Main North Main North Park Putnam Harrison Rye Harrison Rye Pittsfield Stanford Branch bnamebcityassets Downtown Redwood Perry Mianus Brooklyn Palo Alto Horseneck 9M 2.1M 1.7M 0.4M Borrower cnamelno Jones Smith Hayes Jackson L-17 L-23 L-15 L-14 Loan bnamelnoamt Downtown Redwood Perry Downtown Mianus R.H. Perry L-17 L-23 L-15 L-14 L-93 L-11 L

Example Queries Find the names of all customers who have a loan at the Perryridge branch. Find the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank.  customer_name (  branch_name = “Perryridge” (  borrower.loan_number = loan.loan_number (borrower x loan))) –  customer_name (depositor)  customer_name (  branch_name=“Perryridge ” (  borrower.loan_number = loan.loan_number (borrower x loan)))

Example Queries Find the names of all customers who have a loan at the Perryridge branch. Query 2  customer_name (  loan.loan_number = borrower.loan_number ( (  branch_name = “Perryridge ” (loan)) x borrower)) Query 1  customer_name (  branch_name = “Perryridge” (  borrower.loan_number = loan.loan_number (borrower x loan)))

Example Queries Find the largest account balance Strategy: Find those balances that are not the largest –Rename account relation as d so that we can compare each account balance with all others Use set difference to find those account balances that were not found in the earlier step. The query is:  balance (account) -  account.balance (  account.balance < d.balance (account x  d (account)))