Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is a Relation ? To consider how Set Theory defines the structure of relations; To consider how relations can be exploited to provide the equivalent.

Similar presentations


Presentation on theme: "What is a Relation ? To consider how Set Theory defines the structure of relations; To consider how relations can be exploited to provide the equivalent."— Presentation transcript:

1 What is a Relation ? To consider how Set Theory defines the structure of relations; To consider how relations can be exploited to provide the equivalent of bags of duplicates & ordered sequences. Objectives of the Lecture :

2 Set Theory l The relational model is based on mathematical sets. Definition : “A set is a collection of things”. l A set has two main characteristics : l It has no sequence or structure. l It has no duplicates. Example :{ 2, 7, 5, 4 }  { 7, 5, 4, 2 } They are the same set because sets have no order. Example : { 2, 2, 7, 5, 4, 8, 8 } is not a set because it has duplicates. l Note. A ‘set’ whose values are ordered is a Sequence. A ‘set’ with duplicates is a Bag (or Multiset).

3 Relation = Set of Tuples Hence the two relations below are the same, even though their tuples appear in a different order. 33E177Robson66D6632,500 444E6 E8 888Blake Jones 888M W 88854,000 68,000 1 2 ENo 1 2 1E3 E5 5 6 EName 5 6 5Smith Mitchell 2 4 M-S 2 4 2S M 2 4 Sal 2 4 212,500 21,000 22E566Mitchell44M4421,000 444E6888Blake888M88854,000 E8JonesW68,000 111E3555Smith222S22212,500 33E177Robson66D6632,500 ENoENameM-SSal Definition : a relation is a set of tuples. Neither version of the relation has any particular rhyme or reason for the ordering of its tuples.

4 Relation - No Duplicate Tuples This cannot be a relation as some tuples appear more than once. 22E566Mitchell44M4421,000 33E177Robson66D6632,500 444E6888Blake888M88854,000 E8JonesW68,000 111E3555Smith222S22212,500 ENoENameM-SSal 111E3555Smith222S22212,500 444E6888Blake888M88854,000 444E6888Blake888M88854,000 2 Smiths 3 Blakes

5 Storing a Bag in a Relation Example : a library holds information about books, but it often has many copies of the same book. 5 6 7 8 Title 7 5 6 8 5Computing Relational DBMS Nuclear Physics 8Genetics Solar Energy 2 4 6 8 Author 6 2 4 8 2Smith & Jones C J Date D Dewhirst 8M Platt Will & Smythe 2 4 6 8 No. 6 2 4 8 212 21 5 84 6 The number of copies of the book. Data about the books.

6 Handling a Sequence with a Relation Although the DBMS presents a relation to the user as a set of tuples, we can retrieve a relation in any tuple sequence which depends on the values in the tuples. 111E3555Smith222S22212,500 22E566Mitchell44M4421,000 33E177Robson66D6632,500 444E6888Blake888M88854,000 E8JonesW68,000 ENoENameM-SSal Sort sequence : ascending order of ‘Eno’

7 A ‘Sequenced’ Relation (1) 111E3555Smith222S22212,500 22E566Mitchell44M4421,000 33E177Robson66D6632,500 444E6888Blake888M88854,000 E8JonesW68,000 ENoENameM-SSal Sort sequence : ascending order of ‘Sal’ 111E3555Smith222S22212,500 22E566Mitchell44M4421,000 33E177Robson66D6632,500 444E6888Blake888M88854,000 E8JonesW68,000 ENoENameM-SSal Sort sequence : descending order of ‘Sal’ Same relation, in 2 different sequences.

8 A ‘Sequenced’ Relation (2) 111E3555Smith222S22212,500 22E566Mitchell44M4421,000 33E177Robson66D6632,500 444E6888Blake888M88854,000 E8JonesW68,000 ENoENameM-SSal Sort sequence : ascending order of ‘EName’ within ascending ‘M-S’

9 Sorting a Relation into Sequences Summary.A relation is sorted on the values of one or more of its attributes. l Thus any sort attribute must contain orderable data. Examples.Numbers have a natural sequence. Text can be sorted alphabetically. Photos : there is no agreed way of sorting them. l Any orderable attribute’s data can be sorted into ascending or descending order. l If more than one attribute is used for sorting, a major to minor order must be specified.

10 SQL : Retrieving a Sequenced Relation To retrieve the relation EMPLOYEE in order of increasing employee salary : Select * From EMPLOYEE Order By Sal Asc ; Keyword Select indicates : Do a retrieval. The attributes to get : * means all attributes. Keyword From indicates the relation to use. Keyword Order By indicates the sequencing. Asc indicates ascending.

11 Limitations of Sequencing This relation contains a plumber’s job list, in order of priority :- JobENameOwner Fix Dripping Tap Repair Toilet Install Central Heating Install new bath 21, Byker Grove, NE4 6TY 100, Osborne Rd., Jesmond 10, Conaught Drive Whitley Bay 16A, Racing View, Blaydon Mr Bloggs Ms J Wareing Prof Caldwell Mrs Johnson Its tuple sequence is based on the urgency of the job, followed by the profit margin the plumber can make on the job. As this data is not stored in the relation it cannot be used for sorting on tuple values; so the DBMS cannot retrieve the tuples in this order.

12 Overcoming Sequencing Limitations The solution is to add another attribute to the relation, on whose values the tuples can be sorted to create the required sequence. JobENameOwner Fix Dripping Tap Repair Toilet Install Central Heating Install new bath 21, Byker Grove, NE4 6TY 100, Osborne Rd., Jesmond 10, Conaught Drive Whitley Bay 16A, Racing View, Blaydon Mr Bloggs Ms J Wareing Prof Caldwell Mrs Johnson 2 1 3 4 Priority Example : add a priority attribute to the plumber‘s relation and sort on it :-

13 Tuple = Set of Attributes 1 2 3 4 ENo 3 1 2 4 1E3 E5 E1 4E6 E8 5 6 7 8 EName 7 5 6 8 5Smith Mitchell Robson 8Blake Jones 2 4 6 8 M-S 6 2 4 8 2S M D 8M W 2 4 6 8 Sal 6 2 4 8 212,500 21,000 32,500 854,000 68,000 1 2 3 4 ENo 3 1 2 4 1E3 E5 E1 4E6 E8 5 6 7 8 EName 7 5 6 8 5Smith Mitchell Robson 8Blake Jones 2 4 6 8 M-S 6 2 4 8 2S M D 8M W 2 4 6 8 Sal 6 2 4 8 212,500 21,000 32,500 854,000 68,000 These two relations are the same, even though their attributes appear in a different sequence. Definition : a tuple is a set of attributes. Because a relation is a set of tuples, the relation has that same set of attributes. When depicted as a table, the attributes for each tuple are always shown in the same sequence, for simplicity & convenience.

14 Relation - No Duplicate Attributes This cannot be a relation because attribute ‘M-S’ appears twice. 1 2 3 4 ENo 3 1 2 4 1E3 E5 E1 4E6 E8 5 6 7 8 EName 7 5 6 8 5Smith Mitchell Robson 8Blake Jones 2 4 6 8 M-S 6 2 4 8 2S M D 8M W 2 4 6 8 Sal 6 2 4 8 212,500 21,000 32,500 854,000 68,000 2 4 6 8 M-S 6 2 4 8 2S M D 8M W Duplicate attributes are those with duplicate names, whether their contents are duplicated or not.

15 A Relation - its Definition so far l A relation is a set of tuples, each of which is a set of attributes. l Each tuple in the relation contains the same set of attributes by name, although the same attribute in different tuples may, and often does, hold different values. l Each named attribute holds data of one specific type (sometimes expressed as being from a particular domain). Attributes may or may not have data types in common with other attributes in a tuple. l Consequently it is convenient to consider the relation as having those named attributes, each with its particular data type/domain. l A relation’s Heading (or Intension) is its type, which is a composite type based on that of its attributes. l A relation’s Body (or Extension) is its value, which is a composite being the value of all of its attributes in all of its tuples.


Download ppt "What is a Relation ? To consider how Set Theory defines the structure of relations; To consider how relations can be exploited to provide the equivalent."

Similar presentations


Ads by Google