Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 2 The Relational Data Model

Similar presentations


Presentation on theme: "Week 2 The Relational Data Model"— Presentation transcript:

1 Week 2 The Relational Data Model
CSE9002 Week 2 The Relational Data Model

2 Joins of Tables The joining of attributes depends on certain types of relationships; Consider two attributes C1 and C2 which are join attributes There are 4 types of relationships possible (a) the values of C1 and C2 are equal (b) the values of C1 are a subset of those of C2 (or vice versa) (c) the values of C1 and C2 are conjoint - they have some values in common (d) the values of C1 and C2 are disjoint - they have no values in common

3 Joins of Tables In set theory, these take the forms (a) C1 = C2
(b) C1  C2 or C2  C1 (c) C1 - C2  0 or C2 - C1  0 (d) C1 - C2 = C1 and C2 - C1 = C2

4 Joins of Tables There are a number of possible ‘join’ types allowable in the relational model They are: 1. Thetajoin 2.Equijoin 3. Natural join 4. Inner join 5. Outer join 6. Left Outer join 7. Right Outer join 8. Full Outer join

5 Joins of Tables 1. Thetajoin This is the case when the ‘condition of join’, or comparator, is of the form < = > ! or any combination (>=) as in the case select * from parts, rates where parts.partno (< > =) rates.partno 2. If the comparator is =, the join is a equijoin 3. If the select clause contains the name of only one of the join attributes, this results in a natural join (if the clause had been stated as select parts.partno, rates.partno, then both these attributes would have been in the result table)

6 Joins of Tables 4. Inner Join This is based on there being 2 tables - e.g. members and penalties as in a video club members, and penalty tables. This results in only that data which is supported by the comparator being presented in the result table. The only results will be those rows which satisfy the join condition of (in this case) = (equality). Any other non-matched rows will not be displayed. It is to be expected that not all members will have penalties, and so only some of the members will be listed.

7 Joins of Tables 5. Outer join.
There may be other data which the user wanted to ‘see’ but it would have been excluded. A case could be where a librarian wants to see who has incurred a fine for late or non return of videos, but also wants to see that the other customers (a) are listed (b) are shown as 0 penalty (even though this is not a value as the row would not exist in the penalties table) This generally extends the query to a union and a substitute attribute.

8 Joins of Tables 6. Left Outer join
This is a type of outer equijoin, and takes its name from the use of the ‘left’ table as the dominant table. The result table will only include values from the secondary table where the equijoin is valid. The result is the ‘intersection’ of the populations of the 2 tables involved. The ‘left’ table is the first named table in the from clause

9 Joins of Tables 7. Right outer join
The dominant attributes are those from the table named as the rightmost position in the from clause. 8. Full Outer Join All rows from both tables named in the from clause are processed. This is the same as a union of the left and right outer joins of the tables involved.

10 Joins of Tables Some pointers:
1. Some versions of SQL allow for the nomination of the join type (see example) 2. The correct use of the various joins depends on the knowledge of table content and of course the clear understanding and statement of the required result table content 3. The complex joins invariably require sub-queries which may require the exist or not exist or the in or not in operators, and the union function

11 Remote Presentation Client/Server
Desktop Host Presentation Services Data Services Presentation Logic File Services Business Logic Data Logic

12 3 Tiered Client Server 3 Tiered Client/Server
Database Desktop Application Server DataBase Server Presentation Services Business Logic Data Services Presentation Logic Data Logic File Services

13 Mirror Tables Base Tables (disk located) and Virtual Tables (memory)
Consider a table ‘employee’ which contains staff detail supervisor detail manager details Select a.name,b.name,c.name,d.name,e.name from employee a, employee b, employee c, employee d, employee e; would produce a results table of all names in 5 columns of output

14 Joining a Table to Itself
Typical Query: For each employee, list the employee number, name Manager and Manager’s name Select X.EMPNO, X.NAME, X.MGR, Y.NAME from EMP X, EMP Y where X.MGR = Y.EMPNO Result: EMPNO NAME MGR NAME SMITH BROWN JONES BROWN BLACK BROWN BROWN WHITE The Primary Key and the Foreign are both in the same table Two virtual tables are created for joining (‘alias’ feature)

15 DIVISION Divides a BINARY relation by a UNARY relation and produces a UNARY relation as a result. skill-reqd result emp-skill empno empno skillcode skillcode E2 S2 S4 E1 E2 E3 E5 E6 S1 S2 S3 S4 S5 S6 Divide emp-skill by skill-reqd to give result Special note: JOIN, INTERSECTION and DIVISION can be defined in terms of the other 5 operators (which are known as the ‘primitive’ operators).

16 A DIVISION example In the Air Transport Industry, pilot’s records contain details of the aircraft they are qualified to fly. And there are also records of the number and types of aircraft in the hangars and which Company owns what. In this case, the table of pilot’s names and the planes they can fly is the dividend The details of the planes in the hangars is the quotient

17 A DIVISION example The query is to obtain the names of the pilots who can fly every type of plane in the hangars

18 Suggested Solution create table pilotskill (pilot vchar (150) not null, plane vchar(15) not null); create table hangar (plane vchar(15)); select pilot from pilotskill ps1, hangar h1 where ps1.plane = h1.plane group by ps1.pilot having count(ps1.plane = select count(*) from hangar);

19 Division [notice the absence of any ‘division’ operator - this is effectively performed by the execution plan]

20 Division Examples A B C J K L 1 3 1 J 1 K 1 L 2 J 2 K 3 K 3 L 3 J
Result J K L 1 3 1 J 1 K 1 L 2 J 2 K 3 K 3 L 3 J

21 Division Examples Name Degree D1 D2 B Sc M Sc D3 B Sc Jones B Sc
Jensen B Sc Jensen M Sc Jensen PhD Smith B Sc Smith M Sc Rogers B Sc Rogers PhD M Sc B Sc PhD R3 Jones Jensen Smith Rogers R2 Jensen Smith R1 Jensen


Download ppt "Week 2 The Relational Data Model"

Similar presentations


Ads by Google