Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Models and Relational Databases Chapter 2. Learning Objectives Identify primary and foreign keys for each entity and relevant relationships in the.

Similar presentations


Presentation on theme: "Data Models and Relational Databases Chapter 2. Learning Objectives Identify primary and foreign keys for each entity and relevant relationships in the."— Presentation transcript:

1 Data Models and Relational Databases Chapter 2

2 Learning Objectives Identify primary and foreign keys for each entity and relevant relationships in the data model, Create tables that are properly linked with foreign keys or through relationship table(s), Examine a table design for any anomalies, and Normalize a table to the third normal form.

3 The Database Development Process Begins with enterprise modeling  Setting the range and general contents of organizational databases Follows with conceptual data modeling  Analyzing overall entities’ requirements based on transaction cycles Involves with logical database design  Transforming the conceptual data model into a logical data model using relational data models

4 Enterprise Modeling

5 Conceptual Data Modeling

6 Complete REA Diagram for Cherokee’s Revenue Cycle Resource EventsAgents

7 Structure of a Relational Database The primary construct is called a relation or a table.  Constructed with rows and columns much like a spreadsheet.  Each table represents either an entity or a relationship between entities.  Tables must be properly linked to make a relational database. The columns in a table are called fields.  Fields represent the attributes or characteristics of the entity or relationship. The rows in a table are called records or tuples.  Records represent all the specific data values that are associated with one instance.

8 Primary and Foreign Keys Primary key The attributes (column) or combination of attributes (multiple columns) that uniquely identifies a specific row in a table. Foreign key An attribute appearing in one table that is a primary key in another table. Foreign key is used to link tables

9 Basic Requirements of Tables The Entity Integrity Rule The Referential Integrity Rule Each attribute must be uniquely named. Values of a specific attribute must be of the same type. Each attribute (column) of a record (row) must be single- valued.

10 Steps of Implementing an REA Diagram in a Relational Database Step 1: Create a table for each entity Step 2: Create a table for each many-to-many relationship Step 3: Examine tables with a one-to-one relationship Step 4: Identify the attributes and assign the primary key for each table (note that a relationship table uses two columns as the primary key – concatenated key) Step 5: Assign non-key attributes to tables Step 6: Implement relationships using foreign keys

11 Implementing an REA Diagram in a Relational Database Implement relationships using foreign keys One-to-One (1:1): Include the primary key of the first entity as the foreign key in the second entity (Note: it does not matter which primary key becomes the foreign key). One-to-Many (1:N or N:1): Place the primary key of the entity with the maximum cardinality of N as a foreign key in the entity that has a maximum cardinality of 1

12 Revised REA Data Model

13 Database Anomalies and Normalization If a table is not well-designed, problems (anomalies) arise. Three types of anomalies:  Update anomaly causes problems when every occurrence of a data item is not updated.  Insert anomaly causes difficulties (sometimes impossibility) to insert new data into a table without violating the basic integrity rules.  Delete anomaly involves unintended loss of data that occurs when deleting a record in a table.

14 Customer IDLast Name First NamePhone No. Product IDQuantityUnit Selling Price 10001BlairDiane(408) 255-1129P-33011$25.99 10001BlairDiane(408) 255-1129P-54903$79.99 10001BlairDiane(408) 255-1129P-711211$5.99 10201SandersonTed(510) 996-7110P-54902$79.99 10201SandersonTed(510) 996-7110P-54985$35.00 10211RyanBird(562) 985-1024P-33017$25.99 10211RyanBird(562) 985-1024P-54982$35.00 10211RyanBird(562) 985-1024P-711923$15.99 Suppose Ted Sanderson changes his phone number. You need to make the change two times. If you fail to change it in both places or change it incorrectly in one place, then the records for Ted will be inconsistent. This problem is referred to as an update anomaly.

15 What if there is a new product to add but no customers have purchased it yet? Can we add the new product? Why or why not? This problem is referred to as an insert anomaly. What is the primary key for this table? Customer IDLast Name First NamePhone No. Product IDQuantity Unit Selling Price 10001BlairDiane(408) 255-1129P-33011$25.99 10001BlairDiane(408) 255-1129P-54903$79.99 10001BlairDiane(408) 255-1129P-711211$5.99 10201SandersonTed(510) 996-7110P-54902$79.99 10201SandersonTed(510) 996-7110P-54985$35.00 10211RyanBird(562) 985-1024P-33017$25.99 10211RyanBird(562) 985-1024P-54982$35.00 10211RyanBird(562) 985-1024P-711923$15.99

16 Assume that Bird has filed bankruptcy and you decide to delete her record from the table. If Bird is the only customer that purchased item P-7119, deleting Bird’s record will also delete any information pertaining to P-7119. If there are any other requests for this product in the future, the product info will not be available. This problem is referred to as a delete anomaly. Customer IDLast Name First NamePhone No. Product IDQuantity Unit Selling Price 10001BlairDiane(408) 255-1129P-33011$25.99 10001BlairDiane(408) 255-1129P-54903$79.99 10001BlairDiane(408) 255-1129P-711211$5.99 10201SandersonTed(510) 996-7110P-54902$79.99 10201SandersonTed(510) 996-7110P-54985$35.00 10211RyanBird(562) 985-1024P-33017$25.99 10211RyanBird(562) 985-1024P-54982$35.00 10211RyanBird(562) 985-1024P-711923$15.99

17 Normalization One solution to avoid anomalies is to normalize the table by separating the table into a set of smaller tables. Normalization is a formal process to determine which attributes should be grouped together in a table. It validates the logical design to avoid any violation of database requirements. Each entity is stored in a table and we use foreign keys or relationship tables to link the entities together.

18 Steps in Normalization Step 1: Remove any repeating groups (First Normal Form) Step 2: Remove any partial dependencies (Second Normal Form) Step 3: Remove any transitive dependencies (Third Normal Form)

19 Example: Partial Data in a Vendor Table Vendor#Vendor Name Vendor AddressContact Name Phone#Item#DescriptionUnit Price 2010AXA, Inc.22 Heaven Wood Drive Annie White234-3435M-45Blender$15.99 K-4iPod$65.00 2011Sys Co.2273 Morgan Street John Doe655-7512D-12LCD projector$560.00 2012MFS46 Kings Square, #5 Rachelle Brown 234-7799M-45Blender$18.00 K-4iPod$63.80 2013Lazard8 Santa AnaEd King734-0109K-8DVD player$134.00 2014XYZ Co.19 ColumbusLan Lee878-1888K-8DVD player$129.99

20 Remove repeating groups (1 st NF) Vendor#Vendor Name Vendor AddressContact Name Phone#Item#DescriptionUnit Price 2010AXA, Inc.22 Heaven Wood Drive Annie White234-3435M-45Blender$15.99 K-4iPod$65.00 2011Sys Co.2273 Morgan Street John Doe655-7512D-12LCD projector$560.00 2012MFS46 Kings Square, #5 Rachelle Brown 234-7799M-45Blender$18.00 K-4iPod$63.80 2013Lazard8 Santa AnaEd King734-0109K-8DVD player$134.00 2014XYZ Co.19 ColumbusLan Lee878-1888K-8DVD player$129.99 Vendor TableInventory Table

21 The data exists in separate tables Vendor#Vendor Name Vendor AddressContact Name Phone#Item#DescriptionUnit Price 2010AXA, Inc.22 Heaven Wood Drive Annie White234-3435M-45Blender$15.99 K-4iPod$65.00 2011Sys Co.2273 Morgan Street John Doe655-7512D-12LCD projector$560.00 2012MFS46 Kings Square, #5 Rachelle Brown 234-7799M-45Blender$18.00 K-4iPod$63.80 2013Lazard8 Santa AnaEd King734-0109K-8DVD player$134.00 2014XYZ Co.19 ColumbusLan Lee878-1888K-8DVD player$129.99 Vendor TableInventory Table

22 We must link the separate tables Using foreign keys, or Using a relationship table if it is M:N relationship using a concatenated key (two columns together as the primary key) Vendor#Item# 2010M-45 2010K-4 2011D-12 2012M-45 2012K-4 Vendor-Item Table

23 End of Chapter 2


Download ppt "Data Models and Relational Databases Chapter 2. Learning Objectives Identify primary and foreign keys for each entity and relevant relationships in the."

Similar presentations


Ads by Google