Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC424: Database Design Instructor: Amol Deshpande

Similar presentations


Presentation on theme: "CMSC424: Database Design Instructor: Amol Deshpande"— Presentation transcript:

1 CMSC424: Database Design Instructor: Amol Deshpande amol@cs.umd.edu

2 Stuff SQL Assignment Accounts Questions on E/R Model ?

3 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

4 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

5 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

6 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

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

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

9 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

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

11 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

12 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

13 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

14 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

15 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

16 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

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

18 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

19 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 … 4-1234 4-5678 …

20 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

21 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

22 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

23 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

24 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

25 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

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

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

28 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

29 Schema Diagram for the Banking Enterprise

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

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

32 Account bnameacct_nobalance Downtown Mianus Perry R.H. A-101 A-215 A-102 A-305 500 700 400 350 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-16 1000 2000 1500 500 900 1300

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

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

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


Download ppt "CMSC424: Database Design Instructor: Amol Deshpande"

Similar presentations


Ads by Google