Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process...

36
Unit 4 Object Relational Modeling

Transcript of Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process...

Page 1: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Unit 4

Object Relational Modeling

Page 2: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Key ConceptsObject-Relational Modeling outcomes and

processRelational data modelNormalizationAnomaliesFunctional dependencyReferential integrityObject-oriented extensions to relational

modelingTranslating from a CDM to an ORM.

Page 3: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Object-relational data modeling converts a conceptual data model to a logical data model based on relational and object-oriented technology.

Page 4: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Purposes of Object-Relational ModelingProduce database structures

Create entity classesEnhance and finalize the attributes in the data model

Page 5: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

What Is a Relational Data Model?Based on the concept of relations

(tables of data)Relationships established by matching

primary and foreign keysRelational DBMSs (RDBMS) are the most

commonly used in industry today.Many DBMS vendors have begun adding

object-oriented features, creating an object-relational model.

Page 6: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

What Is a Relation?A named, two-dimensional table with the

following properties:All cells contain are atomic (single-value)

data.Entries in columns are from the same set of

values (domain range).Each row is unique (i.e. has a nonempty

primary key).Column order does not matter.Row order does not matter.

This is called First Normal Form (1NF)

Page 7: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Homework Review Question #2 page 234

Give one example of each of the three normal forms (other than those included in this chapter)

Give an example of 1NF now

Page 8: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Example relation: note uniqueness of rows guaranteed by Emp_ID. Primary keys are underlined

Page 9: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

What Is Normalization?The process of converting complex data

structures into well-structured relationsWell-structured relation

Contains a minimum amount of redundancies Allows rows to be inserted, modified, and

deleted without introducing errors or inconsistencies

Page 10: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Consequences of Relations that Are Not Well StructuredInsertion anomaly – adding new rows

forces user to create duplicate dataUpdate anomaly – changes in one row

force changes in other rows because of duplication

Deletion anomaly – deleting rows may cause a loss of data that would be needed for other future rows

Data integrity requires well-structured relations.

Page 11: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

What Is a Functional Dependency?The functional dependency of attribute

B on attribute A is represented by an arrow A B, and implies that every valid value of attribute A uniquely determines the value of attribute B.

Determinant – the attribute on the left side of the arrow

All primary keys are determinants

Page 12: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

The Normal Forms1st NF

All relations are in 1NFRelations do not include multivalued attributes

2nd NF Relations with no partial-key functional

dependencies3rd NF

Relations with no transitive functional dependenciesHigher

Normal forms: 4th, Boyce Codd, 5th – mainly theoretical, not needed for most OOSAD problems

Main goal is to achieve 3NF for all relations.

Page 13: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Second Normal Form (2NF)1NF, plus no partial key functional

dependenciesIf the primary key is a composite key

(composed of more than one attribute) it is possible for an attribute to be functionally dependent on only part of the key

Avoid partial dependencies for 2NF

Page 14: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Homework Review Question #2 page 234

Give one example of each of the three normal forms (other than those included in this chapter)

Give an example of 2NF now

Page 15: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

This table has a composite key (Emp_ID and Course)

Functional dependencies:Emp_ID Name, Dept, SalaryEmp_ID, Course Date_Completed

Name, Dept, and Salary all have partial key dependencies, causing duplication of data.

Page 16: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Solution:

Break the relation into two separate relations.

1:N relationship linked by Emp_ID

No partial key dependencies

Well structured

Page 17: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Third Normal Form (3NF)2NF, plus no transitive functional

dependenciesGiven three attributes in a relation A, B, C

If A B and B C, this forms a transitive functional dependency

Avoid transitive dependencies for 3NF

Page 18: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Here, Customer_ID Salesperson, and Salesperson Region, cause a transitive dependency

Page 19: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Break the relation into two separate relations.

1:N relationship linked by SalesPerson

No transitive dependencies

Well structured

Solution:

Page 20: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Homework Review Question #2 page 234

Give one example of each of the three normal forms (other than those included in this chapter)

Give an example of 3NF now

Page 21: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Primary and Foreign KeysPrimary key – one or more attributes that

together form a unique identifier for rows in the relation

Foreign key – an attribute that appears as a non-primary key attribute or as part of a primary key in one relation and as a primary key attribute in another relation

Relationship – rows in one relation are matched with related rows in another relation through foreign keys

Page 22: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Referential IntegrityA rule that states that any foreign key

value (on the relation of the many side) MUST match a primary key value in the relation of the one side

No foreign key can contain a value that is not present in a primary key in the other relation

Page 23: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Homework Review Question #4 page 234

If referential integrity is violated, what problems will result?

Page 24: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Object-Oriented Extensions to Relational Modeling

GeneralizationMultivalued attributes (OK to violate atomicity requirement of 1NF)

AggregationObject identifiersPointersBehaviorsRicher set of data types

Object-relational Data Model

Page 25: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Translating Conceptual Data Model to Object-Relational Model

Page 26: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Relational approach, forces atomic attributes

Object-relational approach, with multivalued attribute

Comparison of techniques for translating multivalued attributes

STUDENT (StudentID, Name, Address)STUDENT_PHONES(StudentID, PhoneNumber)

STUDENT (StudentID, Name, Address, {PhoneNumbers})

Page 27: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

When constructing 1:N relationships, the foreign key is added as an attribute to the relation on the N side.

Page 28: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Associative class and M:N relationship

Page 29: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Homework Review Question #8 page 234

How are relationships between classes represented in relational notation?

Homework Review Question #9 page 234

How is a foreign key represented in relational notation?

Page 30: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Associative ClassesAssociative class is translated into a

relation whose primary key is a composite of the primary keys of the other two classes.

M:N relationship between Work and Skill is implemented as an extra relation UseSkills, whose primary key is composed of the primary keys of Work and Skill.

Page 31: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Unary 1:N relationship is represented by a foreign key managerID, which matches with the empId primary key of the same relation.

Page 32: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Unary M:N relationship is represented by a separate relation Prerequisite, whose primary key is composed of two primary key values from the Course relation.

Page 33: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Other RelationshipsAggregation and Composition – use

same rules that apply to association. Composition is 1:N, aggregation is M:N

Generalization – no uniform standard exists. Can make use of stereotypes to annotate generalizations

Page 34: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Merging Object Relationships

Similar or redundant object relations can be merged into one

Example:

merge

Page 35: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

Problems with MergingSynonyms – two relations with different

attribute names referring to same meaningSolution – decide on a single standard name

Homonyms – two relations with same attribute names referring to different meaningsSolution – create new attribute for second meaning

Dependencies between non-key attributes after mergeSolution – normalize the merged relation

Page 36: Unit 4 Object Relational Modeling. Key Concepts Object-Relational Modeling outcomes and process Relational data model Normalization Anomalies Functional.

PVF WebStore Conceptual Data ModelUse Figure 7.31 on page 208 of Object-

Oriented Systems Analysis and Design