57
Chapter Chapter 5: Logical Database Design and Logical Database Design and the Relational Model the Relational Model the Relational Model the Relational Model Modern Database Management Modern Database Management 9 th th Edition Edition 9 Edition Edition Jeffrey A. Jeffrey A. Hoffer Hoffer, Mary B. Prescott, , Mary B. Prescott, Heikki Heikki Topi Topi © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice Hall Publishing as Prentice Hall 1

Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Embed Size (px)

Citation preview

Page 1: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Chapter Chapter 55::Logical Database Design and Logical Database Design and

the Relational Modelthe Relational Modelthe Relational Modelthe Relational Model

Modern Database ManagementModern Database Management99thth EditionEdition99 EditionEdition

Jeffrey A. Jeffrey A. HofferHoffer, Mary B. Prescott, , Mary B. Prescott, HeikkiHeikki TopiTopi

© © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 11

Page 2: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

ObjectivesObjectivesObjectivesObjectivesD fi iti f tD fi iti f tDefinition of termsDefinition of termsList five properties of relationsList five properties of relationsState t o p ope ties of candidate ke sState t o p ope ties of candidate ke sState two properties of candidate keysState two properties of candidate keysDefine first, second, and third normal formDefine first, second, and third normal formDescribe problems from merging relationsDescribe problems from merging relationsDescribe problems from merging relationsDescribe problems from merging relationsTransform ETransform E--R and EER diagrams to relationsR and EER diagrams to relationsCreate tables with entity and relational integrityCreate tables with entity and relational integrityCreate tables with entity and relational integrity Create tables with entity and relational integrity constraintsconstraintsUse normalization to convert anomalous tablesUse normalization to convert anomalous tablesUse normalization to convert anomalous tables Use normalization to convert anomalous tables to wellto well--structured relationsstructured relations

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 22

Page 3: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

RelationRelationDefinition: A relation is a named, twoDefinition: A relation is a named, two--dimensional table of dimensional table of data data datadataTable consists of rows (records) and columns (attribute or Table consists of rows (records) and columns (attribute or field)field)R i t f t bl t lif l tiR i t f t bl t lif l tiRequirements for a table to qualify as a relation:Requirements for a table to qualify as a relation:

It must have a unique nameIt must have a unique nameEvery attribute value must be atomic (not multivalued, not Every attribute value must be atomic (not multivalued, not y ( ,y ( ,composite)composite)Every row must be unique (can’t have two rows with exactly the Every row must be unique (can’t have two rows with exactly the same values for all their fields)same values for all their fields)Attributes (columns) in tables must have unique namesAttributes (columns) in tables must have unique namesThe order of the columns must be irrelevantThe order of the columns must be irrelevantThe order of the rows must be irrelevantThe order of the rows must be irrelevant

NOTE: all NOTE: all relationsrelations are in are in 11stst Normal formNormal formChapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 33

Page 4: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Correspondence with ECorrespondence with E--R ModelR Model

Relations (tables) correspond with entity types Relations (tables) correspond with entity types and with manyand with many toto many relationship typesmany relationship typesand with manyand with many--toto--many relationship typesmany relationship typesRows correspond with entity instances and with Rows correspond with entity instances and with manymany toto many relationship instancesmany relationship instancesmanymany--toto--many relationship instancesmany relationship instancesColumns correspond with attributesColumns correspond with attributes

NOTE: The word NOTE: The word relationrelation (in relational (in relational ((database) is NOT the same as the word database) is NOT the same as the word relationshiprelationship (in E(in E--R model)R model)

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 44

pp (( ))

Page 5: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Key FieldsKey Fields

Keys are special fields that serve two main purposes:Keys are special fields that serve two main purposes:Primary keysPrimary keys are are uniqueunique identifiers of the relation in question. identifiers of the relation in question. y yy y qq qqExamples include employee numbers, social security numbers, Examples include employee numbers, social security numbers, etc. etc. This is how we can guarantee that all rows are uniqueThis is how we can guarantee that all rows are uniqueForeign keysForeign keys are identifiers that enable aare identifiers that enable a dependentdependent relationrelationForeign keysForeign keys are identifiers that enable a are identifiers that enable a dependentdependent relation relation (on the many side of a relationship) to refer to its (on the many side of a relationship) to refer to its parentparent relation relation (on the one side of the relationship)(on the one side of the relationship)

Keys can beKeys can be simplesimple (a single field) or(a single field) or compositecomposite (more(moreKeys can be Keys can be simplesimple (a single field) or (a single field) or compositecomposite (more (more than one field)than one field)Keys usually are used as indexes to speed up the Keys usually are used as indexes to speed up the y y p py y p presponse to user queries (More on this in Chapter response to user queries (More on this in Chapter 66))

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 55

Page 6: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-3 Schema for four relations (Pine Valley Furniture Company)

Primary KeyF i KForeign Key (implements 1:N relationship between customer and order)

Combined, these are a composite primary key (uniquely identifies the order line)…individually they are foreign keys (implement M:N relationship between order and product)

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 66

Page 7: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Integrity ConstraintsIntegrity Constraints

Domain ConstraintsDomain ConstraintsAllowable values for an attribute. See Table Allowable values for an attribute. See Table 55--11

Entity IntegrityEntity IntegrityNo primary key attribute may be null AllNo primary key attribute may be null AllNo primary key attribute may be null. All No primary key attribute may be null. All primary key fields primary key fields MUSTMUST have datahave data

Action AssertionsAction AssertionsBusiness rules. Recall from ChapterBusiness rules. Recall from Chapter 44

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 77

Business rules. Recall from Chapter Business rules. Recall from Chapter 44

Page 8: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Domain definitions enforce domain integrity constraints

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 88

Domain definitions enforce domain integrity constraints

Page 9: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Integrity ConstraintsIntegrity Constraints

Referential IntegrityReferential Integrity––rule states that any foreign key value (on rule states that any foreign key value (on the relation of the many side) MUST match a primary key valuethe relation of the many side) MUST match a primary key valuethe relation of the many side) MUST match a primary key value the relation of the many side) MUST match a primary key value in the relation of the one side. (Or the foreign key can be null) in the relation of the one side. (Or the foreign key can be null)

For example: Delete RulesFor example: Delete RulesFor example: Delete RulesFor example: Delete RulesRestrictRestrict––don’t allow delete of “parent” side if related rows exist in don’t allow delete of “parent” side if related rows exist in “dependent” side“dependent” sideCascadeCascade––automatically delete “dependent” side rows that correspond automatically delete “dependent” side rows that correspond with the “parent” side row to be deletedwith the “parent” side row to be deletedSetSet--toto--NullNull––set the foreign key in the dependent side to null ifset the foreign key in the dependent side to null ifSetSet--toto--NullNull––set the foreign key in the dependent side to null if set the foreign key in the dependent side to null if deleting from the parent side deleting from the parent side not allowed for weak entitiesnot allowed for weak entities

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 99

Page 10: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-5 Referential integrity constraints (Pine Valley Furniture)

Referential integrity

t i tconstraints are drawn via arrows from dependent tofrom dependent to

parent table

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 1010

Page 11: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-6 SQL table definitions

Referential integrity

constraints are implemented with

foreign key to i kprimary key

references

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 1111

Page 12: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Transforming EER Diagrams intoTransforming EER Diagrams intoTransforming EER Diagrams into Transforming EER Diagrams into RelationsRelations

Mapping Regular Entities to Relations Mapping Regular Entities to Relations 1.1. Simple attributes: ESimple attributes: E--R attributes map R attributes map

directly onto the relationdirectly onto the relationyy2.2. Composite attributes: Use only their simple, Composite attributes: Use only their simple,

component attributescomponent attributescomponent attributes component attributes 3.3. Multivalued Attribute: Becomes a separate Multivalued Attribute: Becomes a separate

relation with a foreign key taken from therelation with a foreign key taken from therelation with a foreign key taken from the relation with a foreign key taken from the superior entitysuperior entity

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 1212

Page 13: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-8 Mapping a regular entity

(a) CUSTOMER tit t ithentity type with

simple attributesattributes

(b) CUSTOMER relation(b) CUSTOMER relation

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 1313

Page 14: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-9 Mapping a composite attribute

(a) CUSTOMER entity type with y ypcomposite attribute

(b) CUSTOMER relation with address detail

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 1414

Page 15: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-10 Mapping an entity with a multivalued attribute

(a)

M lti l d tt ib t b t l ti ith f i kMultivalued attribute becomes a separate relation with foreign key

(b)

i i i i i iChapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 1515

One–to–many relationship between original entity and new relation

Page 16: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Transforming EER Diagrams intoTransforming EER Diagrams intoTransforming EER Diagrams into Transforming EER Diagrams into Relations (cont.)Relations (cont.)( )( )

Mapping Weak EntitiesMapping Weak Entitiespp gpp gBecomes a separate relation with a Becomes a separate relation with a foreign key taken from the superiorforeign key taken from the superiorforeign key taken from the superior foreign key taken from the superior entityentityPrimary key composed of:Primary key composed of:

Partial identifier of weak entityPartial identifier of weak entityPartial identifier of weak entityPartial identifier of weak entityPrimary key of identifying relation (strong Primary key of identifying relation (strong entity)entity)

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 1616

entity)entity)

Page 17: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-11 Example of mapping a weak entity

a) Weak entity DEPENDENT

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 1717

Page 18: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-11 Example of mapping a weak entity (cont.)

b) Relations resulting from weak entity

NOTE: the domain constraint for the foreign key should NOT ll ll l ifNOT allow null value if DEPENDENT is a weak entity

Foreign key

Composite primary key

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 1818

Page 19: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Transforming EER Diagrams intoTransforming EER Diagrams intoTransforming EER Diagrams into Transforming EER Diagrams into Relations (cont.)Relations (cont.)( )( )

Mapping Binary RelationshipsMapping Binary Relationshipspp g y ppp g y pOneOne--toto--ManyMany––Primary key on the one side Primary key on the one side becomes a foreign key on the many sidebecomes a foreign key on the many sidebecomes a foreign key on the many sidebecomes a foreign key on the many sideManyMany--toto--ManyMany––Create a Create a new relationnew relation with with the primary keys of the two entities as itsthe primary keys of the two entities as itsthe primary keys of the two entities as its the primary keys of the two entities as its primary keyprimary keyOneOne toto OneOne Primary key on the mandatoryPrimary key on the mandatoryOneOne--toto--OneOne––Primary key on the mandatory Primary key on the mandatory side becomes a foreign key on the optional side becomes a foreign key on the optional sideside

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 1919

sideside

Page 20: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-12 Example of mapping a 1:M relationship

a) Relationship between customers and orders

Note the mandatory oney

b) Mapping the relationship

Again, no null value in the f i k hi i bforeign key…this is because of the mandatory minimum cardinality

Foreign key

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 2020

Foreign key

Page 21: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-13 Example of mapping an M:N relationship

a) Completes relationship (M:N)

The Completes relationship will need to become a separate relation

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 2121

Page 22: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-13 Example of mapping an M:N relationship (cont.)

b) Three resulting relations

Composite primary key

New Foreign keyF i k intersection

relationForeign key

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 2222

Page 23: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-14 Example of mapping a binary 1:1 relationship

a) In_charge relationship (1:1)

Often in 1:1 relationships one direction is optionalOften in 1:1 relationships, one direction is optional

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 2323

Page 24: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-14 Example of mapping a binary 1:1 relationship (cont.)

b) Resulting relations

Foreign key goes in the relation on the optional sideForeign key goes in the relation on the optional side,matching the primary key on the mandatory side

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 2424

Page 25: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Transforming EER Diagrams intoTransforming EER Diagrams intoTransforming EER Diagrams into Transforming EER Diagrams into Relations (cont.)Relations (cont.)( )( )

Mapping Associative EntitiesMapping Associative EntitiesIdentifier Not Assigned Identifier Not Assigned

Default primary key for the associationDefault primary key for the associationDefault primary key for the association Default primary key for the association relation is composed of the primary keys of relation is composed of the primary keys of the two entities (as in M:N relationship)the two entities (as in M:N relationship)( p)( p)

Identifier Assigned Identifier Assigned It is natural and familiar to endIt is natural and familiar to end--usersusersIt is natural and familiar to endIt is natural and familiar to end usersusersDefault identifier may not be uniqueDefault identifier may not be unique

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 2525

Page 26: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-15 Example of mapping an associative entity

a) An associative entity

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 2626

Page 27: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-15 Example of mapping an associative entity (cont.)

b) Three resulting relations

Composite primary key formed from the two foreign keys

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 2727

Page 28: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-16 Example of mapping an associative entity withan identifieran identifier

a) SHIPMENT associative entity

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 2828

Page 29: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-16 Example of mapping an associative entity withan identifier (cont )an identifier (cont.)

b) Three resulting relations

Primary key differs from foreign keysPrimary key differs from foreign keys

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 2929

Page 30: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Transforming EER Diagrams intoTransforming EER Diagrams intoTransforming EER Diagrams into Transforming EER Diagrams into Relations (cont.)Relations (cont.)( )( )

Mapping Unary RelationshipsMapping Unary Relationshipspp g y ppp g y pOneOne--toto--ManyMany––Recursive foreign key in the Recursive foreign key in the same relationsame relationsame relationsame relationManyMany--toto--ManyMany––Two relations:Two relations:

One for the entity typeOne for the entity typeOne for the entity typeOne for the entity typeOne for an associative relation in which the One for an associative relation in which the

i k h ib b h ki k h ib b h kprimary key has two attributes, both taken primary key has two attributes, both taken from the primary key of the entityfrom the primary key of the entity

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 3030

Page 31: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-17 Mapping a unary 1:N relationship

(a) EMPLOYEE entity with unary relationship

(b) EMPLOYEE relation withrelation with recursive foreign key

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 3131

Page 32: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-18 Mapping a unary M:N relationship

(a) Bill-of-materials relationships (M:N)

(b) ITEM and COMPONENT relationsrelations

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 3232

Page 33: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Transforming EER Diagrams intoTransforming EER Diagrams intoTransforming EER Diagrams into Transforming EER Diagrams into Relations (cont.)Relations (cont.)( )( )

Mapping Ternary (and nMapping Ternary (and n--ary) ary) pp g y (pp g y ( y)y)RelationshipsRelationships

One relation for each entity and One relation for each entity and one for the associative entityone for the associative entityone for the associative entityone for the associative entityAssociative entity has foreign keys Associative entity has foreign keys to each entity in the relationshipto each entity in the relationship

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 3333

Page 34: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-19 Mapping a ternary relationship

a) PATIENT TREATMENT Ternary relationship witha) PATIENT TREATMENT Ternary relationship with associative entity

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 3434

Page 35: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-19 Mapping a ternary relationship (cont.)

b) Mapping the ternary relationship PATIENT TREATMENT

Remember that the

This is why t t t d t

But this makes a very

It would be better to create athat the

primary key MUST be

i

treatment date and time are

included in the

very cumbersome

key…

better to create a surrogate key

like Treatment#

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 3535

unique composite primary key

Page 36: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Transforming EER Diagrams Transforming EER Diagrams into Relations (cont.)into Relations (cont.)

Mapping Supertype/Subtype RelationshipsMapping Supertype/Subtype RelationshipsOne relation for supertype and for each subtypeOne relation for supertype and for each subtypep yp ypp yp ypSupertype attributes (including identifier and Supertype attributes (including identifier and subtype discriminator) go into supertype subtype discriminator) go into supertype

llrelationrelationSubtype attributes go into each subtype; Subtype attributes go into each subtype; primary key of supertype relation also becomesprimary key of supertype relation also becomesprimary key of supertype relation also becomes primary key of supertype relation also becomes primary key of subtype relationprimary key of subtype relation11::11 relationship established between supertyperelationship established between supertype11::1 1 relationship established between supertype relationship established between supertype and each subtype, with supertype as primary and each subtype, with supertype as primary tabletable

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 3636

Page 37: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-20 Supertype/subtype relationships

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 3737

Page 38: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-21 M i S t / bt l ti hi t l tiMapping Supertype/subtype relationships to relations

These are implemented as one-to-one relationships

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 3838

Page 39: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Data NormalizationData NormalizationPrimarily a tool to validate and improve Primarily a tool to validate and improve a logical design so that it satisfiesa logical design so that it satisfiesa logical design so that it satisfies a logical design so that it satisfies certain constraints that certain constraints that avoid avoid

d li ti fd li ti funnecessary duplication of unnecessary duplication of datadataThe process of decomposing relations The process of decomposing relations with anomalies to produce smallerwith anomalies to produce smallerwith anomalies to produce smaller, with anomalies to produce smaller, wellwell--structuredstructured relationsrelations

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 3939

Page 40: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

WellWell Structured RelationsStructured RelationsWellWell--Structured RelationsStructured Relations

A relation that contains minimal data redundancy and A relation that contains minimal data redundancy and allows users to insert, delete, and update rows allows users to insert, delete, and update rows without causing data inconsistencieswithout causing data inconsistencieswithout causing data inconsistencieswithout causing data inconsistenciesGoal is to avoid anomaliesGoal is to avoid anomalies

Insertion AnomalyInsertion Anomaly––adding new rows forces user to create adding new rows forces user to create yy ggduplicate dataduplicate dataDeletion AnomalyDeletion Anomaly––deleting rows may cause a loss of data deleting rows may cause a loss of data that would be needed for other future rowsthat would be needed for other future rowsModification AnomalyModification Anomaly––changing data in a row forces changing data in a row forces changes to other rows because of duplicationchanges to other rows because of duplication

General rule of thumb: A table should not pertain to more than one entity type

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 4040

more than one entity type

Page 41: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

ExampleExample––Figure Figure 55--22bb

Question–Is this a relation? Answer–Yes: Unique rows and no multivalued attributes

Question–What’s the primary key? Answer–Composite: Emp_ID, Course_Title

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 4141

Page 42: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Anomalies in this TableAnomalies in this TableAnomalies in this TableAnomalies in this TableInsertionInsertion––can’t enter a new employee without can’t enter a new employee without p yp yhaving the employee take a classhaving the employee take a classDeletionDeletion––if we remove employeeif we remove employee 140140 we losewe loseDeletionDeletion if we remove employee if we remove employee 140140, we lose , we lose information about the existence of a Tax Acc classinformation about the existence of a Tax Acc classModificationModification giving a salary increase to employeegiving a salary increase to employeeModificationModification––giving a salary increase to employee giving a salary increase to employee 100 100 forces us to update multiple recordsforces us to update multiple records

Why do these anomalies exist? Because there are two themes (entity types) in thisBecause there are two themes (entity types) in this one relation. This results in data duplication and an unnecessary dependency between the entities

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 4242

y p y

Page 43: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Functional Dependencies and KeysFunctional Dependencies and KeysFunctional Dependencies and KeysFunctional Dependencies and KeysFunctional Dependency: The value of Functional Dependency: The value of p yp yone attribute (the one attribute (the determinantdeterminant) ) determines the value of anotherdetermines the value of anotherdetermines the value of another determines the value of another attributeattributeCandidate Key:Candidate Key:Candidate Key:Candidate Key:

A unique identifier. One of the candidate A unique identifier. One of the candidate keys will become the primary keykeys will become the primary keykeys will become the primary keykeys will become the primary key

E.g. perhaps there is both credit card number E.g. perhaps there is both credit card number and SS# in a table…in this case both areand SS# in a table…in this case both areand SS# in a table…in this case both are and SS# in a table…in this case both are candidate keyscandidate keys

Each nonEach non--key field is functionally key field is functionally

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 4343

y yy ydependent on every candidate keydependent on every candidate key

Page 44: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5.22 Steps in normalization

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 4444

Page 45: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

First Normal FormFirst Normal FormFirst Normal FormFirst Normal Form

No m lti al ed att ib tesNo m lti al ed att ib tesNo multivalued attributesNo multivalued attributesEvery attribute value is atomicEvery attribute value is atomicEvery attribute value is atomicEvery attribute value is atomicFig. Fig. 55--25 25 is notis not in in 11stst Normal Form Normal Form (multivalued attributes)(multivalued attributes) it is notit is not(multivalued attributes) (multivalued attributes) it is not it is not a relationa relationFig. Fig. 55--26 26 isis in in 11stst Normal formNormal formAll relationsAll relations are inare in 11stst NormalNormalAll relationsAll relations are in are in 11stst Normal Normal FormForm

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 4545

Page 46: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Table with multivalued attributes not in 1st normal formTable with multivalued attributes, not in 1st normal form

Note: this is NOT a relation

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 4646

Page 47: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Table with no multivalued attributes and unique rows, in 1st

normal form

Note: this is a relation, but not a well-structured one

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 4747

,

Page 48: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Anomalies in this TableAnomalies in this TableAnomalies in this TableAnomalies in this TableInsertionInsertion––if new product is ordered for order if new product is ordered for order 1007 1007 f i ti t t d t t bf i ti t t d t t bof existing customer, customer data must be reof existing customer, customer data must be re--

entered, causing duplicationentered, causing duplicationDeletionDeletion if we delete the Dining Table from Orderif we delete the Dining Table from OrderDeletionDeletion––if we delete the Dining Table from Order if we delete the Dining Table from Order 10061006, we lose information concerning this item's , we lose information concerning this item's finish and pricefinish and pricefinish and pricefinish and priceUpdateUpdate––changing the price of product ID changing the price of product ID 4 4 requires requires update in several recordsupdate in several recordspp

Why do these anomalies exist? B th lti l th ( tit t ) iBecause there are multiple themes (entity types) in one relation. This results in duplication and an unnecessary dependency between the entities

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 4848

unnecessary dependency between the entities

Page 49: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Second Normal FormSecond Normal FormSecond Normal FormSecond Normal Form11NF PLUSNF PLUS every nonevery non key attribute iskey attribute is11NF PLUS NF PLUS every nonevery non--key attribute is key attribute is fully functionally dependent on the fully functionally dependent on the ENTIRE primary keyENTIRE primary key

Every nonEvery non--key attribute must be defined bykey attribute must be defined byEvery nonEvery non key attribute must be defined by key attribute must be defined by the entire key, not by only part of the keythe entire key, not by only part of the keyN ti l f ti l d d iN ti l f ti l d d iNo partial functional dependenciesNo partial functional dependencies

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 4949

Page 50: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5 27 Functional dependency diagram for INVOICEFigure 5-27 Functional dependency diagram for INVOICE

Order ID Order Date Customer ID Customer Name Customer AddressOrder_ID Order_Date, Customer_ID, Customer_Name, Customer_AddressCustomer_ID Customer_Name, Customer_AddressProduct ID Product Description, Product Finish, Unit Price

Therefore NOT in 2nd Normal Form

Product_ID Product_Description, Product_Finish, Unit_PriceOrder_ID, Product_ID Order_Quantity

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 5050

Therefore, NOT in 2nd Normal Form

Page 51: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5 28 Removing partial dependenciesFigure 5-28 Removing partial dependencies

Getting it intoGetting it intoGetting it into Getting it into Second Normal FormSecond Normal Form

Partial dependencies are removed, but there are still transitive dependencies

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall5151

Page 52: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Third Normal FormThird Normal Form

22NF PLUS NF PLUS no transitive dependenciesno transitive dependencies(functional dependencies on non(functional dependencies on non--primaryprimary--key key ( p( p p yp y yyattributes)attributes)Note: This is called transitive, because the Note: This is called transitive, because the

i k i d t i t f thi k i d t i t f thprimary key is a determinant for another primary key is a determinant for another attribute, which in turn is a determinant for a attribute, which in turn is a determinant for a thirdthirdthirdthirdSolution: NonSolution: Non--key determinant with transitive key determinant with transitive dependencies go into a new table; nondependencies go into a new table; non--key key p g ;p g ; yydeterminant becomes primary key in the new determinant becomes primary key in the new table and stays as foreign key in the old table table and stays as foreign key in the old table

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 5252

Page 53: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5 29 Removing partial dependenciesFigure 5-29 Removing partial dependencies

Getting it into Getting it into Third Normal Third Normal FormFormFormForm

Transitive dependencies are removedTransitive dependencies are removed

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 5353

Page 54: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Merging RelationsMerging RelationsMerging RelationsMerging RelationsView IntegrationView Integration––Combining entities from Combining entities from gg ggmultiple ER models into common relationsmultiple ER models into common relationsIssues to watch out for when merging entities Issues to watch out for when merging entities g gg gfrom different ER models:from different ER models:

SynonymsSynonyms––two or more attributes with different two or more attributes with different b t ib t inames but same meaningnames but same meaning

HomonymsHomonyms––attributes with same name but different attributes with same name but different meaningsmeaningsmeaningsmeaningsTransitive dependenciesTransitive dependencies––even if relations are in even if relations are in 33NF NF prior to merging, they may not be after mergingprior to merging, they may not be after mergingp g g, y y g gp g g, y y g gSupertype/subtype relationshipsSupertype/subtype relationships––may be hidden prior may be hidden prior to mergingto merging

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 5454

Page 55: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Enterprise KeysEnterprise KeysEnterprise KeysEnterprise Keys

Primary keys that are unique in the Primary keys that are unique in the whole database not just within awhole database not just within awhole database, not just within a whole database, not just within a single relationsingle relationCorresponds with the concept of an Corresponds with the concept of an object ID in objectobject ID in object--oriented systemsoriented systemsobject objectobject object o e ted syste so e ted syste s

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 5555

Page 56: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

Figure 5-31 Enterprise keys

a) Relations with enterprise key

b) Sample data with enterprise key

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 5656

Page 57: Chapter Chapter 55:: Logical Database Design and …is.cba.edu.kw/433/Handouts/ch05.pdfLogical Database Design and the Relational Modelthe Relational Model Modern Database Management

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means electronicretrieval system, or transmitted, in any form or by any means, electronic,

mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America.

Copyright © Copyright © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall

Chapter 5 © © 2009 2009 Pearson Education, Inc. Pearson Education, Inc. Publishing as Prentice HallPublishing as Prentice Hall 5757