Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling.

Similar presentations


Presentation on theme: "Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling."— Presentation transcript:

1 Agenda  TMA01  M876 Block 2 Relational Theory

2 Data Modeling

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

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

5 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

6 Relationships  Binary  Recursive (Unary) Student Enrols Course Prerequisite

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

8 Relational Terminlogy StudentIdNameRegisteredCounsellorNoRegion s01Akeroyd199331583 s02Thompson199852124 s05Ellis199752124 s07Gillies199631583 s09Reeves199852124 s10Urbach199752124 relation a tuple primary key degree (5) attributes cardinality (6) relation name Student

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

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

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

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

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

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

15 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

16 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

17 Step 2 - Declare Domain model TrainingCentre domains CourseIdentifiers = c1..c9 DescriptionOfEquipment = string EquipmentIdentifiers = 1000..9999 NamesOfCourses = string NamesOfPeople = string RoomIdentifiers = r01..r99 SessionIdentifiers = 1..8 StaffIdentifiers = 1000..9999 StudentIdentifiers = 1000..9999 RoomCapacity = 1..9999

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

19 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

20 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

21 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

22 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

23 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  

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

25 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!

26 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

27 Revised E-R Diagram of Training Centre Staff Enrolment Teaches Room CourseSession Equipment Organises Manages Has LocatedIn MayBe Use Require Student

28 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

29 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

30 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

31 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

32 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

33 Relational Model of Training Centre model TrainingCentre domains CourseIdentifiers = c1..c9 DescriptionOfEquipment = string EquipmentIdentifiers = 1000..9999 NamesOfCourses = string NamesOfPeople = string RoomIdentifiers = r01..r99 SessionIdentifiers = 1..8 StaffIdentifiers = 1000..9999 StudentIdentifiers = 1000..9999 RoomCapacity = 1..9999

34 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

35 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

36 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

37 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

38 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

39 Manipulating Relations

40 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

41 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

42 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

43 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

44 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

45 Union Operator Region4Students CourseCodec4Students

46 Venn Diagram

47 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

48 Intersection Operator Region4Students CourseCodec4Students

49 Venn Diagram

50 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

51 Difference Operator Region4Students CourseCodec4Students

52 Venn Diagram

53 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

54 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

55 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


Download ppt "Agenda  TMA01  M876 Block 2 Relational Theory. Data Modeling."

Similar presentations


Ads by Google