Upload
bennett-poole
View
225
Download
1
Tags:
Embed Size (px)
Citation preview
Agenda
TMA01 M876 Block 2 Relational Theory
Data Modeling
Entity-Relationship (E-R) Modelling
A conceptual data model is produced after data analysis stage.
It models data to be stored in database and their inter-relationships.
E-R modelling expresses data requirements in terms of entity types, attributes of entity types and relationships between entity types.
E-R diagram is a graphical representation of an E-R model.
Fundamental Constructs of E-R Model
An entity represents a thing that has meaning in a given context and about which there is a need to record data, e.g. student, staff, course, etc..
Entity type defines the properties common to a collection of entities.
An attributes is a component of an entity type that represent a single property of entities of that type.
A relationship is an association of entities that has meaning in a given context and which needs to be recorded.
Relationships
Involves one, two, or more entities, and documents an association between these entities.
Must have a name and occasionally, may carry data.
Three types of relationships: binary recursive ternary
Relationships
Binary
Recursive(Unary)
Student Enrols Course
Course
Prerequisite
Degree of a Relationship
Used to document the number of occurrences of an entity that are associated with another entity.
One to One (1:1)
One to Many (1:N)
Many to Many (M:N)
Relational Terminlogy
StudentId Name Registered
CounsellorNo
Region
s01 Akeroyd 1993 3158 3
s02 Thompson
1998 5212 4
s05 Ellis 1997 5212 4
s07 Gillies 1996 3158 3
s09 Reeves 1998 5212 4
s10 Urbach 1997 5212 4
relation
a tuple
primarykey
degree (5)
attributes
cardinality(6)
relationname Student
Example – Training Centre
The administrator of an IT training centre needs to maintain data on each member of staff working for the centre, on each student enrolling a course at the centre, and on the rooms used and equipment required for each session of practical activities. Staff working at the centre carry out a number of tasks, including organizing and teaching courses and managing other staff. Some staff may be enrolling as students, since the company encourages staff to enrol on its courses as part of its staff development programme. Each course taught at the centre has eight sessions of practical activities. For each item of equipment it necessary to record the room in which it is used.
Find Entity Types and Attributes
The administrator of an IT training centre needs to maintain data on each member of staff working for the centre, on each student enrolling a course at the centre, and on the rooms used and equipment required for each session of practical activities. Staff working at the centre carry out a number of tasks, including organising and teaching courses and managing other staff. Some staff may be enrolling as students, since the company encourages staff to enrol on its courses as part of its staff development programme. Each course taught at the centre has eight sessions of practical activities. For each item of equipment it necessary to record the room in which it is used.
Entity Types of Training Centre
Staff
Student
Room
Course Session
Equipment
Entity typesStaff(StaffNo, Name)Student(StudentId, Name)Course(CourseCode, Name)Session(CourseCode, SessionNo)Room(RoomNo, Capacity)Equipment(InventoryNo, Description)
Find Relationships Between Entity Types
The administrator of an IT training centre needs to maintain data on each member of staff working for the centre, on each student enrolling a course at the centre, and on the rooms used and equipment required for each session of practical activities. Staff working at the centre carry out a number of tasks, including organising and teaching courses and managing other staff. Some staff may be enrolling as students, since the company encourages staff to enrol on its courses as part of its staff development programme. Each course taught at the centre has eight sessions of practical activities. For each item of equipment it necessary to record the room in which it is used.
E-R Model of Training Centre
Staff
Student
Teaches
Room
Course Session
Equipment
Organises
Manages
Has
Enrols
Uses
Requires
LocatedIn
MayBe
Entity typesStaff(StaffNo, Name)Student(StudentId, Name)Course(CourseCode, Name)Session(CourseCode, SessionNo)Room(RoomNo, Capacity)Equipment(InventoryNo, Description)
Relational Model A relational model is based on three constructs:
Relation Attribute Domain
A relation consists of a fixed set of attributes, each of which is defined on some underlying domain.
A domain is a named set of values. Attribute(s) is candidate key if and only if it has the
properties of uniqueness and minimality. Primary key of a relation is one particular key chosen
from the candidate keys. The remaining candidate keys become alternate keys.
Alternate key can have NULL value but NOT primary key. Foreign key is attribute(s) in one relation, R2, whose
values are the same as values of a primary key of some relation, R1 (where R1 and R2 are not necessarily distinct).
Translating an ER-Diagram into a Relational Model
Declare relations Identify primary keys.
Declare domainDeclare relationships
Represent 1:N relationships using foreign keys. Represent 1:1 relationships using foreign keys
and alternative keys. Represent M:N relationships using additional
relations.Declare constraints
Step 1 - Declare Relationsrelation Staff
StaffNo : StaffIdentifiersName : NamesOfPeopleManagerStaffNo : StaffIdentifiersCourseCode : CourseIdentifiersStudentId : StudentIdentifiersprimary key StaffNo
relation StudentStudentId : StudentIdentifiersName : NamesOfPeopleCourseCode : CourseIdentifiersprimary key StudentId
relation CourseCourseCode : CourseIdentifiersName : NamesOfCoursesOrganiserStaffNo : StaffIdentifiersprimary key CourseCode
relation SessionCourseCode : CourseIdentifiersSessionNo : SessionIdentifiersprimary key (CourseCode, SessionNo)
relation RoomRoomNo : RoomIdentifiersCapacity : RoomCapacityprimary key RoomNo
relation EquipmentInventoryNo : EquipmentIdentifiersDescription : DescriptionOfEquipmentRoomNo : RoomIdentifiersprimary key InventoryNo
Step 2 - Declare Domain
model TrainingCentre
domainsCourseIdentifiers = c1..c9DescriptionOfEquipment = stringEquipmentIdentifiers = 1000..9999NamesOfCourses = stringNamesOfPeople = stringRoomIdentifiers = r01..r99SessionIdentifiers = 1..8StaffIdentifiers = 1000..9999StudentIdentifiers = 1000..9999RoomCapacity = 1..9999
Step 3 – Declare 1:N Relationships Between Relations
Use primary key/foreign key mechanism. Include the primary key of the relation on the
1: side of the relationship to the relation on the :N side.
1 (Mandatory) : N (Mandatory) Relationship
Staff
Student
Teaches
Course SessionOrganises
Manages
Has
Enrols
MayBe
relation SessionCourseCode : CourseIdentifiersSessionNo : SessionIdentifiersprimary key (CourseCode, SessionNo){relationship Has}foreign key CourseCode references
Course
1 (Mandatory) : N (Optional) Relationship
Staff
Student
Teaches
Course SessionOrganises
Manages
Has
Enrols
MayBe
relation StaffStaffNo : StaffIdentifiersName : NamesOfPeopleManagerStaffNo : StaffIdentifiersCourseCode : CourseIdentifiersStudentId : StudentIdentifiersprimary key StaffNo {relationship Teaches}foreign key CourseCode references Course allowed null
1 (Optional) : N (Mandatory) Relationship
Staff
Student
Teaches
Course SessionOrganises
Manages
Has
Enrols
MayBe
relation CourseCourseCode : CourseIdentifiersName : NamesOfCoursesOrganiserStaffNo : StaffIdentifiersprimary key CourseCode{relationship Organises}foreign key OrganiserStaffNo references Staff not allowed null
1 (Optional) : N (Optional) Relationship
Staff
Student
Teaches
Course SessionOrganises
Manages
Has
Enrols
MayBe
relation StaffStaffNo : StaffIdentifiersName : NamesOfPeopleManagerStaffNo : StaffIdentifiersCourseCode : CourseIdentifiersStudentId : StudentIdentifiersprimary key StaffNo{relationship Manages}foreign key ManagerStaffNo references Staff allowed null
Step 3 – Declare 1:1 Relationships Between Relations
Two steps involved Decide which one of the two relations will
have the attribute declared as foreign key. Declare the foreign key to be an alternate
key.Staff StudentMayBe
relation StaffStaffNo : StaffIdentifiers
StudentId : StudentIdentifiersprimary key StaffNo
{relationship MayBe}alternate key StudentIdforeign key StudentId references Student allowed null
Step 3 – Declare M:N Relationships Between Relations
The primary key/foreign key mechanism CANNOT be used to represent a M:N relationship. (Why?).
Use an intersection relation to represent a M:N relationship. Its primary key consists of the combination of the primary keys of the two other relations.
In other words, the M:N relationship is decomposed into two 1:N relationships.
CourseCode SessionNo
c1 1
c1 2
c2 1
c2 2
RoomNo
Capacity
r1 40
r2 60
r3 100
CourseCode SessionNo
RoomNo Capacity
c1 1 r1 40
c1 2 r1 40
c2 1 r2 60
c2 2 r2 60
? ? r3 100
Include the primary key of the session relation to the primary key the room relation
Why Primary Key/Foreign Key Mechanism CANNOT Be Used To Represent M:N Relationship?Session Room
Primary key CANNOT be NULL!
Step 3 – Declare M:N Relationships Between Relations (Cont.)
SessionUses
Room
Session RoomUse
follow followmust be mandatory
For example,
relation UseCourseCode : CourseIdentifiersSessionNo : SessionIdentifiersRoomNo : RoomIdentifiersprimary key (CourseCode, SessionNo, RoomNo)foreign key (CourseCode, SessionNo) references
Session foreign key RoomNo references Room
Revised E-R Diagram of Training Centre
Staff
Enrolment
Teaches
Room
Course Session
Equipment
Organises
Manages
Has LocatedIn
MayBe
Use
RequireStudent
Relational Model of Training Centre - After Step 3
relation StaffStaffNo : StaffIdentifiersName : NamesOfPeopleManagerStaffNo : StaffIdentifiersCourseCode : CourseIdentifiersStudentId : StudentIdentifiersprimary key StaffNo{relationship Manages}foreign key ManagerStaffNo references Staff allowed null{relationship Teaches}foreign key CourseCode references Course allowed null{relationship MayBe}alternate key StudentIdforeign key StudentId references Student allowed null
Relational Model of Training Centre - After Step 3
relation StudentStudentId : StudentIdentifiersName : NamesOfPeopleprimary key StudentId
relation CourseCourseCode : CourseIdentifiersName : NamesOfCoursesOrganiserStaffNo : StaffIdentifiersprimary key CourseCode{relationship Organises}foreign key OrganiserStaffNo references Staff not allowed null
relation EnrolmentStudentId : StudentIdentifiersCourseCode : CourseIdentifiersprimary key (StudentId, CourseCode)foreign key StudentId references Student foreign key CourseCode references Course
Relational Model of Training Centre - After Step 3
relation SessionCourseCode : CourseIdentifiersSessionNo : SessionIdentifiersprimary key (CourseCode, SessionNo){relationship Has}foreign key CourseCode references Course
relation UseCourseCode : CourseIdentifiersSessionNo : SessionIdentifiersRoomNo : RoomIdentifiersprimary key (CourseCode, SessionNo, RoomNo)foreign key (CourseCode, SessionNo) references Session foreign key RoomNo references Room
Relational Model of Training Centre - After Step 3
relation RoomRoomNo : RoomIdentifiersCapacity : RoomCapacityprimary key RoomNo
relation RequiresCourseCode : CourseIdentifiersSessionNo : SessionIdentifiersInventoryNo : EquipmentIdentifiersprimary key (CourseCode, SessionNo, InventoryNo)foreign key (CourseCode, SessionNo) references Session foreign key InventoryNo references Equipment
relation EquipmentInventoryNo : EquipmentIdentifiersDescription : DescriptionOfEquipmentRoomNo : RoomIdentifiersprimary key InventoryNo{relationship LocatedIn}foreign key RoomNo references Room not allowed null
Step 4 – Declare Constraints Key Constraints
Primary keys Alternative keys Foreign keys and referential integrity
If a relation R2 has a foreign key, F, that references the primary key, P, in another relation, R1, then ever R2.F entry must either be a value equal to an R1.P primary key value or be null.
Attribute Constraints Allowing NULL Attribute conditions, e.g. constraint today – DOB > 18
General Constraints Mandatory participation conditions
:1 side - Use relational algebra :n side - Use NOT NULL
Inclusivity/exclusivity conditions
Relational Model of Training Centre
model TrainingCentre
domainsCourseIdentifiers = c1..c9DescriptionOfEquipment = stringEquipmentIdentifiers = 1000..9999NamesOfCourses = stringNamesOfPeople = stringRoomIdentifiers = r01..r99SessionIdentifiers = 1..8StaffIdentifiers = 1000..9999StudentIdentifiers = 1000..9999RoomCapacity = 1..9999
Relational Model of Training Centre
relation StaffStaffNo : StaffIdentifiersName : NamesOfPeopleManagerStaffNo : StaffIdentifiersCourseCode : CourseIdentifiersStudentId : StudentIdentifiersprimary key StaffNo{relationship Manages}foreign key ManagerStaffNo references Staff allowed null{relationship Teaches}foreign key CourseCode references Course allowed null{relationship MayBe}alternate key StudentIdforeign key StudentId references Student allowed null
Relational Model of Training Centre
relation CourseCourseCode : CourseIdentifiersName : NamesOfCoursesOrganiserStaffNo : StaffIdentifiersprimary key CourseCode{relationship Organises}foreign key OrganiserStaffNo references Staff not allowed nullconstraint (project Course over CourseCode) difference
(project Staff over CourseCode) is emptyconstraint (project Course over CourseCode) difference
(project Session over CourseCode) is empty
Relational Model of Training Centre
relation StudentStudentId : StudentIdentifiersName : NamesOfPeopleCourseCode : CourseIdentifiersprimary key StudentId{relationship Attends}foreign key CourseCode references Course not allowed nullconstraint (project Student over StudentId) difference (project Enrolment over StudentId) is empty
relation EnrolmentStudentId : StudentIdentifiersCourseCode : CourseIdentifiersprimary key (StudentId, CourseCode)foreign key StudentId references Studentforeign key CourseCode references Course
Relational Model of Training Centre
relation SessionCourseCode : CourseIdentifiersSessionNo : SessionIdentifiersprimary key (CourseCode, SessionNo){relationship Has}foreign key CourseCode references Course constraint (project Session over CourseCode, SessionNo) difference
(project Use over CourseCode, SessionNo) is empty
relation UseCourseCode : CourseIdentifiersSessionNo : SessionIdentifiersRoomNo : RoomIdentifiersprimary key (CourseCode, SessionNo, RoomNo)foreign key (CourseCode, SessionNo) references Session foreign key RoomNo references Room
Relational Model of Training Centrerelation Room
RoomNo : RoomIdentifiersCapacity : RoomCapacityprimary key RoomNo
relation RequiresCourseCode : CourseIdentifiersSessionNo : SessionIdentifiersInventoryNo : EquipmentIdentifiersprimary key (CourseCode, SessionNo, InventoryNo)foreign key (CourseCode, SessionNo) references Session foreign key InventoryNo references Equipment
relation EquipmentInventoryNo : EquipmentIdentifiersDescription : DescriptionOfEquipmentRoomNo : RoomIdentifiersprimary key InventoryNo{relationship LocatedIn}foreign key RoomNo references Room not allowed null
Manipulating Relations
Select Operator The general form of a select expression is:- select <relation> where <selection condition>
Produce a relation whose extension is a subset of the extension of a given relation, the content of the subset being determined by a selection condition.
Can be though of ‘slicing’ a relation horizontally. For example,
select Student where Registered > 1996
StudentId Name Registered CounsellorNo Region
s02 Thompson 1998 5212 4
s05 Ellis 1997 5212 4
s09 Reeves 1998 5212 4
s10 Urbach 1997 5212 4
Project Operator
The general form of a project expression is:- project <relation> over <attribute list>
Pick out the wanted attributes from a given relation.
Can be though of ‘slicing’ a relation vertically. For example,
project Course over Title, Credit
Title Credit
Syntax 30
Semantics 60
Pragmatics 30
Join Operator The general form of a join expression is :- join <relation1> and <relation2> where
<attribute1>=<attribute2> 'Paste' tuples of two relations together where the
condition in the join expression holds. Attributes in a condition must be defined on same
domain. The possible duplication of attribute names is a minor
problem as the result of a join (can be resolved by qualified attribute names).
E.g. join Student and Staff where CounsellorNo = StaffNo
StudentId Name Registered CounsellorNo Region Name Region
s01 Akeroyd 1993 3158 3 Jennings 3 s02 Thompson 1998 5212 4 Heathcote 4 s05 Ellis 1997 5212 4 Heathcote 4 s07 Gillies 1996 3158 3 Jennings 3 s09 Reeves 1998 5212 4 Heathcote 4 s10 Urbach 1997 5212 4 Heathcote 4
Divide Operator The general form of a divide expression is :- divide <dividend relation> by <divisor relation> over
<attribute>
Reduce a relation into a smaller relation, which contain only those tuples for which the second relation was a 'factor'.
E.g. divide Studies by AllCourses over CourseCode
StudentId CourseCodes01 c4s05 c2s05 c7s07 c4s09 c4s09 c2s09 c7S10 c7S10 c4
CourseCode
c2
c4
c7
StudentId
s09
Studies AllCourses
Union Operator
The general form of a union expression is :- <relation 1> union <relation 2>
<relation 1> and <relation 2> must be union-compatible.
Two relations are union-compatible only if They are of the same degree There is a one-to-one mapping between each attribute Corresponding attributes are defined on the same
domain The result of a union is a relation containing all the
tuples belonging to either <relation 1> or <relation 2> (or to both).
E.g. Region4Students union CourseCodec4Students
Union OperatorStudentId Name Registered CounsellorNo Region
s02 Thompson 1998 5212 4s05 Ellis 1997 5212 4s09 Reeves 1998 5212 4s10 Urbach 1997 5212 4
Region4Students
StudentId Name Registered CounsellorNo Region
s01 Akeroyd 1993 3158 3s07 Gillies 1996 3158 3s09 Reeves 1998 5212 4s10 Urbach 1997 5212 4
CourseCodec4Students
StudentId Name Registered CounsellorNo Region
s02 Thompson 1998 5212 4s05 Ellis 1997 5212 4s09 Reeves 1998 5212 4s10 Urbach 1997 5212 4s01 Akeroyd 1993 3158 3s07 Gillies 1996 3158 3
Venn Diagram
<s02, …>
<s05, …> <s10, …>
<s01, …>
<s07, …>
<s09, …>
Intersection Operator
The general form of a intersection expression is :- <relation 1> intersection <relation 2>
The result of a intersection is a relation containing all the tuples belonging to both <relation 1> and <relation 2>.
E.g. Region4Students intersection CourseCodec4Students
Intersection OperatorStudentId Name Registered CounsellorNo Region
s02 Thompson 1998 5212 4s05 Ellis 1997 5212 4s09 Reeves 1998 5212 4s10 Urbach 1997 5212 4
Region4Students
StudentId Name Registered CounsellorNo Region
s01 Akeroyd 1993 3158 3s07 Gillies 1996 3158 3s09 Reeves 1998 5212 4s10 Urbach 1997 5212 4
CourseCodec4Students
StudentId Name Registered CounsellorNo Region
s09 Reeves 1998 5212 4s10 Urbach 1997 5212 4
Venn Diagram
<s02, …>
<s05, …> <s10, …>
<s01, …>
<s07, …>
<s09, …>
Difference Operator
The general form of a difference expression is :- <relation 1> difference <relation 2>
The result of a difference is a relation containing the tuples that belong to <relation 1> but do not belong to <relation 2>.
E.g. Region4Students difference CourseCodec4Students
Difference OperatorStudentId Name Registered CounsellorNo Region
s02 Thompson 1998 5212 4s05 Ellis 1997 5212 4s09 Reeves 1998 5212 4s10 Urbach 1997 5212 4
Region4Students
StudentId Name Registered CounsellorNo Region
s01 Akeroyd 1993 3158 3s07 Gillies 1996 3158 3s09 Reeves 1998 5212 4s10 Urbach 1997 5212 4
CourseCodec4Students
StudentId Name Registered CounsellorNo Region
s02 Thompson 1998 5212 4s05 Ellis 1997 5212 4
Venn Diagram
<s02, …>
<s05, …> <s10, …>
<s01, …>
<s07, …>
<s09, …>
Example 1
Produce a relation containing the identifiers of rooms that are not used for any of the practical sessions.
1. project Room over RoomNo giving AllRooms2. project Uses over RoomNo giving RoomsUsed3. AllRooms difference RoomsUsed giving Answer OR(project Room over RoomNo) difference (project Uses over RoomNo)giving Answer
Example 2 Produce a relation containing the identifiers of
rooms that are used for at least one practical session and can accommodate at least ten students.1. join Uses and Room where Uses.RoomNo
= Room.RoomNo giving RoomsUsed 2. select RoomsUsed where Capacity >= 10
giving LargeRoomsUsed3. project LargeRoomsUsed over RoomNo
giving Answer OR
project ( select ( join Uses and Room where Uses.RoomNo = Room.RoomNo) where Capacity >= 10) over RoomNo giving Answer
Example 3
Staff attending a course at the centre as students must not be involved in the organization or teaching of that course. Write a query that check this rule and give empty relation if it has not been broken.
1. join Staff and Student where Staff.StudentId = Student.StudentId giving StaffAsStudents
2. project StaffAsStudents over StaffNo, Student.CourseCode giving Students
3. project Staff over StaffNo, CourseCode giving Teachers
4. project Course over OrganizerStaffNo, CourseCode giving CourseManagers
5. Teachers union CourseManagers giving Teachers&CourseManagers
6. Students interaction Teachers&CourseManagers giving Answer