25
24th March 2020 Birkbeck College, U. London 1 Introduction to Computer Systems Department of Computer Science and Information Systems Lecturer: Steve Maybank [email protected] Spring 2020 Week 11a: Relational Operations

Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

24th March 2020 Birkbeck College, U. London 1

Introduction to Computer Systems

Department of Computer Science and Information Systems

Lecturer: Steve Maybank

[email protected]

Spring 2020

Week 11a: Relational Operations

Page 2: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 2

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 asystematic way?

Page 3: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 3

Relational Operations

The answers to database queries are obtained using the relational operations

SELECT

PROJECT

JOIN

These operations produce new relations from old ones

Page 4: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Birkbeck College 4

Relational Database

37 102 £1000 1.7.19 Plate

43 103 £2000 5.5.19 Case

20 54 £3400 2.4.19 Panel

102 Sperry 1 The Lane

103 Univac 15 Retail Road

54 Honeywell 205 North Street

Num CNum Price Due date

Product

O C

Num Name Address

O: relation containing ordersC: relation containing details of customers

Page 5: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 5

Table of Orders for Plate

37 102 £1000 1.7.19 Plate

43 103 £2000 5.5.19 Case

20 54 £3400 2.4.19 Panel

Num CNum Price Due date

Product

37 102 £1000 1.7.19 Plate

O SELECT: take all tuplesfor which the productis Plate, to give …

Num CNum Price Due date

Product

ANS1

ANS1<- SELECT from O where Product=Plate

Page 6: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 6

Table of Order Numbers and Products

37 102 £1000 1.7.19 Plate

43 103 £2000 5.5.19 Case

20 54 £3400 2.4.19 Panel

Num CNum Price Due date

Product

37 Plate

43 Case

20 Panel

O PROJECT: remove attributes CNum, Price and Due date to leave…

Num Product

ANS2

ANS2 <- PROJECT Num, Product from O

Page 7: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 7

Example of a JOIN

a 1

b 2

m 5

n 2

A.V A.W

Relation A Relation B

B.X B.Y

a 1 m 5

a 1 n 2

b 2 m 5

b 2 n 2

A.V A.W B.X B.Y

C <- JOIN A and B

Page 8: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 8

Definition of JOIN

Let A, B, C be relations such that C=JOIN(A,B)

The tuples in C are obtained by merging tuples a from A and b from B

All possible pairs a, b are used to construct the tuples in C.

Page 9: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 9

Relation Containing O.Num and C.Name

37 102 £1000 1.7.19 Plate

43 103 £2000 5.5.19 Case

20 54 £3400 2.4.19 Panel

102 Sperry 1 The Lane

103 Univac 15 Retail Road

54 Honeywell 205 North Street

Num CNum Price Due date

Product

O C

Num Name Address

JOIN: combine tuples in O and C. Then use PROJECT toremove all attributes except O.Num and C.Name.

Page 10: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 10

JOIN and then PROJECT

37 102 £1000 1.7.19 Plate 102 Sperry 1 The Lane

43 103 £2000 5.5.19 Case 103 Univac 15 Retail Road

20 54 £3400 2.4.19 Panel 54 Honeywell

205 North Street

O.Num O.CNum O.Prc O.D O.Prd C.Num C.Name C.A

37 Sperry

43 Univac

20 Honeywell

TEMP <- JOIN O and C where O.CNum=C.Num

ANS3

O.Num C.Name

ANS3<-PROJECT O.Num, C.Name from TEMP

Page 11: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 11

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’

Page 12: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 12

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

Page 13: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 13

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

Page 14: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear, Section 9.2 14

Second Example of JOIN

R3 <- JOIN O and C where O.Price>£1500 and O.CNum=C.Num

43 103 £2000 5.5.19 Case 103 Univac 15 Retail Road

20 54 £3400 2.4.19 Panel 54 Honeywell

205 North Street

O.Num O.CNum O.Prc O.D O.Prd C.Num C.Name C.A

R3

Page 15: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

15

Join and Keys

34 16 D1

28 22 D2

key foreign key

data key data

16 DB1

22 DB2

BA

C <- JOIN A and B where A.foreign key=B.key

34 16 D1 16 DB1

28 22 D2 22 DB2

A.key A.Foreign key

A.data B.key B.data

C

Page 16: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

cf. Brookshear, Section 9.2 16

Symmetries

A relation does not specify the order of the attributes or the order of the tuples

102 Sperry 1 The Lane

103 Univac 15 Retail Road

54 Honeywell 205 North Street

is thesame as

102 1 The Lane Sperry

54 205 North Street

Honeywell

103 15 Retail Road

Univac

C C

Page 17: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

cf. Brookshear, Section 9.2 17

Repeated Tuples

A relation cannot have repeated tuples

102 Sperry 1 The Lane

103 Univac 15 Retail Road

54 Honeywell 205 North Street

54 Honeywell 205 North Street

Not a relation in arelational database.

Page 18: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Birkbeck College 18

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

Page 19: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Brookshear Ch. 9, problem 12 19

Problem 1

3 J

4 K

In terms of the relations shown below, what is the appearance of therelation RESULT after executing each of these instructions?

X relation

U V W

A Z 5

B D 3

C O 5

Y relationR S

a. RESULT <- PROJECT W from Xb. RESULT <- SELECT from X where W=5c. RESULT <- PROJECT S from Yd. RESULT <- JOIN X and Y where X.W>= Y.R.

Page 20: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

Problem 2

Use select, project and join to answer a) which depts (D) offer IT665? b) list all depts.; c) which depts offer 4 credit courses (C)?

20Brookshear, Ch 9 rev. problem 13

Networks IT655

Database CS543

VLSI EC653

C.Name C.ID

Computing CS543 4

Computing EC653 5

Electronics IT655 4

Electronics EC653 5

IT CS543 4

IT IT655 4

D.Name D.ID D.Credits

Page 21: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

BB Ch 9 Review Problem 10 21

Problem 3

To what extent is the order in which SELECT and PROJECT operations are applied to a relation significant? That is, under what conditions will Selecting and then Projecting produce the same results as first Projecting and then Selecting?

Page 22: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

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.

22Brookshear, Section 9.2

Page 23: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

23

SQL Statement 1

SELECT O.Num, C.Name

FROM O, C

WHERE O.CNum=C.Num

Interpretationfrom O, C: Join O and Cwhere O.Cnum=C.Num: retain tuples satisfying this condition

select O.Num, C.Name: project, retaining these attributes

Page 24: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

24

General Form

SELECT <attributes>

FROM <relations>

WHERE <conditions>

Interpretation

Join relationsImpose conditionsProject attributes

Page 25: Introduction to Computer Systemssjmaybank/ICS/ICS week 11a.pdf · 43 103 £2000 5.5.19 Case 20 54 £3400 2.4.19 Panel 102 Sperry 1 The Lane 103 Univac 15 Retail Road 54 Honeywell

25

SQL Statement 2

SELECT O.Num, O.Price

FROM O

Interpretation

Project O.Num and O.Price from O