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 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 StudentIdNameRegisteredCounsellorNoRegion s01Akeroyd s02Thompson s05Ellis s07Gillies s09Reeves s10Urbach relation a tuple primary key degree (5) attributes cardinality (6) relation name 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 CourseSession Equipment Entity types Staff(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 CourseSession Equipment Organises Manages Has Enrols Uses Requires LocatedIn MayBe Entity types Staff(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, R 2, whose values are the same as values of a primary key of some relation, R 1 (where R 1 and R 2 are not necessarily distinct).
Translating an ER-Diagram into a Relational Model Declare relations Identify primary keys. Declare domain Declare 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 Relations relation Staff StaffNo : StaffIdentifiers Name : NamesOfPeople ManagerStaffNo : StaffIdentifiers CourseCode : CourseIdentifiers StudentId : StudentIdentifiers primary key StaffNo relation Student StudentId : StudentIdentifiers Name : NamesOfPeople CourseCode : CourseIdentifiers primary key StudentId relation Course CourseCode : CourseIdentifiers Name : NamesOfCourses OrganiserStaffNo : StaffIdentifiers primary key CourseCode relation Session CourseCode : CourseIdentifiers SessionNo : SessionIdentifiers primary key (CourseCode, SessionNo) relation Room RoomNo : RoomIdentifiers Capacity : RoomCapacity primary key RoomNo relation Equipment InventoryNo : EquipmentIdentifiers Description : DescriptionOfEquipment RoomNo : RoomIdentifiers primary key InventoryNo
Step 2 - Declare Domain model TrainingCentre domains CourseIdentifiers = c1..c9 DescriptionOfEquipment = string EquipmentIdentifiers = NamesOfCourses = string NamesOfPeople = string RoomIdentifiers = r01..r99 SessionIdentifiers = 1..8 StaffIdentifiers = StudentIdentifiers = RoomCapacity =
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 CourseSession Organises Manages Has Enrols MayBe relation Session CourseCode : CourseIdentifiers SessionNo : SessionIdentifiers primary key (CourseCode, SessionNo) {relationship Has} foreign key CourseCode references Course
1 (Mandatory) : N (Optional) Relationship Staff Student Teaches CourseSession Organises Manages Has Enrols MayBe relation Staff StaffNo : StaffIdentifiers Name : NamesOfPeople ManagerStaffNo : StaffIdentifiers CourseCode : CourseIdentifiers StudentId : StudentIdentifiers primary key StaffNo {relationship Teaches} foreign key CourseCode references Course allowed null
1 (Optional) : N (Mandatory) Relationship Staff Student Teaches CourseSession Organises Manages Has Enrols MayBe relation Course CourseCode : CourseIdentifiers Name : NamesOfCourses OrganiserStaffNo : StaffIdentifiers primary key CourseCode {relationship Organises} foreign key OrganiserStaffNo references Staff not allowed null
1 (Optional) : N (Optional) Relationship Staff Student Teaches CourseSession Organises Manages Has Enrols MayBe relation Staff StaffNo : StaffIdentifiers Name : NamesOfPeople ManagerStaffNo : StaffIdentifiers CourseCode : CourseIdentifiers StudentId : StudentIdentifiers primary 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 Student MayBe relation Staff StaffNo : StaffIdentifiers StudentId : StudentIdentifiers primary key StaffNo {relationship MayBe} alternate key StudentId foreign 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.
CourseCodeSessionNo c11 2 c21 2 RoomNoCapacity r140 r260 r3100 CourseCodeSessionNoRoomNoCapacity c11r140 c12r140 c21r260 c22r260 ??r3100 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.) Session Uses Room Session RoomUse follow must be mandatory For example, relation Use CourseCode : CourseIdentifiers SessionNo : SessionIdentifiers RoomNo : RoomIdentifiers primary 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 CourseSession Equipment Organises Manages Has LocatedIn MayBe Use Require Student
Relational Model of Training Centre - After Step 3 relation Staff StaffNo : StaffIdentifiers Name : NamesOfPeople ManagerStaffNo : StaffIdentifiers CourseCode : CourseIdentifiers StudentId : StudentIdentifiers primary key StaffNo {relationship Manages} foreign key ManagerStaffNo references Staff allowed null {relationship Teaches} foreign key CourseCode references Course allowed null {relationship MayBe} alternate key StudentId foreign key StudentId references Student allowed null
Relational Model of Training Centre - After Step 3 relation Student StudentId : StudentIdentifiers Name : NamesOfPeople primary key StudentId relation Course CourseCode : CourseIdentifiers Name : NamesOfCourses OrganiserStaffNo : StaffIdentifiers primary key CourseCode {relationship Organises} foreign key OrganiserStaffNo references Staff not allowed null relation Enrolment StudentId : StudentIdentifiers CourseCode : CourseIdentifiers primary key (StudentId, CourseCode) foreign key StudentId references Student foreign key CourseCode references Course
Relational Model of Training Centre - After Step 3 relation Session CourseCode : CourseIdentifiers SessionNo : SessionIdentifiers primary key (CourseCode, SessionNo) {relationship Has} foreign key CourseCode references Course relation Use CourseCode : CourseIdentifiers SessionNo : SessionIdentifiers RoomNo : RoomIdentifiers primary key (CourseCode, SessionNo, RoomNo) foreign key (CourseCode, SessionNo) references Session foreign key RoomNo references Room
Relational Model of Training Centre - After Step 3 relation Room RoomNo : RoomIdentifiers Capacity : RoomCapacity primary key RoomNo relation Requires CourseCode : CourseIdentifiers SessionNo : SessionIdentifiers InventoryNo : EquipmentIdentifiers primary key (CourseCode, SessionNo, InventoryNo) foreign key (CourseCode, SessionNo) references Session foreign key InventoryNo references Equipment relation Equipment InventoryNo : EquipmentIdentifiers Description : DescriptionOfEquipment RoomNo : RoomIdentifiers primary 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 R 2 has a foreign key, F, that references the primary key, P, in another relation, R 1, then ever R 2.F entry must either be a value equal to an R 1.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 domains CourseIdentifiers = c1..c9 DescriptionOfEquipment = string EquipmentIdentifiers = NamesOfCourses = string NamesOfPeople = string RoomIdentifiers = r01..r99 SessionIdentifiers = 1..8 StaffIdentifiers = StudentIdentifiers = RoomCapacity =
Relational Model of Training Centre relation Staff StaffNo : StaffIdentifiers Name : NamesOfPeople ManagerStaffNo : StaffIdentifiers CourseCode : CourseIdentifiers StudentId : StudentIdentifiers primary key StaffNo {relationship Manages} foreign key ManagerStaffNo references Staff allowed null {relationship Teaches} foreign key CourseCode references Course allowed null {relationship MayBe} alternate key StudentId foreign key StudentId references Student allowed null
Relational Model of Training Centre relation Course CourseCode : CourseIdentifiers Name : NamesOfCourses OrganiserStaffNo : StaffIdentifiers primary key CourseCode {relationship Organises} foreign key OrganiserStaffNo references Staff not allowed null constraint (project Course over CourseCode) difference (project Staff over CourseCode) is empty constraint (project Course over CourseCode) difference (project Session over CourseCode) is empty
Relational Model of Training Centre relation Student StudentId : StudentIdentifiers Name : NamesOfPeople CourseCode : CourseIdentifiers primary key StudentId {relationship Attends} foreign key CourseCode references Course not allowed null constraint (project Student over StudentId) difference (project Enrolment over StudentId) is empty relation Enrolment StudentId : StudentIdentifiers CourseCode : CourseIdentifiers primary key (StudentId, CourseCode) foreign key StudentId references Student foreign key CourseCode references Course
Relational Model of Training Centre relation Session CourseCode : CourseIdentifiers SessionNo : SessionIdentifiers primary 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 Use CourseCode : CourseIdentifiers SessionNo : SessionIdentifiers RoomNo : RoomIdentifiers primary key (CourseCode, SessionNo, RoomNo) foreign key (CourseCode, SessionNo) references Session foreign key RoomNo references Room
Relational Model of Training Centre relation Room RoomNo : RoomIdentifiers Capacity : RoomCapacity primary key RoomNo relation Requires CourseCode : CourseIdentifiers SessionNo : SessionIdentifiers InventoryNo : EquipmentIdentifiers primary key (CourseCode, SessionNo, InventoryNo) foreign key (CourseCode, SessionNo) references Session foreign key InventoryNo references Equipment relation Equipment InventoryNo : EquipmentIdentifiers Description : DescriptionOfEquipment RoomNo : RoomIdentifiers primary 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 where 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
Project Operator The general form of a project expression is:- project over Pick out the wanted attributes from a given relation. Can be though of ‘slicing’ a relation vertically. For example, project Course over Title, Credit
Join Operator The general form of a join expression is :- join and where = '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
Divide Operator The general form of a divide expression is :- divide by over 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 Studies AllCourses
Union Operator The general form of a union expression is :- union and 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 or (or to both). E.g. Region4Students union CourseCodec4Students
Union Operator Region4Students CourseCodec4Students
Venn Diagram
Intersection Operator The general form of a intersection expression is :- intersection The result of a intersection is a relation containing all the tuples belonging to both and. E.g. Region4Students intersection CourseCodec4Students
Intersection Operator Region4Students CourseCodec4Students
Venn Diagram
Difference Operator The general form of a difference expression is :- difference The result of a difference is a relation containing the tuples that belong to but do not belong to. E.g. Region4Students difference CourseCodec4Students
Difference Operator Region4Students CourseCodec4Students
Venn Diagram
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 AllRooms 2.project Uses over RoomNo giving RoomsUsed 3.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 LargeRoomsUsed 3.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