chapter 3: relational model structure of relational databases normal forms (chap. 7) reduction of...
TRANSCRIPT
Chapter 3: Relational ModelChapter 3: Relational Model Structure of Relational DatabasesStructure of Relational Databases Normal forms (chap. 7)Normal forms (chap. 7) Reduction of an E-R Schema to Relational Reduction of an E-R Schema to Relational
(Sect. 2.9)(Sect. 2.9) Relational AlgebraRelational Algebra Tuple Relational CalculusTuple Relational Calculus Domain Relational CalculusDomain Relational Calculus Extended Relational-Algebra-OperationsExtended Relational-Algebra-Operations Modification of the DatabaseModification of the Database ViewsViews
E-R Diagram for the Banking E-R Diagram for the Banking EnterpriseEnterprise
Reduction of an E-R Schema to Reduction of an E-R Schema to Relational (Silberchatz Sect. 2.9)Relational (Silberchatz Sect. 2.9)
Entity sets and relationship sets can be Entity sets and relationship sets can be expressed uniformly as expressed uniformly as relations/tablesrelations/tables which represent the contents of the which represent the contents of the database.database.
A database which conforms to an E-R A database which conforms to an E-R diagram can be represented by a collection diagram can be represented by a collection of relations/tables.of relations/tables.
Each relation/table has a number of Each relation/table has a number of columnscolumns (generally corresponding to attributes), which (generally corresponding to attributes), which have unique names.have unique names.
Representing Entity SetsRepresenting Entity Sets A A strong entity setstrong entity set reduces to a table with the reduces to a table with the
same attributessame attributes
The The primary key of the entity setprimary key of the entity set becomes the becomes the primary key of the relation.primary key of the relation.
Customer(Customer(customer-idcustomer-id, customer-name, customer-street, customer-city), customer-name, customer-street, customer-city)
Composite and Multivalued Composite and Multivalued AttributesAttributes
Composite attributesComposite attributes are flattened out by creating a are flattened out by creating a separate attribute for each component attributeseparate attribute for each component attribute E.g. given entity set E.g. given entity set customecustomer with composite attribute r with composite attribute namename
with component attributes with component attributes first-name first-name and and last-namelast-name the table the table corresponding to the entity set has two attributescorresponding to the entity set has two attributes name.first-namename.first-name and and name.last-namename.last-name
Multivalued attributeMultivalued attribute M of an entity E is represented by M of an entity E is represented by a separate table EMa separate table EM Table EM has attributes corresponding to the Table EM has attributes corresponding to the
primary key of E and an attribute corresponding to primary key of E and an attribute corresponding to multivalued attribute Mmultivalued attribute M
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
Representing Weak Entity SetsRepresenting Weak Entity Sets A weak entity set becomes a relation/table that includes a column for
the primary key of the identifying strong entity set
The primary key of the relation consists of the union of the primary key of the strong entity set and the discriminator of the weak entity set.
The relation/table corresponding to a relationship set linking a weak entity set to its identifying strong entity set is redundant.
Payment (loan-number, payment-number, payment-date, payment-amount)
Representing Relationship SetsRepresenting Relationship Sets A A many-to-many relationshipmany-to-many relationship set is represented as a relation/table set is represented as a relation/table
with columns for the primary keys of the two participating entity sets, with columns for the primary keys of the two participating entity sets, and any descriptive attributes of the relationship set. and any descriptive attributes of the relationship set.
The union of the primary keys of the related entity sets becomes a The union of the primary keys of the related entity sets becomes a super key of the relation.super key of the relation.
Borrower (customer-id, loan-number)• For binary many-to-one For binary many-to-one relationship sets, the relationship sets, the primary key of the “many” primary key of the “many” entity set becomes the entity set becomes the relation’s primary key.relation’s primary key.
• For one-to-one relationship For one-to-one relationship sets, the relation’s primary sets, the relation’s primary key can be that of either key can be that of either entity set.entity set.
• For For many-to-manymany-to-many relationship sets, the union relationship sets, the union of the primary keys of the primary keys becomes the relation’s becomes the relation’s primary keyprimary key
Many-to-one, one-to-many and one-to-one Many-to-one, one-to-many and one-to-one relationshipsrelationships
Many-to-one and one-to-many relationship sets 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
Account (account-number, balance, branch-name)
Many-to-one, one-to-many and Many-to-one, one-to-many and one-to-one relationships (Cont.)one-to-one relationships (Cont.)
For For one-to-oneone-to-one relationship sets, either side relationship sets, either side can be chosen to act as the “many” sidecan be chosen to act as the “many” side That is, extra attribute can be added to either of That is, extra attribute can be added to either of
the tables corresponding to the two entity sets the tables corresponding to the two entity sets
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
Representing SpecializationRepresenting Specialization Method 1Method 1: :
Form a relation/table for the Form a relation/table for the higher level entity higher level entity Form a table for each Form a table for each lower level entity setlower level entity set, include , include
primary key of higher level entity set and local primary key of higher level entity set and local attributesattributes
Person ( Person (namename, street, city) , street, city) Customer ( Customer (namename, credit-rating), credit-rating)
Employee (Employee (namename, salary), salary)
DrawbackDrawback: getting information about, e.g., : getting information about, e.g., employeeemployee requires accessing two tables requires accessing two tables
Representing Specialization Representing Specialization (Cont.)(Cont.) Method 2Method 2: :
Form a relation/table for Form a relation/table for each entity set with all each entity set with all local and inherited attributeslocal and inherited attributes
person (person (namename, street, city, street, city ))customer (customer (namename, street, city, credit-rating), street, city, credit-rating)
employee (employee (namename, street, city, salary), 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
DrawbackDrawback: street and city may be stored : street and city may be stored redundantly for people who are both customers redundantly for people who are both customers and employeesand employees
Representing AggregationRepresenting Aggregation
To represent aggregation, create a relation/table containing:
primary key of the aggregated relationship,
primary key of the associated entity set
any descriptive attributes
Representing Aggregation Representing Aggregation (Cont.)(Cont.)
E.g. to represent aggregation manages between relationship works-on and entity set manager, create a relation
Manages (employee-id, branch-name, title, manager-name) Table works-on is redundant provided we are willing to store
null values for attribute manager-name in table manages
Schema Diagram for the Banking EnterpriseSchema Diagram for the Banking Enterprise