13
Convert ER to Relational Convert ER to Relational Database Database Entity Entity relation relation Attributes attributes Attributes attributes Primary key primary key Primary key primary key Relationship Relationship relation relation Attributes attributes Attributes attributes We will talk about primary later We will talk about primary later Weak entity set Weak entity set relation relation Attributes attributes Attributes attributes We will talk about primary key later We will talk about primary key later

Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

Embed Size (px)

Citation preview

Page 1: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

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 later We will talk about primary 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 2: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

Representing Entity Sets as Representing Entity Sets as TablesTables

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

Page 3: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

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 4: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

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 5: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

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

Page 6: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

Representing Representing Relationship Sets as Relationship Sets as

TablesTables A many-to-many A many-to-many

relationship set is relationship set is represented as a represented as a table with columns table with columns for the primary keys for the primary keys of the two of the two participating entity participating entity sets, and any sets, and any descriptive attributes descriptive attributes of the relationship of the relationship set. set. E.g.: table for E.g.: table for

relationship set relationship set borrowerborrower

Page 7: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

Redundancy of TablesRedundancy of 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

Page 8: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

Redundancy of TablesRedundancy of 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

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

Page 9: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

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 10: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

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

Relationship setRelationship set. The union of the primary . The union of the primary keys of the related entity sets becomes a keys of the related entity sets becomes a super key of the relation.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 relation’s primary key. Why?the relation’s primary key. Why?

For one-to-one relationship sets, the relation’s For one-to-one relationship sets, the relation’s primary key can be that of either entity set. Why?primary key can be that of either entity set. Why?

For many-to-many relationship sets, the union of For many-to-many relationship sets, the union of the primary keys becomes the relation’s primary the primary keys becomes the relation’s primary key. Why?key. Why?

Page 11: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

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 table table table attributestable attributespersonpersonname, street, city name, street, city customercustomer name, credit-ratingname, credit-ratingemployeeemployee name, salaryname, salary

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

Page 12: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

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 attributestable table table attributestable attributes

personpersonname, street, cityname, street, citycustomercustomer name, street, city, credit-ratingname, street, city, credit-ratingemployee employee name, street, city, salaryname, street, city, salary

If specialization is total, table for generalized entity If specialization is total, table for generalized 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 Drawback: street and city may be stored redundantly Drawback: street and city may be stored redundantly

for persons who are both customers and employeesfor persons who are both customers and employees

Page 13: Convert ER to Relational Database Entity relation Entity relation Attributes attributes Attributes attributes Primary key primary key Primary key primary

ER for Banking EnterpriseER for Banking Enterprise