Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC424, Spring 2005 CMSC424: Database Design Lecture 3.

Similar presentations


Presentation on theme: "CMSC424, Spring 2005 CMSC424: Database Design Lecture 3."— Presentation transcript:

1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 3

2 CMSC424, Spring 2005 2 Database Design Steps Three Levels of Modeling info Conceptual Data Model Logical Data Model Physical Data Model Conceptual DB design Logical DB design Physical DB design Entity-relationship Model Typically used for conceptual database design Relational Model Typically used for logical database design

3 CMSC424, Spring 2005 Review: Entity-Relationship Model Basics E1E1 Entity set R Relationship set Attribute (primary key if underlined) E1E1 … … … R E2E2 a1a1 anan c1c1 ckck b1b1 bmbm a

4 CMSC424, Spring 2005 One-to-One One-to-Many One customer can be associated with many accounts. customer has account customer has account Relationship Cardinalities

5 CMSC424, Spring 2005 One-to-One One-to-Many Many-to-One Many customers can be associated with one account. customer has account customer has account customer has account Relationship Cardinalities

6 CMSC424, Spring 2005 One-to-One One-to-Many Many-to-One Many-to-Many customer has account customer has account customer has account customer has account Relationship Cardinalities

7 CMSC424, Spring 2005 Relationship Set Keys Is {cust-id, account-number} a candidate key ? Depends customer has cust-id account number access-date If one-to-many relationship (as shown), {account-number} is a candidate key A given customer can have many accounts, but at most one account holder per account allowed So account number sufficient to uniquely identify a relationship

8 CMSC424, Spring 2005 Thoughts… Nothing about actual data How is it stored ? No talk about the query languages How do we access the data ? Semantic vs Syntactic Data Models Remember: E/R Model is used for conceptual modeling Many conceptual models have the same properties They are much more about representing the knowledge than about database storage/querying

9 CMSC424, Spring 2005 Thoughts… Basic design principles Faithful Must make sense Satisfies the application requirements Models the requisite domain knowledge If not modeled, lost afterwards Avoid redundancy Potential for inconsistencies Go for simplicity Typically an iterative process that goes back and forth

10 CMSC424, Spring 2005 Relational Data Model Before = “Network Data Model” (Cobol as DDL, DML) Very contentious: Database Wars (Charlie Bachman vs. Mike Stonebraker) Introduced by Ted Codd (late 60’s – early 70’s) 1.Separation of logical, physical data models (data independence) 2.Declarative query languages 3.Formal semantics 4.Query optimization (key to commercial success) Relational data model contributes: Ingres  CA Postgres  Illustra  Informix  IBM System R  Oracle, DB2 1 st prototypes:

11 CMSC424, Spring 2005 Key Abstraction: Relation bnameacct_nobalance Downtown Brighton A-101 A-201 A-217 500 900 500 Account = Terms: Tables (aka: Relations) Why called Relations?

12 CMSC424, Spring 2005 Why Called Relations? 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

13 CMSC424, Spring 2005 Relations bnameacct_nobalance Downtown Brighton A-101 A-201 A-217 500 900 500 Account = Relational database semantics defined in terms of mathematical relations { (Downtown, A-101, 500), (Brighton, A-201, 900), (Brighton, A-217, 500) } Considered equivalent to…

14 CMSC424, Spring 2005 Relations bnameacct_nobalance Downtown Brighton A-101 A-201 A-217 500 900 500 Rows (aka: tuples) Account = Terms: Columns (aka: attributes) { (Downtown, A-101, 500), (Brighton, A-201, 900), (Brighton, A-217, 500) } Considered equivalent to… Tables (aka: Relations) Schema (e.g.: Acct_Schema = (bname, acct_no, balance))

15 CMSC424, Spring 2005 Definitions 1.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) Relation Instance A particular instantiation of a relation with actual values Will change with time bnameacct_nobalance Downtown Brighton A-101 A-201 A-217 500 900 500 Programming language equivalent: A variable (e.g. x) Programming language equivalent: Value of a variable

16 CMSC424, Spring 2005 Rest of the Class Converting from an E/R diagram to a relational schema –Remember: We still use E/R models for conceptual modeling of the database Relational Algebra –Data retrieval language

17 CMSC424, Spring 2005 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)

18 CMSC424, Spring 2005 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)

19 CMSC424, Spring 2005 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

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

21 CMSC424, Spring 2005 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

22 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

23 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

24 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

25 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

26 CMSC424, Spring 2005 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

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

28 CMSC424, Spring 2005 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

29 CMSC424, Spring 2005 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

30 CMSC424, Spring 2005 E/R Diagrams & Relations E/RRelational Schema Multivalued Attributes Emp = (ssn, name) Emp-Phones = (ssn, phone) Emp ssnname 001 … Smith … Emp-Phones ssnphone 001 … 4-1234 4-5678 … Employee ssn name phone

31 CMSC424, Spring 2005 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

32 CMSC424, Spring 2005 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

33 CMSC424, Spring 2005 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

34 CMSC424, Spring 2005 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!!


Download ppt "CMSC424, Spring 2005 CMSC424: Database Design Lecture 3."

Similar presentations


Ads by Google