Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases...

34
Chapter 3: Relational Chapter 3: Relational Model I Model I Structure of Relational Structure of Relational Databases Databases Convert a ER Design to a Convert a ER Design to a Relational Database Relational Database

Transcript of Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases...

Page 1: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Chapter 3: Relational Chapter 3: Relational Model IModel I

Structure of Relational Structure of Relational DatabasesDatabases

Convert a ER Design to a Convert a ER Design to a Relational DatabaseRelational Database

Page 2: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

RelationRelation

Another name for Another name for tabletable Columns – attributesColumns – attributes Rows – tuplesRows – tuples Content of a table – instance of a relationContent of a table – instance of a relation

Page 3: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Attribute TypesAttribute Types

Each attribute of a relation has a nameEach attribute of a relation has a name The set of allowed values for each attribute is The set of allowed values for each attribute is

called the called the domaindomain of the attribute of the attribute Attribute values are (normally) required to be Attribute values are (normally) required to be

atomicatomic, that is, indivisible, that is, indivisible E.g. multivalued attribute values are not atomicE.g. multivalued attribute values are not atomic E.g. composite attribute values are not atomicE.g. composite attribute values are not atomic

The special value The special value nullnull is a member of every is a member of every domaindomain

Page 4: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Example of a RelationExample of a Relation

Page 5: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

FormallyFormally

Given sets Given sets DD11, , DD22, …. , …. DDnn a a relationrelation r r is a is a subset of subset of DD11 x x DD2 2 x … x x … x DDnn

Thus a relation is a set of n-tuples (Thus a relation is a set of n-tuples (aa11,, a a22, , …, …, aann) where each ) where each aaii DDii

Page 6: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Relation Relates ThingsRelation Relates Things

Things:Things:customer-namecustomer-name = {Jones, Smith, Curry, = {Jones, Smith, Curry,

Lindsay}Lindsay}customer-streetcustomer-street = {Main, North, Park} = {Main, North, Park}customer-citycustomer-city = {Harrison, Rye, Pittsfield} = {Harrison, Rye, Pittsfield}

RelationRelationThen Then rr = { (Jones, Main, Harrison), = { (Jones, Main, Harrison),

(Smith, North, Rye), (Smith, North, Rye), (Curry, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)} (Lindsay, Park, Pittsfield)} is a relation over is a relation over customer-name x customer-customer-name x customer-street x customer-citystreet x customer-city

Page 7: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Relation SchemaRelation Schema

AA11, , AA22, …, , …, AAnn are are attributesattributes

RR = ( = (AA11, , AA22, …, , …, AAnn ) is a ) is a relation schemarelation schema

E.g. E.g. Customer-schemaCustomer-schema = = ( (customer-name, customer-street, customer-name, customer-street, customer-citycustomer-city))

rr((RR) is a ) is a relationrelation on the on the relation schema Rrelation schema R

E.g.E.g. customer (Customer-schema)customer (Customer-schema)

Page 8: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Relation InstanceRelation Instance The current values (The current values (relation instancerelation instance) )

of a relation are specified by a tableof a relation are specified by a table An element An element tt of of rr is a is a tupletuple, ,

represented by a represented by a row row in a tablein a table

JonesSmithCurryLindsay

customer-name

MainNorthNorthPark

customer-street

HarrisonRyeRye

Pittsfield

customer-city

customer

attributes(or columns)

tuples(or rows)

Page 9: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Relations are UnorderedRelations are Unordered

Order of tuples is irrelevant (tuples may be stored in an arbitrary order)

Page 10: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

DatabaseDatabase

In relational database, a database In relational database, a database consists of many relationsconsists of many relations Both things and their relationships are Both things and their relationships are

represented by relationsrepresented by relations Normalization theory (Chapter 7) deals Normalization theory (Chapter 7) deals

with how to design relational schemaswith how to design relational schemas

Page 11: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

KeysKeys Let K Let K R R K K is a is a superkeysuperkey of of RR if values for if values for KK are are

sufficient to identify a unique tuple of each sufficient to identify a unique tuple of each possible relation possible relation r(R)r(R) by “possibleby “possible r r” we mean a relation ” we mean a relation rr that could that could

exist in the enterprise we are modeling.exist in the enterprise we are modeling. Example: {Example: {customer-name, customer-streetcustomer-name, customer-street} }

andand { {customer-namecustomer-name} } are both superkeys of are both superkeys of CustomerCustomer, if no two , if no two customers can possibly have the same name.customers can possibly have the same name.

Page 12: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Candidate KeysCandidate Keys

KK is a is a candidate keycandidate key if if KK is minimal is minimal

Example: {Example: {customer-namecustomer-name} is a candidate key } is a candidate key for for CustomerCustomer, since it is a superkey (assuming , since it is a superkey (assuming no two customers can possibly have the same no two customers can possibly have the same name), and no subset of it is a superkey.name), and no subset of it is a superkey.

Page 13: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Convert ER to Relational Convert ER to Relational DatabaseDatabase

Entity Entity relationrelation Attributes attributesAttributes attributes Primary key primary key Primary key primary key

Relationship Relationship relationrelation Attributes attributesAttributes attributes We will talk about primary key later We will talk about primary key later

Weak entity set Weak entity set relationrelation Attributes attributesAttributes attributes We will talk about primary key laterWe will talk about primary key later

Page 14: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Representing Entity Sets as Representing Entity Sets as TablesTables

A strong entity set reduces to a table A strong entity set reduces to a table with the same attributes.with the same attributes.

The primary key of the entity set The primary key of the entity set becomes the primary key of the becomes the primary key of the relation.relation.

Page 15: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.
Page 16: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Composite AttributesComposite Attributes

Composite attributes are flattened out by Composite attributes are flattened out by creating a separate attribute for each creating a separate attribute for each component attributecomponent attribute E.g. given entity set E.g. given entity set customecustomer with composite r with composite

attribute attribute namename with component attributes with component attributes first-first-name name and and last-namelast-name the table corresponding to the table corresponding to the entity set has two attributesthe entity set has two attributes name.first-namename.first-name and and name.last-name.last-namename

Page 17: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Multivalued AttributesMultivalued Attributes

A multivalued attribute M of an entity E is A multivalued attribute M of an entity E is represented by a separate table EMrepresented by a separate table EM Table EM has attributes corresponding to the primary Table EM has attributes corresponding to the primary

key of E and an attribute corresponding to multivalued key of E and an attribute corresponding to multivalued attribute Mattribute M

E.g. Multivalued attribute E.g. Multivalued attribute dependent-namesdependent-names of of employeeemployee is represented by a table is represented by a table employee-dependent-namesemployee-dependent-names(( employee-id, dname employee-id, dname))

Each value of the multivalued attribute maps to a Each value of the multivalued attribute maps to a separate row of the table EMseparate row of the table EM

E.g., an employee entity with primary key John and E.g., an employee entity with primary key John and dependents Johnson and Johndotir maps to two rows: dependents Johnson and Johndotir maps to two rows: (John, Johnson) and (John, Johndotir) (John, Johnson) and (John, Johndotir)

Page 18: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

ExampleExample

The relation(s) the ER mapped to?

1. customer(customer-id, first-name, last-name, middle-initial,date-of-birth,age,street-number,street-name,apartment-number,city,state,zip-code)

2. customer-phone(customer-id,phone-number)

Page 19: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Representing Weak Entity Representing Weak Entity SetsSets

A weak entity set becomes a table that includes a column for the primary key of the identifying strong entity set

The primary key of the relation consists of The primary key of the relation consists of the union of the primary key of the strong the union of the primary key of the strong entity set and the discriminator of the weak entity set and the discriminator of the weak entity set.entity set.

Page 20: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Weak Entity ExampleWeak Entity Example

Page 21: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Representing Representing Relationship Sets as Relationship Sets as

TablesTables A many-to-many relationship set is A many-to-many relationship set is

represented as a table with attributes from represented as a table with attributes from the primary keys of the two participating the primary keys of the two participating entity sets, and any descriptive attributes of entity sets, and any descriptive attributes of the relationship set. the relationship set. E.g.: table for relationship set E.g.: table for relationship set borrowerborrower The union of the primary keys of the related entity sets The union of the primary keys of the related entity sets

becomes a super key of the relation.becomes a super key of the relation.

Page 22: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Many-to-many relationship

What is the relationship borrower has an attribute “date”?

Page 23: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Representing Relationship Representing Relationship Sets as TablesSets as Tables

Many-to-one and one-to-many relationship sets that are total on the many-side can be represented by adding an extra attribute to the many side, containing the primary key of the one side E.g.: Instead of creating a table for relationship account-branch, add an attribute branch to the entity set account

the primary key of the “many” entity set the primary key of the “many” entity set becomes the primary key that represents becomes the primary key that represents the relationship and the many sidethe relationship and the many side

If participation is If participation is partialpartial on the many side, on the many side, replacing a table by an extra attribute in the replacing a table by an extra attribute in the relation corresponding to the “many” side relation corresponding to the “many” side could result in null valuescould result in null values

Page 24: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

account(account-number,balance)

branch(branch-name,branch-city,assets)

account-branch(account-number,branch-name)

account(account-number,balance,branch-name)

branch(branch-name,branch-city,assets)

Redundancy!

?

Page 25: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Representing Relationship Representing Relationship Sets as TablesSets as Tables

For one-to-one relationship sets, either For one-to-one relationship sets, either side can be chosen to act as the “many” side can be chosen to act as the “many” sideside That is, extra attribute can be added to either That is, extra attribute can be added to either

of the tables corresponding to the two entity of the tables corresponding to the two entity sets sets

Page 26: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Determining Keys from E-R Determining Keys from E-R SetsSets

Strong entity setStrong entity set. The primary key of . The primary key of the entity set becomes the primary key of the entity set becomes the primary key of the relation.the relation.

Weak entity setWeak entity set. The primary key of the . The primary key of the relation consists of the union of the relation consists of the union of the primary key of the strong entity set and primary key of the strong entity set and the discriminator of the weak entity set.the discriminator of the weak entity set.

Page 27: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Determining Keys from E-R Determining Keys from E-R SetsSets

Relationship setRelationship set. The union of the . The union of the primary keys of the related entity sets primary keys of the related entity sets becomes a super key of the relation.becomes a super key of the relation. For binary many-to-one relationship sets, the For binary many-to-one relationship sets, the

primary key of the “many” entity set becomes primary key of the “many” entity set becomes the primary key that represent both the the primary key that represent both the relationship and the many side. Why?relationship and the many side. Why?

What about one-to-one relationship sets. Why?What about one-to-one relationship sets. Why? For many-to-many relationship sets, the union For many-to-many relationship sets, the union

of the primary keys becomes the relation’s of the primary keys becomes the relation’s primary key. Why?primary key. Why?

Page 28: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Representing Representing Specialization as TablesSpecialization as Tables

Method 1: Method 1: Form a table for the higher level entity Form a table for the higher level entity Form a table for each lower level entity set, Form a table for each lower level entity set,

include primary key of higher level entity set include primary key of higher level entity set and local attributesand local attributes

Page 29: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Person(Person(namename, street, city), street, city)

Customer(Customer(namename, credit-, credit-rating)rating)

Employee(Employee(namename, salary), salary)

Drawback: getting Drawback: getting information about, information about, e.g., e.g., employeeemployee requires accessing requires accessing two tablestwo tables

Page 30: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Representing Specialization as Representing Specialization as TablesTables

Method 2: Method 2: Form a table for each entity set with all local and Form a table for each entity set with all local and

inherited attributesinherited attributes If specialization is total, table for generalized If specialization is total, table for generalized

entity (entity (personperson) not required to store information) not required to store information Can be defined as a “view” relation containing union of Can be defined as a “view” relation containing union of

specialization tablesspecialization tables But explicit table may still be needed for foreign key But explicit table may still be needed for foreign key

constraintsconstraints

Page 31: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Person(Person(namename, street, city), street, city)

Customer(Customer(namename, street, city,credit-, street, city,credit-rating)rating)

Employee(Employee(namename, street,citysalary), street,citysalary)

Drawback: street Drawback: street and city may be and city may be stored redundantly stored redundantly for persons who are for persons who are both person and both person and customers/employeecustomers/employeess

Page 32: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

ER for Banking EnterpriseER for Banking Enterprise

Page 33: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Schema Diagram for the Schema Diagram for the Banking EnterpriseBanking Enterprise

Page 34: Chapter 3: Relational Model I Structure of Relational Databases Structure of Relational Databases Convert a ER Design to a Relational Database Convert.

Convert the ER diagram to relational Convert the ER diagram to relational models.models.