Databases I (H.9.1-2)Van EER naar relationeel
Wiebren de JongeVrije Universiteit, Amsterdam
versie 2003
Waar in DB ontwerp proces? Nu stap 3a.
Miniworld REQUIREMENTS COLLECTION AND ANALYSIS
CONCEPTUAL DB-DESIGNFUNCTIONAL DESIGN
Data RequirementsFunctional Requirements
High-level Transaction Specifiation Conceptual Schema (in a high-level data model)
LOGICAL DB-DESIGN(DATA MODEL MAPPING)
Conceptual Schema in DBMS independent data model
LOGICAL DB-DESIGN(DBMS MAPPING)
Logical (Conceptual) Schema(in the data model of a specific DBMS)
PHYSICAL DB-DESIGN
Internal Schema (for the same DBMS)
DBMS & implementation data model independent
Implementation data model specific
DBMS specific
APPLICATION PROGRAM DESIGN
TRANSACTION IMPLEMENTATION
Application Programs
1.
2a.2b.
3a.
3b.
4a.4b.
5.
Voorbeeld ER-diagram (Fig. 3.15)
Relationeel schema diagram (Fig. 7.7)
Voorbeeld mapping regular entity type
EMPLOYEEbdate
ssn
name
fname minit lname
sex
address
salary
1
FNAME MINIT LNAME SSN1
BDATE ADDRESS SEX SALARY
was oorspronkelijk composite attribuut
EMPLOYEE
1. Mapping regular entity types
Voor elk regular (non-weak, non-subclass) entity type E:
maak een tabel met als attributen alle simple attributes van E (N.B. ook de simple attributes die behoren tot evt. composite attributes)
laat eventuele multivalued attributes nog even achterwege kies één van de keys van E als primary key
Voorbeeld mapping weak entity type
DEPENDENT
name
sex
bdate relationship
DEPENDENTS_OF EMPLOYEE
bdate
ssn name
fname minit lname
sex
addresssalary
ESSN DEPENDENT_NAME1 SEX BDATE RELATIONSHIP
DEPENDENT
1
partial key weak entity
FK naar owner (EMPLOYEE)N.B. identifying rel.ship is nu in feite ook al verwerkt
2. Mapping weak entity types
Voor weak entity types E:
maak een tabel met als attributen alle simple attributes van E (N.B. ook de simple attributes die behoren tot evt. composite attributes)
voeg voor elke owner aan deze tabel een FK (foreign key) toe die verwijst naar die owner
(hiermee is de identifying relationship afgehandeld)
laat eventuele multivalued attributes nog even achterwege kies als primary key de combinatie van:
– de attributen van de partial key (tenminste, als die bestaat)
– de attributen van elke foreign key naar een owner specificeer bij FK’s naar owners: “cascade” of “restricted”
(beslist niet: “nullify” !!)
Voorbeeld mapping binary 1:1 rel.ship type
EMPLOYEE
bdate
ssn name
fname minit lname
sex
addresssalary
MANAGES
startdate
DEPARTMENT
name
number
locations
DNAME DNUMBER2 MGRSSN MGRSTARTDATE1
2
1
DEPARTMENTattribuut van relationship
FK naar “EMPLOYEE” (hier: no nulls)
Merk op: “locations” wordt niet in tabel “DEPARTMENT” opgenomen!
(0,1) (1,1)
dus: partial participation van EMPLOYEE
dus: total participation van DEPARTMENT
3. Mapping binary 1:1 relationship types
Voor binaire 1:1 relationship types:
kies één van de participerende entity types (zeg: E1),zo mogelijk (resp. liefst) één met totale participatie
voeg aan de tabel (zeg: R1) van dit entiteitstype E1 toe:– een foreign key naar de tabel van het andere entiteitstype– eventuele attributen van de relationship
als E1 totaal participeert in deze relationship, specificeerdan bij de genoemde FK “no nulls” (i.e. “nulls not allowed”)
Voorbeeld mapping binary 1:N rel.ship type
EMPLOYEE
bdate
ssn name
fname minit lname
sex
addresssalary
WORKS_FOR DEPARTMENT
name
number
locations2
1
(1,1) (4,N)
FNAME MINIT LNAME SSN1
BDATE ADDRESS SEX SALARY
EMPLOYEE deze hadden we al
DNO
nu toevoegen: FK naar “DEPARTMENT”(specificeer “no nulls” vanwege total partic.)
4. Mapping binary N:1 relationship types
Voor binaire N:1 (resp. 1:N) relationship types:
voeg in de tabel “aan de N-kant” het volgende toe:
– een FK (foreign key) naar de tabel “aan de 1-kant”
– eventuele attributen van de relationship als het entiteitstype “aan de N-kant” totaal participeert
in deze relationship, specificeer dan bij de genoemdeFK “no nulls” (oftewel “nulls not allowed”)
Voorbeeld mapping binary N:M rel.ship type
bdate
ssn name
fname minit lname
sex
addresssalary
WORKS_ON PROJECT
name
number
location
(1,N) (1,N)EMPLOYEE
2
1
hours
ESSN PNO1 1
WORKS_ONHOURS
FK naar “EMPLOYEE”FK naar “PROJECT”
attribuut van relationship type
5. Mapping binary M:N relationship types
Voor binaire M:N relationship types:
maak een tabel (zeg: R) met daarin:
– foreign keys naar de tabellen aan de M- en de N-kant
– eventuele attributen van de relationship kies als primary key van R de combinatie van alle
attributen van de bovengenoemde foreign keys specificeer bij de genoemde FK’s “cascade” of “restricted”
N.B.: 1:1 en N:1 relationship types kunnen evt. op
soortgelijke wijze afgehandeld worden !!
(Bij 1:1 zijn beide FK’s dan een sleutel en wordt eentje de PK. Bij N:1 is dan alleen de FK “naar de N-kant” een sleutel en dus PK.)
Extra voorbeeld mapping binary M:N rel.ship
ssn naam adres
LID_VAN PARTIJ
naam adres
PERSOONM N
SSN NAAM1 ADRES
SSN_PERSOON1
ADRESNAAM
NAAM_PARTIJ1
1
PERSOON
PERSOON_PARTIJ
PARTIJ
FK FK
Alternatieve mapping binary N:1 rel.ship types
ssn naam adres
LID_VAN PARTIJ
naam adres
PERSOONN 1
SSN NAAM1 ADRES
SSN_PERSOON1
ADRESNAAM
NAAM_PARTIJ
1
PERSOON
PERSOON_PARTIJ
PARTIJ
FK FK
SSN NAAM1 ADRES
PERSOON1.
2.
NAAM_PARTIJ ADRESNAAM1
PARTIJ
FKvoordelen: - relatief “eenvoudig en snel”
- indien persoon totale participatie heeft:geen nulls en geen verspilling opslagruimte
voordelen: - geen nulls, ook niet indien persoon partiële participatie heeft !!- minder opslagruimte nodig indien persoon (zeer) partiële participatie heeft- flexibel (als ’t later een N:M rel.ship wordt, dan slechts: kies nieuwe, samengestelde PK)
Voorbeeld mapping n-ary (n>2) rel.ship type
s# name
SUPPLY PROJECT
j# name
SUPPLIER
PART namep#
SNO1
JNO1
PNO1QUANTITY
SUPPLY
quantity
7. Mapping n-ary relationship types (n > 2)
Voor n-aire relationship types (n > 2):
maak een tabel (zeg: R) met daarin:– een foreign key naar iedere tabel van een in dit
relationship type participerend entiteitstype– eventuele attributen van dit relationship type
kies als primary key van R de attributen van de foreign keysmet “cardinaliteitsconstraint” N (oftewel “many”)
Voorbeeld mapping multi-valued attribute
DEPARTMENT
name
number
locations2
1
WORKS_FOR
MANAGES
CONTROLS
DNAME DNUMBER2 MGRSSN MGRSTARTDATE1
DEPARTMENT
dit hadden we al
DNUMBER DLOCATION1 1
DEPT_LOCATIONS
FK
dit nu toevoegen
6. Mapping multivalued attributes
Voor multivalued attributes:
maak voor ieder multi-valued attribute (A) een aparte tabel (R) met daarin:– het single-valued attribuut corresponderend met A– een foreign key (F) naar de tabel van het entiteitstype
met dit multi-valued attribute A de primary key van R bestaat uit de attributen van F en A
Voorbeeld mapping subclass entities (1)
EMPLOYEE
bdatessn name address
d
SALESMAN ENGINEERSECRETARY
specialism
limit
typing speed
NAME SSN1
BDATE
EMPLOYEE1)ADDRESS
TSPEED
SECRETARYSSN
1LIMIT
SALESMANSSN
1SPECIALISM
ENGINEERSSN
1
EMP-TYPE
Voorbeeld mapping subclass entities (2)
NAME SSN1 BDATE
SECRETARY2)ADDRESS TSPEED NAME SSN1 BDATE
SALESMANADDRESS LIMIT
NAME SSN1
BDATE
ENGINEERADDRESS SPECIALISM
EMPLOYEE3)NAME SSN1 BDATE ADDRESS TSPEEDEMP_TYPE LIMIT SPECIALISM
EMPLOYEE4)NAME SSN1 BDATE ADDRESS TSPEEDSEC_FLAG LIMIT SPECSALE_FLAG ENG_FLAG
8a. Mapping subclass entity types
Voor iedere subclass entity (Sub)
van een superclass entity (Super):
maak een tabel met daarin:– de local attributes (single-valued) van Sub– een foreign key naar Super
de primary key van Sub bestaat uit de attributes van de foreign key naar Super (i.e. Sub heeft dezelfde PK als Super)
Volgorde uitvoeren stappen
1. definieer tabellen voor:
a. reguliere entiteiten (boek: step 1)
b. subclass entiteiten (boek: step 8a)
c. zwakke entiteiten (boek: step 2)
2. voeg aan de tabellen van de entiteiten extra attributen toe voor:
a. binaire 1:1 relationships (boek: step 3)
b. binaire 1:N relationships (boek: step 4)
3. voeg extra tabellen toe voor:
a. binaire N:M relationships (boek: step 5)
b. n-aire relationships, n>2 (boek: step 7)
4. voeg extra tabellen toe voor:
multivalued attributes (boek: step 6)
Voorbeeld ternaire rel.ship als weak entity
s#
name
SS PROJECT
j#
name
SUPPLIER
PART namep#
SNO1
PNO1
JNO1QUANTITY
SUPPLY
SUPPLY
quantity
SJ
SP
N
N
N1
1
1
stap 1a: reguliere entiteiten
SUPPLIERSNO
1NAME
PARTPNO
1NAME
PROJECTJNO
1NAME
stap 1c: zwakke entiteiten
stap 2b: N:1 relationships
[geen verandering, want de toe te voegen foreign keys zitten al in SUPPLY!]