Docsity

ER to Relational Mapping: Translating Entity-Relationship Diagrams to Relational Schemas -, Study notes of Computer Science

Partial preview of the text

Download ER to Relational Mapping: Translating Entity-Relationship Diagrams to Relational Schemas - and more Study notes Computer Science in PDF only on Docsity!

ER to Relational

Mapping

Logical DB Design: ER to Relational

  • Entity sets to tables.

CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn))

Employees

ssn

name lot

ssn name lot 123-22-3666 Attishoo 48 231-31-5368 Smiley 22 131-24-3650 Smethurst 35

Review: Key Constraints

  • Each dept has at most one manager, according to the key constraint on Manages.

Translation to relational model?

1-to-1 1-to Many Many-to-1 Many-to-Many

dname did^ budget

since

lot

name ssn

Employees Manages Departments

Translating ER Diagrams with Key Constraints

  • Map relationship set to a table: - Note that did is the key now! - Separate tables for Employees and Departments.
  • Since each department has a unique manager, we could instead combine Manages and Departments.

CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)

CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)

Participation Constraints in SQL

  • We can capture participation constraints involving one entity set in a binary relationship, but little else (without resorting to CHECK constraints).

CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE NO ACTION)

Review: Weak Entities

  • A weak entity can be identified uniquely only by

considering the primary key of another (owner) entity.

  • Owner entity set and weak entity set must participate in a one-to-many relationship set (1 owner, many weak entities).
  • Weak entity set must have total participation in this

identifying relationship set.

lot

name pname age

Employees^ Dependents

ssn

Policy

cost

Review: ISA Hierarchies

Contract_Emps

name ssn

Employees

lot

hourly_wages ISA

Hourly_Emps

contractid

hours_worked

As in C++, or other PLs, attributes are inherited.

If we declare A ISA B, every A entity is also considered to be a B entity.

  • Overlap constraints: Can Joe be an Hourly_Emps as well as a Contract_Emps entity? (Allowed/disallowed)
  • Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no)

Translating ISA Hierarchies to Relations

  • General approach:
    • 3 relations: Employees, Hourly_Emps and Contract_Emps.
      • Hourly_Emps: Every employee is recorded in Employees. For hourly emps, extra info recorded in Hourly_Emps (hourly_wages, hours_worked, ssn); must delete Hourly_Emps tuple if referenced Employees tuple is deleted).
      • Queries involving all employees easy, those involving just Hourly_Emps require a join to get some attributes.
  • Alternative: Just Hourly_Emps and Contract_Emps.
    • Hourly_Emps: ssn, name, lot, hourly_wages, hours_worked.
    • Each employee must be in one of these two subclasses.

More on relationships

  • What about:
  • Could have :
  • put b 1 as the key for R1, it is also the key for E2=(b 1 , …., bn)
  • Usual strategy:
    • ignore R
    • Add a1, c1, …., ck to E2 instead, i.e.
    • E2=(b 1 , …., bn, a 1 , c 1 , …, ck)

E1 R1 E a 1 …. an c 1 …. ck b 1 …. bm

R1= ( a 1 , b 1 , c 1 , …, ck )

More

E1 R1 E a 1 …. an c 1 …. ck b 1 …. bm

R

R

R

R

E1 = ( a 1 , …, an ) E2 = ( b 1 ,^ …,^ bm ) R1 = ( a 1 , b 1 , c 1 …, ck )

E1 = ( a 1 , …, an ) E2 = ( b 1 , …, bm , a 1 , c 1 , …, ck) E1 = ( a 1 , …, an , b 1 , c 1 , …, ck) E2 = ( b 1 , …, bm ,)

Treat as n:1 or 1:m

E/R to Relational

E

S

Isa

S

a …

an

b 1 …. bm^ c 1 ….^ ck

Method 1: E = ( a^1 ,^ …,^ an^ ) S1 = (a 1 , b 1 , …, bm ) S2 = ( a 1 , c 1 …, ck )

Method 2: S1 = (a 1 ,…, an, b 1 , …, bm ) S2 = ( a 1 , …, an, c 1 …, ck )

Q: When is method 2 not possible?

E/R to Relational

  • Aggregation

E1 R1 E

R

E

a 1 …. an b 1 …. bm

c 1 …. ck

d 1 … dj

E1, R1, E2, E3 as before

R2 = (c 1 , a 1 , b 1 , d 1 , …, dj)