eer >r.model

64
1 Translation of ER-diagram Translation of ER-diagram into Relational Schema into Relational Schema Prof. Sin-Min Lee Department of Computer Science

Upload: lavya3

Post on 28-Nov-2014

766 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Eer >r.model

1

Translation of ER-diagram Translation of ER-diagram into Relational Schemainto Relational Schema

Prof. Sin-Min Lee

Department of Computer

Science

Page 2: Eer >r.model

2

Learning ObjectivesLearning Objectives Define each of the following database terms

Relation

Primary key

Foreign key

Referential integrity

Field

Data type

Null value

9.2

Discuss the role of designing databases in the analysis and design of an information system

Learn how to transform an entity-relationship (ER) Diagram into an equivalent set of well-structured relations

Page 3: Eer >r.model

3

Page 4: Eer >r.model

4

9.4

Page 5: Eer >r.model

5

Page 6: Eer >r.model

6

Process of Database Process of Database Design Design

• Logical Design– Based upon the conceptual data model

– Four key steps

1. Develop a logical data model for each known user interface for the application using normalization principles.

2. Combine normalized data requirements from all user interfaces into one consolidated logical database model

3. Translate the conceptual E-R data model for the application into normalized data requirements

4. Compare the consolidated logical database design with the translated E-R model and produce one final logical database model for the application9.6

Page 7: Eer >r.model

7

9.7

Page 8: Eer >r.model

8

Relational Database ModelRelational Database Model• Data represented as a set of related tables or

relations

• Relation

– A named, two-dimensional table of data. Each relation consists of a set of named columns and an arbitrary number of unnamed rows

– Properties• Entries in cells are simple

• Entries in columns are from the same set of values

• Each row is unique

• The sequence of columns can be interchanged without changing the meaning or use of the relation

• The rows may be interchanged or stored in any sequence

9.8

Page 9: Eer >r.model

9

Relational Database ModelRelational Database Model

• Well-Structured Relation

– A relation that contains a minimum

amount of redundancy and allows

users to insert, modify and delete the

rows without errors or inconsistencies

9.9

Page 10: Eer >r.model

10

Transforming E-R Diagrams Transforming E-R Diagrams into Relationsinto Relations

• It is useful to transform the conceptual

data model into a set of normalized

relations

• Steps

1. Represent entities

2. Represent relationships

3. Normalize the relations

4. Merge the relations

9.10

Page 11: Eer >r.model

11

Transforming E-R Diagrams Transforming E-R Diagrams into Relationsinto Relations

– The primary key must satisfy the

following two conditions

a. The value of the key must uniquely identify

every row in the relation

b. The key should be nonredundant9.11

Page 12: Eer >r.model

12

9.12

Page 13: Eer >r.model

13

Page 14: Eer >r.model

14

Transforming E-R Diagrams Transforming E-R Diagrams into Relationsinto Relations

1. Represent Relationships

– Binary 1:N Relationships

• Add the primary key attribute (or attributes)

of the entity on the one side of the

relationship as a foreign key in the relation

on the right side

• The one side migrates to the many side

9.14

Page 15: Eer >r.model

15

9.15

Page 16: Eer >r.model

16

Transforming E-R Diagrams Transforming E-R Diagrams into Relationsinto Relations

– Binary or Unary 1:1

•Three possible options

a.Add the primary key of A as a foreign key of B

b.Add the primary key of B as a foreign key of A

c.Both

9.16

Page 17: Eer >r.model

17

Transforming E-R Diagrams Transforming E-R Diagrams into Relationsinto Relations

1. Represent Relationships

(continued)

– Binary and higher M:N relationships

• Create another relation and include

primary keys of all relations as primary

key of new relation

9.17

Page 18: Eer >r.model

18

9.18

Page 19: Eer >r.model

19

Transforming E-R Diagrams Transforming E-R Diagrams into Relationsinto Relations

– Unary 1:N Relationships

• Relationship between instances of a single entity type

• Utilize a recursive foreign key

– A foreign key in a relation that references the primary key

values of that same relation

– Unary M:N Relationships

• Create a separate relation

• Primary key of new relation is a composite of two

attributes that both take their values from the same

primary key

9.19

Page 20: Eer >r.model

20

9.20

Page 21: Eer >r.model

21

9.21

Page 22: Eer >r.model

22

Primary Key ConstraintsPrimary Key Constraints

• A set of fields is a key for a relation if :

1. No two distinct tuples can have same values in all key fields, and

2. This is not true for any subset of the key.

– Part 2 false? A superkey.

– If there’s >1 key for a relation, one of the keys is chosen (by DBA) to be the primary key.

• E.g., sid is a key for Students. (What about name?) The set {sid, gpa} is a superkey.

Primary key can not have null value

Page 23: Eer >r.model

23

Foreign Keys, Referential Foreign Keys, Referential IntegrityIntegrity

• Foreign key : Set of fields in one relation that is used to `refer’ to a tuple in another relation. (Must correspond to primary key of the second relation.) Like a `logical pointer’.

• E.g. sid is a foreign key referring to Students:– Enrolled(sid: string, cid: string, grade: string)

– If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references.

– Can you name a data model w/o referential integrity?

• Links in HTML!

Page 24: Eer >r.model

24

Enforcing Referential Enforcing Referential IntegrityIntegrity

• Consider Students and Enrolled; sid in Enrolled is a foreign key that references Students.

• What should be done if an Enrolled tuple with a non-existent student id is inserted? (Reject it!)

• What should be done if a Students tuple is deleted?

– Also delete all Enrolled tuples that refer to it.

– Disallow deletion of a Students tuple that is referred to.

– Set sid in Enrolled tuples that refer to it to a default sid.

– (In SQL, also: Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown’ or `inapplicable’.)

• Similar if primary key of Students tuple is updated.

Page 25: Eer >r.model

25

Logical DB Design: ER to Logical DB Design: ER to RelationalRelational

• Entity sets to tables.

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

Employees

ssnname

lot

Page 26: Eer >r.model

26

Relationship Sets to TablesRelationship Sets to Tables

• In translating a relationship set to a relation, attributes of the relation must include:

– Keys for each participating entity set (as foreign keys).

•This set of attributes forms a superkey for the relation.

– All descriptive attributes.

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

Page 27: Eer >r.model

27

Review: Key ConstraintsReview: Key Constraints

• Each dept has at most

one manager,

according to the key

constraint on Manages.

Translation to relational model?

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

dname

budgetdid

since

lot

name

ssn

ManagesEmployees Departments

Page 28: Eer >r.model

28

Translating ER Diagrams with Key Translating ER Diagrams with Key ConstraintsConstraints

• Map relationship 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)

Page 29: Eer >r.model

29

Review: Participation Review: Participation ConstraintsConstraints

• Does every department have a manager?

– If so, this is a participation constraint: the participation of Departments in

Manages is said to be total (vs. partial).

• Every did value in Departments table must appear in a row of the Manages

table (with a non-null ssn value!)

lot

name dnamebudgetdid

sincename dname

budgetdid

since

Manages

since

DepartmentsEmployees

ssn

Works_In

Page 30: Eer >r.model

30

Participation Constraints Participation Constraints in SQLin 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)

Page 31: Eer >r.model

31

Review: Weak EntitiesReview: 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

agepname

DependentsEmployees

ssn

Policy

cost

Page 32: Eer >r.model

32

Page 33: Eer >r.model

33

Translating Weak Entity Translating Weak Entity SetsSets

• Weak entity set and identifying relationship set are translated into a

single table.

– When the owner entity is deleted, all owned weak entities must

also be deleted.

CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)

Page 34: Eer >r.model

34

Review: Binary vs. Ternary Review: Binary vs. Ternary RelationshipsRelationships

• If each policy is

owned by just 1

employee:

– Key constraint

on Policies

would mean

policy can only

cover 1

dependent!

• What are the additional

constraints in the

2nd diagram?

agepname

DependentsCovers

name

Employees

ssn lot

Policies

policyid cost

Beneficiary

agepname

Dependents

policyid cost

Policies

Purchaser

name

Employees

ssn lot

Bad design

Better design

Page 35: Eer >r.model

35

Binary vs. Ternary Relationships Binary vs. Ternary Relationships (Contd.)(Contd.)

• The key constraints allow us to combine Purchaser with Policies and Beneficiary with Dependents.

• Participation constraints lead to NOT

NULL

constraints.

• What if Policies is a weak entity set?

CREATE TABLE Policies ( policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)

CREATE TABLE Dependents ( pname CHAR(20), age INTEGER, policyid INTEGER, PRIMARY KEY (pname, policyid). FOREIGN KEY (policyid) REFERENCES Policies, ON DELETE CASCADE)

Page 36: Eer >r.model

36

Page 37: Eer >r.model

37

Page 38: Eer >r.model

38

An ExampleAn Example

CREATE TABLE Student (ID NUMBER,Fname VARCHAR2(20),Lname VARCHAR2(20),

);

Page 39: Eer >r.model

39

Constraints in Create TableConstraints in Create Table

• Adding constraints to a table enables the database system to enforce data integrity.

• Different types of constraints:

* Not Null * Default Values

* Unique * Primary Key

* Foreign Key * Check Condition

Page 40: Eer >r.model

40

Not Null ConstraintNot Null Constraint

CREATE TABLE Student (ID NUMBER,Fname VARCHAR2(20) NOT NULL,Lname VARCHAR2(20) NOT NULL,

);

Page 41: Eer >r.model

41

Primary Key ConstraintPrimary Key Constraint

Primary Key implies: * NOT NULL * UNIQUE. There can only be one primary key.

CREATE TABLE Student (ID NUMBER PRIMARY KEY,Fname VARCHAR2(20) NOT NULL,Lname VARCHAR2(20) NOT NULL,

);

Page 42: Eer >r.model

42

Primary Key Constraint Primary Key Constraint (Syntax 2)(Syntax 2)

CREATE TABLE Students (ID NUMBER,Fname VARCHAR2(20) NOT NULL,Lname VARCHAR2(20) NOT NULL,PRIMARY KEY(ID)

);

Needed when the primary key is made up of two or more fields

Page 43: Eer >r.model

43

Another TableAnother Table

CREATE TABLE Studies(Course NUMBER,Student NUMBER

);

What additional constraint do we want on Student?

What should be the primary key?

Page 44: Eer >r.model

44

Foreign Key ConstraintForeign Key Constraint

NOTE: ID must be unique (or primary key) in Student

CREATE TABLE Studies(Course NUMBER,Student NUMBER,FOREIGN KEY (Student) REFERENCES

Students(ID));

Page 45: Eer >r.model

45

Translating ER-Diagrams to Translating ER-Diagrams to Table DefinitionsTable Definitions

Page 46: Eer >r.model

46

Relations vs. TablesRelations vs. Tables

• We show how to translate ER-Diagrams to table definitions

• Sometimes, people translate ER-Diagrams to relation definitions, which is more abstract than table definitions.– e.g., Student(ID, Fname, Lname);

– table definitions contain, in addition, constraints and datatypes

Page 47: Eer >r.model

47

Translating EntitiesTranslating Entities

General Rule:

• Create a table with the name of the Entity.

• There is a column for each attribute

• The key in the diagram is the primary key of the table

Actorid

name address

birthday

Page 48: Eer >r.model

48

Translating EntitiesTranslating Entities

create table Actor(id varchar2(20) primary key,

name varchar2(40),

birthday date,

address varchar2(100));

Actorid

name address

birthday

Relation: Actor (id, name, birthday, address)

Page 49: Eer >r.model

49

Translating Relationships Translating Relationships (without constraints)(without constraints)

General Rule:

• Create a table with the name of the relationship

• The table has columns for all of the relationship's attributes and for the keys of each entity participating in the relationship

• What is the primary key of the table?

• What foreign keys are needed?

Actorid

name

address

birthday

Acted In

Film

title

type

year

salary

Page 50: Eer >r.model

50

Translating relationships Translating relationships (without constraints)(without constraints)

What would be the relation for ActedIn?

How would you define the table for ActedIn?

Actorid

name

address

birthday

Acted In

Film

title

type

year

salary

Page 51: Eer >r.model

51

Translating Recursive Translating Recursive Relationships Relationships

(without constraints)(without constraints)

Employeeid

name

address

Manages

manager

worker

Relation: Actor (worker-id, manager-id)

What would be the table definition?

Page 52: Eer >r.model

52

Translating relationshipsTranslating relationships(key constraints): Option 1(key constraints): Option 1

General Rule for Option 1:

• Same as without key constraints, except that

the primary key is defined differently

Directorid

name

Directed Film title

salary year

Page 53: Eer >r.model

53

Translating relationshipsTranslating relationships(key constraints): Option 1(key constraints): Option 1

create table Directed(

id varchar2(20),

title varchar2(40),

salary integer,

)

Directorid

name

Directed Film title

salary year

What primary and foreign keys are missing?

Page 54: Eer >r.model

54

Translating relationshipsTranslating relationships(key constraints): Option 2(key constraints): Option 2

General Rule for Option 2:• Do not create a table for the relationship

• Add information columns that would have been in the relationship's table to the table of the entity with the key constraint

• What is the disadvantage of this method?

• What is the advantage of this method?

Directorid

name

Directed Film title

salary year

Page 55: Eer >r.model

55

Translating relationshipsTranslating relationships(key constraints): Option 2(key constraints): Option 2

create table Film(

title varchar2(40),

year integer,

primary key (title),

)

Directorid

name

Directed Film title

salary year

What 3 lines are missing?

Page 56: Eer >r.model

56

Translating relationshipsTranslating relationships(key constraints)(key constraints)

• What are the different options for

translating this diagram?

A R B

C

Page 57: Eer >r.model

57

Translating relationshipsTranslating relationships(participation constraints)(participation constraints)

General Rule:

• If has both participation and key constraint, use

Option 2 from before.

• Add the not null constraint to ensure that there will

always be values for the key of the other entity

Directorid

name

Directed Film title

salary year

Page 58: Eer >r.model

58

Translating relationshipsTranslating relationships(participation constraints)(participation constraints)

create table Film(

title varchar2(40),

year integer,

id varchar2(20),

salary integer,

foreign key (id) references Director,

primary key (title))

Directorid

name

Directed Film title

salary year

Where should we add NOT NULL?

Page 59: Eer >r.model

59

Translating relationshipsTranslating relationships(participation constraints)(participation constraints)

• How would we translate this?

Actorid

name

Acted In

Film title

salary year

Page 60: Eer >r.model

60

Translating Weak Entity Translating Weak Entity SetsSets

create table award(

name varchar2(40),

year integer,

money number(6,2),

o_name varchar2(40),

primary key(name, year, o_name),

foreign key (o_name) references Organization(name)

on delete cascade

)

Award

Organization

Gives

year

name

name

phonenumber

money

Page 61: Eer >r.model

61

Translating ISA:Translating ISA:Option 1Option 1

create table MoviePerson( ... )

create table Actor(id varchar2(20),

picture bfile,

primary key(id),

foreign key (id) references MoviePerson))

create table Director(...)

Movie Person

ISA

id

name

address

picture DirectorActor

Page 62: Eer >r.model

62

Translating ISA:Translating ISA:Option 2Option 2

No table for MoviePerson!

create table Actor(id varchar2(20),

address varchar2(100),

name varchar2(20),

picture blob,

primary key(id));

create table Director(...)

Movie Person

ISA

id

name

address

picture DirectorActor

Page 63: Eer >r.model

63

Which Option To Choose?Which Option To Choose?

• What would you choose if:

– Actor and Director DO NOT COVER

MoviePerson?

– Actor OVERLAPS Director?

Page 64: Eer >r.model

64

Translating Translating AggregationAggregation

• Create table for Won using:– key of ActedIn

– key of Award (careful, award is a weak entity)

Actorpicture

Filmyear

typetitle

Acted Insalary

Award

Organization

Gives

year

name

name

phonenumber

Won