Presentation is loading. Please wait.

Presentation is loading. Please wait.

9th December 2014Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

Similar presentations


Presentation on theme: "9th December 2014Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems."— Presentation transcript:

1 9th December 2014Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2014 Week 11a: Relational Operations

2 9th December 2014Brookshear, Section 9.22 Database Queries Show all orders for plate. Show all order numbers and the associated products. Show all order numbers and the associated company names. In the relational model the answers are always relations, ie. tables. Can the answers to database queries be found in a systematic way?

3 9th December 2014Brookshear, Section 9.23 Relational Operations The answers to database queries are obtained using the relational operations SELECT PROJECT JOIN These operations produce new relations from old ones.

4 9th December 2014Birkbeck College4 Relational Database 37102£10001.7.06Plate 43103£20005.5.06Case 2054£34002.4.06Panel 102Sperry1 The Lane 103Univac15 Retail Road 54Honeywell205 North Street NumCNumPriceDue date Product OC NumNameAddress O: relation containing orders C: relation containing details of customers

5 9th December 2014Brookshear, Section 9.25 List of Tuples of Orders for Plate 37102£10001.7.06Plate 43103£20005.5.06Case 2054£34002.4.06Panel NumCNumPriceDue date Product 37102£10001.7.06Plate O SELECT: take all tuples for which the product is Plate, to give … NumCNumPriceDue date Product ANS1 ANS1<- SELECT from O where Product=Plate

6 9th December 2016Brookshear, Section 9.26 List of Order Numbers and Products 37102£10001.7.06Plate 43103£20005.5.06Case 2054£34002.4.06Panel NumCNumPriceDue date Product 37Plate 43Case 20Panel O PROJECT: remove attributes CNum, Price and Due date to leave… NumProduct ANS2 ANS2 <- PROJECT Num, Product from O

7 9th December 2014Brookshear, Section 9.27 Example of a JOIN a1 b2 m5 n2 A.V A.W Relation ARelation B B.X B.Y a1m5 a1n2 b2m5 B2n2 A.V A.W B.X B.Y C <- JOIN A and B

8 9th December 2014Brookshear, Section 9.28 Definition of JOIN Let A, B, C be relations such that C=JOIN(A,B). Let a be any tuple from A and let b be any tuple from B. The tuples in C are obtained by merging pairs of tuples (a, b). All possible pairs (a, b) are used to construct the tuples in C.

9 9th December 2014Brookshear, Section 9.29 Relation Containing O.Num and C.Name 37102£10001.7.06Plate 43103£20005.5.06Case 2054£34002.4.06Panel 102Sperry1 The Lane 103Univac15 Retail Road 54Honeywell205 North Street NumCNumPriceDue date Product OC NumNameAddress JOIN: combine tuples in O and C. Then use PROJECT to remove all attributes except O.Num and C.Name.

10 9th December 2014Brookshear, Section 9.210 JOIN and then PROJECT 37102£10001.7.06Plate102Sperry1 The Lane 43103£20005.5.06Case103Univac15 Retail Road 2054£34002.4.06Panel54Honey well 205 North Street O.NumO.CNumO.PrcO.DO.PrdC.NumC.NameC.A 37Sperry 43Univac 20Honeywell TEMP <- JOIN O and C where O.CNum=C.Num ANS3 O.NumC.Name ANS3<-PROJECT O.Num, C.Name from TEMP

11 9th December 2014Brookshear, Section 9.211 SELECT  The original relation is R1, the new relation created by SELECT is R2 and the conditions on the attribute values in R2 are c1,…cn.  R2 <- SELECT from R1 where c1,…, cn.  Example: ANS1 <- SELECT from O where O.Product=‘Plate’

12 9th December 2014Brookshear, Section 9.212 PROJECT  The original relation is R1, the new relation created by PROJECT is R2 and the attributes projected to R2 are att1,…, attn.  R2 <- PROJECT att1,…attn from R1  Example: ANS2 <- PROJECT O.Num, O.Product from O

13 9th December 2014Brookshear, Section 9.213 JOIN  The original relations are R1, R2, the new relation created by JOIN is R3 and the conditions on the attribute values in R3 are c1,…cn.  R3 <- JOIN R1 and R2 where c1,…, cn  Example: Temp <- JOIN O, C where O.CNum=C.Num

14 9th December 2014Brookshear, Section 9.214 Second Example of JOIN R3 £1500 and O.CNum=C.Num 43103£20005.5.06Case103Univac15 Retail Road 2054£34002.4.06Panel54Honey well 205 North Street O.NumO.CNumO.PrcO.DO.PrdC.NumC.NameC.A R3

15 9th December 201415 Join and Keys 3416D1 2822D2 keyforeign key data keydata 16DB1 22DB2 BA C <- JOIN A and B where A.foreign key=B.key 3416D116DB1 2822D222DB2 A.keyA.Foreign key A.dataB.keyB.data C

16 9th December 2014cf. Brookshear, Section 9.216 Symmetries A relation does not specify the order of the attributes or the order of the tuples. 102Sperry1 The Lane 103Univac15 Retail Road 54Honeywell205 North Street is the same as 1021 The LaneSperry 54205 North Street Honeywell 10315 Retail Road Univac CC

17 9th December 2014cf. Brookshear, Section 9.217 Repeated Tuples A relation cannot have repeated tuples 102Sperry1 The Lane 103Univac15 Retail Road 54Honeywell205 North Street 54Honeywell205 North Street Not a relation in a relational database.

18 9th December 2014Birkbeck College18 Attributes in Different Relations An attribute of a given relation is not an attribute of any other relation. E.g. O.CNum is a different attribute from C.Num.

19 Structured Query Language Standardised language for database queries originally developed and marketed by IBM. An SQL command describes the required information. It does not specify how to obtain that information. 9th December 201419Brookshear, Section 9.2

20 9th December 201420 SQL Statement select O.Num, C.Name from O, C where O.CNum=C.Num Interpretation: JOIN O and C, SELECT those tuples for which O.CNum=C.Num, then PROJECT, retaining the attributes O.Num and C.Name.

21 9th December 2014Brookshear Ch. 9, problem 1221 Problem 3J 4K In terms of the relations shown below, what is the appearance of the relation RESULT after executing each of these instructions? X relation U V W AZ5 BD3 CO5 Y relation R S a.RESULT <- PROJECT W from X b.RESULT <- SELECT from X where W=5 c.RESULT <- PROJECT S from Y d.RESULT = Y.R.


Download ppt "9th December 2014Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems."

Similar presentations


Ads by Google