55
Agenda TMA01 M876 Block 2 Relational Theory

Agenda TMA01 M876 Block 2 Relational Theory. Data Modeling

Embed Size (px)

Citation preview

Page 1: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

Agenda

TMA01 M876 Block 2 Relational Theory

Page 2: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

Data Modeling

Page 3: 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.

Page 4: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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.

Page 5: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 6: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

Relationships

Binary

Recursive(Unary)

Student Enrols Course

Course

Prerequisite

Page 7: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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)

Page 8: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 9: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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.

Page 10: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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.

Page 11: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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)

Page 12: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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.

Page 13: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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)

Page 14: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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).

Page 15: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 16: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 17: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

Step 2 - Declare Domain

model TrainingCentre

domainsCourseIdentifiers = c1..c9DescriptionOfEquipment = stringEquipmentIdentifiers = 1000..9999NamesOfCourses = stringNamesOfPeople = stringRoomIdentifiers = r01..r99SessionIdentifiers = 1..8StaffIdentifiers = 1000..9999StudentIdentifiers = 1000..9999RoomCapacity = 1..9999

Page 18: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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.

Page 19: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 20: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 21: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 22: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 23: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 24: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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.

Page 25: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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!

Page 26: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 27: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

Revised E-R Diagram of Training Centre

Staff

Enrolment

Teaches

Room

Course Session

Equipment

Organises

Manages

Has LocatedIn

MayBe

Use

RequireStudent

Page 28: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 29: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 30: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 31: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 32: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 33: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 34: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 35: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 36: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 37: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 38: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 39: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

Manipulating Relations

Page 40: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 41: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 42: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 43: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 44: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 45: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 46: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

Venn Diagram

<s02, …>

<s05, …> <s10, …>

<s01, …>

<s07, …>

<s09, …>

Page 47: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 48: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 49: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

Venn Diagram

<s02, …>

<s05, …> <s10, …>

<s01, …>

<s07, …>

<s09, …>

Page 50: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 51: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 52: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

Venn Diagram

<s02, …>

<s05, …> <s10, …>

<s01, …>

<s07, …>

<s09, …>

Page 53: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 54: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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

Page 55: Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling

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