• No results found

datalab Databases

N/A
N/A
Protected

Academic year: 2022

Share "datalab Databases"

Copied!
31
0
0

Loading.... (view fulltext now)

Full text

(1)

Databases

04. A. ER to Relations (ch 4.5 and 4.6)

2018. Fall Instructor: Joonho Kwon [email protected] Data Science Lab @ PNU

These slides are mainly based on the chapter 9 of Fundamentals of Database Systems and Ch 4.6 of the text book.

datalab

data science laboratory

(2)

From ER diagrams to Relational Designs (4.5)

Converting Subclass Structures to Relations (4.6)

Outline

2

(3)

Entity sets

Attributes

Keys

Weak entity sets

Relationship sets

Attributes on relationships

Multiplicity

Roles

Binary versus 𝑛-ary relationships

Modeling 𝑛-ary relationships with weak entity sets and binary relationships

ISA relationships

E/R model: review

3

(4)

ER/EER to RDB schema

ER-diagram Relational database schema4

(5)

Basic cases

Entity set E  relation

Attribute of an entity set  attributes of relation

Relationship R  relation whose attributes are

attributes of R +

Key attributes of the connected entity sets

Special cases

weak entity sets

Combining relations (especially M-to-1 relationships)

is-a relationships and subclasses

From ER/EER to Relations

5

(6)

ER-to-Relational Mapping Algorithm

Step 1: Mapping of Regular Entity Types

Step 2: Mapping of Weak Entity Types

Step 3: Mapping of Binary 1:1 Relation Types

Step 4: Mapping of Binary 1:N Relationship Types.

Step 5: Mapping of Binary M:N Relationship Types.

Step 6: Mapping of Multivalued attributes.

Step 7: Mapping of N-ary Relationship Types.

Mapping EER Model Constructs to Relations

Entity-in-all-superclasses (E/R style)

Entity-in-most-specific-class (OO style)

All-entities-in-one-table (Null style)

Guidelines

6

(7)

STEP1: Strong Entity Types (1/2)

7

ER-diagram Relational database schema Regular (Strong ) entity type Relation

Simple attribute Attribute

One of key attributes Primary key

Fname

EMPLOYEE Minit Lname Name

Bdate Ssn

Address

Salary Sex

Fname Minit Lames Ssn Bdate Address Sex Salary EMPLOYEE

(8)

STEP1: Strong Entity Types (2/2)

8

DEPARTMENT Locations

Number Name

PROJECT

Name

Location Number

Dname Dnumber DEPARTMENT

Pname Pnumber Plocation PROJECT

(9)

STEP2: Weak Entity Types

9

ER-diagram Relational database schema

Weak entity type Relation

Simple attribute Attribute

One of key attributes of owner entity type + partial key attributes

Primary key

Dependent_name DEPENDENT

Q) Owner entity type? Q) Identifying relationship type?

Sex Birth_date Relationship

EMPLOYEE

DEPENDENT

HAS_

DEPENDENTS

Name Sex Birth_date Relationship Ssn

Essn

(10)

Identify 1:1 relationship type, and find an entity type with total participation (Manages – Department)

Add a foreign key

a primary key in related entity type

Add an attribute of relationship type

STEP3: 1:1 Relationship Types

10

Dname Dnumber Mgr_ssn Mgr_start_date DEPARTMENT

EMPLOYEE

SSN

DEPARTMENT

MANAGES

Locations

Number Start_date

Name

1 1

(11)

Identify 1:N relationship type

Add a foreign key in N-side

a primary key in related entity type

STEP4: 1:N Relationship Types (1/2)

11

EMPLOYEE

Fname Minit Lames Ssn Bdate Address Sex Salary

N 1

EMPLOYEE

SSN

DEPARTMENT

WORKS_FOR

Locations

Number Name

Dno

(12)

STEP4: 1:N Relationship Types (2/2)

12

EMPLOYEE

PROJECT

Pname Pnumber Plocation Dnum

DEPARTMENT

PROJECT

Name Location

Locations

Number Name

Number CONTROLS

N 1

EMPLOYEE

Ssn

SUPERVISION

1 N

Supervisor Supervisee

Fname Minit Lames Ssn Bdate Address Sex Salary Dno Super_ssn

(13)

Identify M:N relationship type

Create a new relation

Add foreign keys in both side

a primary key in related entity type

The combination of two foreign keys become a primary key

Add attributes of the relationship type

STEP5: M:N Relationship Types (1/2)

13

(14)

STEP5: M:N Relationship Types (2/2)

14

PROJECT

WORKS_ON

Name

Location Hours

Number

M N

EMPLOYEE Ssn

Essn Pno Hours

WORKS_ON

(15)

Create a new relation

Add two attributes

a multi-valued attribute

a key attribute of entity type

Combination of the attributes becomes a primary key

STEP6: Multivalued Attributes

15

Dlocation Dnumber DEPT_LOCATION

DEPARTMENT Locations

Number Name

{Dlocation, Dnumber}: primary key

(16)

Relationship type connected to more than 2 entity types

Create a new relation

Add foreign keys in all side

a primary key in related entity type

Add attributes of the relationship type

STEP7: N-ary Relationship Types (1/2)

16

(17)

STEP7: N-ary Relationship Types (2/2)

17

(18)

From Company ERD to RDB Schema

18

(19)

Correspondence

between ER and Relational Models

19

ER Model Relational Model

Entity type Entity relation

Weak Entity type Entity relation and foreign key

1:1 or 1:N relationship type Foreign key (or relationship relation)

M:N relationship type Relationship relation and two foreign keys n-ary relationship type Relationship relation and n foreign keys Simple attribute Attribute

Composite attribute Set of simple component attributes Multivalued attribute Relation and foreign key

Value set Domain

Key attribute Primary key

(20)

Translate the following ER Diagram into a relational database schema.

Exercise

20

(21)

Three approaches

Entity-in-all-superclasses approach (“E/R style”)

Entity-in-most-specific-class approach (“OO style”)

All-entities-in-one-table approach (“NULL style”)

Translating subclasses & ISA

21

Users Name

uid

Groups

Name

WORKS_ gid

ON

fromDate

PaidUsers Avatar

ISA

(22)

Approach

An entity is represented in the table for each subclass to which it belongs

A table includes only the attributes directly attached to the corresponding entity set, plus the inherited key

Entity-in-all-superclasses approach (“E/R style”)

22

Users Name

uid

Groups

Name

IsMember gid

Of

fromDate

PaidUsers Avatar

ISA

Group (gid, name) User (uid, name)

Member (uid, gid, from_date)

〈142, Bart〉

〈456, Ralph〉

〈456, 〉

PaidUser (uid, avatar)

(23)

Approach

An entity is only represented in one table (the most specific entity set to which the entity belongs)

A table includes the attributes attached to the

corresponding entity set, plus all inherited attributes

Translating subclasses & ISA: OO style

23

Users Name

uid

Groups

Name

IsMember gid

Of

fromDate

PaidUsers Avatar

ISA

Group (gid, name) User (uid, name)

Member (uid, gid, from_date)

〈142, Bart〉

〈456, Ralph, 〉

PaidUser (uid, name, avatar)

(24)

Approach

One relation for the root entity set, with all attributes found in the network of subclasses (plus a “type” attribute when needed)

Use a special NULL value in columns that are not relevant for a particular entity

All-entities-in-one-table approach (“NULL style”)

24

Users Name

uid

Groups

Name

IsMember gid

Of

fromDate

PaidUsers Avatar

ISA

Group (gid, name)

User (uid, name, avatar)

Member (uid, gid, from_date)

〈142, Bart, NULL〉

〈456, Ralph, 〉

(25)

Entity-in-all-superclasses (ER)

User (uid, name), PaidUser (uid, avatar)

Pro: All users are found in one table

Con: Attributes of paid users are scattered in different tables

Entity-in-most-specific-class (OO)

User (uid, name), PaidUser (uid, name, avatar)

Pro: All attributes of paid users are found in one table

Con: Users are scattered in different tables

Comparison of three approaches (1/2)

25

(26)

All-entities-in-one-table (NULL)

User (uid, [type, ]name, avatar)

Pro: Everything is in one table

Con: Lots of NULL’s; complicated if class hierarchy is complex

Comparison of three approaches (2/2)

26

(27)

Example

27

Trains engineer

number

ISA

LocalTrains ExpressTrain

ISA

ExpressStations LocalStations

Stations

address name

ExpressTrainStops LocalTrainStops

time time

Train (number, engineer) LocalTrain (number)

ExpressTrain (number)

Station (name, address) LocalStation (name) ExpressStation (name)

LocalTrainStop (local_train_number, time)

LocalTrainStopsAtStation (local_train_number, time, station_name) ExpressTrainStop (express_train_number, time)

ExpressTrainStopsAtStation (express_train_number, time, express_station_name)

merge

merge

(28)

Eliminate LocalTrain table

Redundant: can be computed as 𝜋𝑛𝑢𝑚𝑏𝑒𝑟(𝑇𝑟𝑎𝑖𝑛) − 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑇𝑟𝑎𝑖𝑛

Slightly harder to check that local_train_number is indeed a local train number

Eliminate LocalStation table

It can be computed as 𝜋𝑛𝑢𝑚𝑏𝑒𝑟(𝑆𝑡𝑎𝑡𝑖𝑜𝑛) − 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑆𝑡𝑎𝑡𝑖𝑜𝑛

Simplifications and refinements

28

Train (number, engineer) LocalTrain (number)

ExpressTrain (number)

Station (name, address) LocalStation (name) ExpressStation (name)

LocalTrainStop (local_train_number, station_name, time)

ExpressTrainStop (express_train_number, express_station_name , time)

(29)

Encode the type of train/station as a column rather than creating subclasses

What about the following constraints?

Type must be either “local” or “express”

Express trains only stop at express stations

They can be expressed/declared explicitly as database constraints in SQL (as we will see later in course)

Arguably a better design because it is simpler

An alternative design

29

Train (number, engineer, type) Station (name, address, type)

TrainStop (train_number, station_name, time)

(30)

KISS

Keep It Simple, Stupid

Avoid redundancy

Redundancy wastes space, complicates modifications, promotes inconsistency

Capture essential constraints, but don’t introduce unnecessary restrictions

Use your common sense

Warning: mechanical translation procedures given in this lecture are no substitute for your own judgment

Design principles

30

(31)

Q&A

31

References

Related documents

Choose from top universities and eer diagrams manually label entities from relational schema generation of people, which student classes, and data model, it should be two..

Define The Following Terms: Relation Schema, Relational Database Schema, Domain, Attribute, Attribute Domain, Relation Instance, Relation Cardinality, And Relation Degree. One

The effects of footwear on the development of children ’s feet has been debated for many years and recent work from the developmental and biomechanical literature has

◦ For each regular entity type, create a relation R that includes all the simple attributes of E.. ◦ Called

Relational DBMS Entity-Relationship model is used in the conceptual design of various database conceptual level conceptual schema Design is independent.. We can have the schema

Relational database schema Relational DBMS Conceptual level Logical level Physical level ER design is subjective: There are many ways to model a given scenario.. Products are shipped

◼ For each weak entity type W in the ER schema with owner entity type E, create a relation R & include all simple attributes (or.. simple components of composite attributes) of

 A história do acampamento é a seguinte.Agentes da Estrela de Aço, ainda leais à Sinter, estavam procurando por ela quanto acamparam aqui na noite anterior à chegada dos PJs.Como