0% found this document useful (0 votes)
27 views43 pages

Curs2 PDF

Uploaded by

VIRGIL SCRIPCARU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views43 pages

Curs2 PDF

Uploaded by

VIRGIL SCRIPCARU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

SELECT lecture

FROM Databases

WHERE INITCAP(chapter) = ‘Relational Database Design’,


AND topic = ’The Entity/Relationship Model’

Mihaela Elena Breabăn


© FII 2017-2018
Relational Database Design
Methodology
1. Requirements analysis

Requirements specs

2. Conceptual Modeling

independent of
E/R diagrams (UML)
physical
considerations
3. Logical modeling
Normalisation
Relational schema

4. Physical modeling tailored to a specific


Tuning RDBMS

2
Plan

 Schema design: motivation


 E/R schema design using Chen’s notation
 E/R concepts
 Modeling constraints
 Connection Traps
 E/R schema design in UML
 From E/R and/or UML to the relational schema

3 08:23
Schema design: motivation

 Several schemas may be designed for a database


 Some are (much) better than others
 redundancy?
 efficiency?
 consistency?

 How generate good schemas?

 Two design approaches:


 E/R data modeling (Chen,’76)
 Schema decomposition - normalization (Codd, ’70-’74)
 Usually they are applied subsequently

4 08:28
Basic E/R concepts
(Chen 1976)
 Entity
 Data that can be modeled as an object having independent existence
 An entity type groups objects having the same properties; each entity type is described by a name and a
list of properties; each object in the group will be called an entity (occurrence/instance)
 Any entity must be uniquely identifiable within its class

 Relationship
 Models the interaction/association between entities
 A relationship type is a set of associations among entities types; each relationship type is given a
(descriptive) name
 A relationship (occurrence/instance) is uniquely identifiable by the participating entities
 Degree of a relationship type = the number of entity types involved
 binary, ternary…
 A recursive relationship type involves one entity type more than once, playing distinct roles

 Attributes
 For entity types these are specific properties describing the independent objects
 For association types these may be
 Properties of the involved entity types
 Specific properties of the relationship type, storing new information related to the association

5 08:28
E/R diagrams

 Graphical notation for E/R concepts


 There exist several graphical standards, we will use for the moment
Chen’s notation:

Entity type Relationship


type
Attribute

 E/R diagram = a graph where


 Entity types, relationship types and attributes are vertices/nodes
 Edges connect
 Entity type nodes with relationship types nodes
 Entity type nodes with attribute nodes
 Relationship type nodes with attribute nodes
6 08:28
Example

 Let’s design a database to store information about


 Students Binary relationship

 Instructors
STUDENTS MENTORS
 Courses
 Grades
 Mentors GRADES INSTRUCTORS

Ternary relationship
Recursive relationship
PRE COURSES

 Requirements:
 We can determine any student’s grades for the courses he/she attended and
the accumulated credits
 We can determine a student’s mentor
 We store the prerequisites for each course
7 08:31
More E/R concepts

 Roles
 Explain the function of the entity type in an association

rol

 Primary key
 The minimal set of attributes that is designated to uniquely identify an entity instance or a
relationship instance
 It is mandatory for entity types in order to identify the entity instances that are involved in
relationships
Primary_key

 Foreign key – defined only for relationships!


 A set of attributes that plays as primary key for the involved entity types
8 08:39
Example

CNP fName lName address

STUDENTS MENTORS
advised by
evaluated
advisor
sem

year GRADES INSTRUCTORS


evaluator
grade
CNP fName lName office
subject
Is necessary

PRE COURSES

necesitates
code title credits

Which are the foreign keys for our relationship types?


9 08:41
Multiplicity constraints

 The E/R model allows us to declare constraints on the number of


relationship instances to which an entity instance may participate

 Let R be a relationship type among n entity-types denoted Ei, i=1..n. The


database satisfies the (Ei, u,v,R) constraint if each entity instance of Ei
participates in at least u and most v relationship instances from R.

u:v

10 08:44
Example

 (Students1,1Mentors)
 (Instructors,0,7,Mentors)
 Interpretation: Every student has exactly one instructor as advisor/mentor
and an instructor can advise at most 7 students

CNP fName lName address

STUDENTS MENTORS
advised
1:1
advisor
0:7
INSTRUCTORS

CNP fName lName office

11 08:48
Multiplicity constraints for binary
relationships (1)

a) One-to-one relationship b) One-to-many relationship


(A,0,1,R) (B,0,1,R) (A,0,n,R) (B,0,1,R), n>1

A 0:1 R 0:1 B A 0:n R 0:1 B

12 08:49
Multiplicity constraints for binary
relationships (2)

a) Many-to-one relationship b) Many-to-many relationship


(A,0,1,R) (B,0,n,R) (A,0,m,R) (B,0,n,R), m,n>1

A 0:1 R 0:n B A 0:m R 0:n B

13 08:49
Weak entity type

 An entity type is said to be weak if its instances depend to some extent on


the existence of entities from other entity type (existential dependence)

Strong entity type

0:n

1:1
Weak entity type

 Has no key
 Must satisfy the multiplicity constraint (Weak-entity,1,1,R), participating in a
one-to-one or one-to-many relationship with respect to the strong entity

14 08:53
Weak entity - example

STUDENTS

0:10
EARN

1:1
SCHOLARSHIPS

15 08:53
Connection traps
(Fan traps)
INSTRUCTOR INSTRUCTOR

works for works for

EMPLOYEES
EMPLOYEES

has has

UNIVERSITY FACULTALTY

composed of components of

STRUCTURE
STRUCTURE

components of composed of
composition
FACULTATY (special type of relationship) UNIVERSITY

Problem: Solution:
In which department profesor X is working? Restructured model
16 22:27
Connection traps
(Chasm traps)

holds
FACULTY FACULTY
offers offers

CURRICULUM CURRICULUM

offered by offered by
COURSE RESOURCE COURSE
activity activity

TIMETABLE TIMETABLE

location location
CLASSROOM property of CLASSROOM

Problem: Solution:
Which are the classrooms belonging to some faculty? New relationships
17 22:27
Enhancements of the E/R model
Specializations

 Subgroups of entity instances


 Having new distinctive attributes or
 Participating in relationships not common to all entity instances
 Correspond to a specialized entity type which is involved in a IS-A relationship
type relative to the basic entity type

STUDENTS

IS A

FOREIGN
STUDENTS

18 22:27
Specialization constraints

 Instances of the specializations inherit all the attributes and the


relationships of the basic entity type, including the key

 An instance of an entity type may belong only to one or to several


specializations:
 Disjoint specializations (exclusive)
IS A {disjoint/overlapped}
 Overlapped specializations

 An instance of an entity type must or must not belong to at least one


specialization:
 Complete specializations {complete/partial}
IS A
 Incomplete (partial) specializations

19 22:27
UML Modeling

 Unified Modeling Language


 Extensively used in software engineering
 Based on object-oriented concepts
 A communication tool with clients, in company-specific terms
 A large language from which we use a small set of elements
(class diagrams) to model a database.

20 22:27
E/R – UML Correspondence

E/R UML
Entity type Class
Relationship type NOT having Association
own attributes
Relationship type with own Association class
attributes
Specialization Subclass
Composition and
Aggregation

21 22:27
Classes

 Components: names, attributes, methods


 DB: name, attribute (primary key)

CNP fName lName address

STUDENTS

22 22:27
Associations

 Express the associations between objects belonging to two classes


 BD: relationship types among entity types

CNP fName lName address CNP fName lName office

STUDENTS advised by MENTORS advisor INSTRUCTORS


1:1 0:7

 Obs: multiplicity constraints are specified in reverse order compared to E/R


diagrams

23 22:27
Associations
Multiplicity constraints
 Constraints
u:v x:y
 (C1,u,v,A) C1 A C2
 (C2,x,y,A)

 Every entity in C1 is associated to at least u and at most v entities in C2


 Every entity in C2 is associated to at least x and at most y entities in) C1

x..y u..v Relationship


0..1 0..1 One-to-one incomplete
1..1 (1) 1..1 (1) One-to-one complete (the default)
0..1 0..* (*) One-to-many incomplete
… … …
24 22:27
Quiz

1. Model the relationship between STUDENTS and UNIVERSITIES. A


student may study to at most 2 universities and must study to at least
one. A university may have at most 10.000 students.

2. Given the relationship

2..4 12..14

What is the minimum number of entities in C1?


What about C2?

25 22:27
Recursive relationships

requires

COURSES PRE

is required by
code title credits

26 22:27
N-ary relationships

27 22:27
Association classes

STUDENTS GRAD_THESIS INSTRUCTORS

title session

28 22:27
Association classes

STUDENTS

COURSES GRADES INSTRUCTORS


sem year grade

29 22:27
Eliminating association classes

 When we have 0..1 or 1..1 multiplicity:

30 22:27
Subclasses
(1)
ID fName lName

STUDENTS

superclass
IS A IS A

ROMANIAN
FOREIGN STUDENTS
STUDENTS

CNP country

Complete, disjoint specialization

31 22:27
Subclass
(2)

Complete overlapping specialization

32 22:27
Compositions and aggregation

 Entities of a type are parts of entities of another type


 Special cases of relationships

 Composition: all entities of a partial class belong to entities of the


composed class; a partial class usually corresponds to a weak entity type
(multiplicity 1..1; no primary key);
 Aggregation: some entities of an entity type belong to entities of another
entity type(multiplicity 0..1)
33 22:27
Transforming E/R, UML into
relational schemas
E/R UML Relational schema
Entity type Class Relation with primary
key
Relationship NOT having own Association Relation with foreign
attributes keys
Relationship having own attributes Associations class Relation with foreign
keys and other
attributes
Specialization Subclass Relation with primary
key (from superclass)
and specialized
attributes
Compositions and Relation with foreign key
aggregation and own attributes

34 22:27
Entity types and relationships

{C1(K1, X1), C2(K2, X2), A(K1,K2)}

 The primary key of a relationship depends on multiplicity:

x..y u..v Primary key for A Observations


0..1 * K2 There’s no need for relation A
1..1 {C1(K1, X1), C2(K2, X2,K1)}
* 0..1 K1 There’s no need for relation A
1..1 {C1(K1, X1,K2), C2(K2, X2)}
* * (K1,K2)

35 22:27
Quiz

 For the E/R diagram below

Is it possible to eliminate the relation corresponding to the relationship?

36 22:27
Recursive relationships

{COURSES (code, title, credits) {UNIVERSITIES (code, uName,address)


PRE (code1, code2)} BRANCHES (subCode, mainCode)}

37 22:27
Association classes

{STUDENTS (ID, fName, lName)


UNIVERSITIES (code, uName, address)
APPLY (ID, code, date, result)}

38 22:27
Specializations/Subclasses

 Choices:
1. Superclass relation + subclass relations containing foreign key and specialized
attributes
 C1(K1,X1), C2(K1,X2), C3(K1,X3)
2. Independent superclass relation + subclass relations containing both general
attributes and specialized attributes
 C1(K1,X1), C2(K2,X1,X2), C3(K2,X1,X3)
3. One relation containing everything
 C(K1,X1,X2,X3)

39 22:27
Quiz

 Let S be a superclass with a number of subclasses. Consider


that the specialization is incomplete and overlapping. If n1, n2
and n3 represent the total number of tuples which would be
generated for each of the three previous translation schemes,
which of the following is true?

o n1<n2<n3
o n1n2n3
o n3<n2<n1
o n3 n2 n1

40 22:27
Composition and aggregation

{ UNIVERSITIES(code, uName, address)


DEPARTMENTS(codeU, dName, building)
OFFICES (codeU, address)}
does NOT accept NULL

accepts NULL

41 22:27
ER / UML Modeling
Summary
 PROS
 Popular technique in conceptual modeling
 Constructions which allow us expressing our own personal point of
view on data/application
 Allows expressing some classes of constraints (primary keys, foreign
keys, multiplicity…)

 CONS
 Subjectivity (entity / attribute, entitity / relationship, subclass,
composition)
 Does not allow modeling all kinds of dependencies
 Necessitates future normalisation steps

42 22:27
Bibliography

 Chapters 11 and 12 in Thomas Connolly, Carolyn Begg: Database Systems: A


Practical Approach to Design, Implementation and Management, (5th edition)
Addison Wesley, 2009

 Tools:
 https://creately.com (ER diagrams, UML class diagrams)
 http://diagramo.com/ (ER diagrams)
 http://argouml-downloads.tigris.org/nonav/argouml-0.32.2/ArgoUML-0.32.2.zip
(UML class diagrams)

43 22:27

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy