29
Relational Model Part 2 Manipulative part

Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

Embed Size (px)

Citation preview

Page 1: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

Relational Model

Part 2

• Manipulative part

Page 2: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

The manipulative part defines the operations you can perform on the relational databases.

The manipulative part is expressed in the form relation algebra.

Conventional algebra:

X+y-(x/20)

–Variables, x and y.

–A constant number, 20.

–Arithmetic operators, +, - and /.

the operation is performed on ‘numbers’.

Page 3: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

relational algebra is defined on a relation:

X union (<1,2,3>,<1,4,6>).

Given X = (<1,6,9>,<1,3,4>) then

X union (<1,2,3>,<1,4,6>) is:

(<1,6,9>,<1,3,4>,<1,2,3>,<1,4,6>).

Page 4: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

Product

Prod_No Name Colour

P1 Pantaloons Blue

P2 Pantaloons Khaki

P3 Socks Harebell

P4 Socks White

P5 Pantaloons White

Customer

Cust_No Name Address

C1 Nippers Ltd 25 High St, Leeds

C2 Tots-Gear 5 Low, Oxford

C3 Super-Brat 30 New St Luton

C6 Tiny-Togs 1 Old Rd, Luton

Sales_Order

Order_No Date Cust_No

01 1/9/87 C1

02 2/5/87 C3

09 1/9/87 C6

010 1/9/87 C6

Sales_Order_line

Order_No Prod_No Quantity

01 P1 100

02 P1 100

02 P4 200

09 P1 50

010 P1 50

 

Why need relational algebra? Question: ‘Extract from the database to know the nam

e of customers who have placed orders on 1/9/87’.

Page 5: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

Extract from the database to know the name of customers who have placed orders on 1/9/87.

Order_No Date Cust_No

01 1/9/87 C1

02 2/5/87 C3

09 1/9/87 C6

010 1/9/87 C6

• Step 1: Search through the Sale_Order table- cut out each row where the date is 1/9/87.

Order_No Date Cust_No

01 1/9/87 C1

09 1/9/87 C6

010 1/9/87 C6

Page 6: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

Step 2: Search through the Customer table the value in the Cust_No column = Cust_No column.

Cust_No Name Address

C1 Nippers Ltd 25 High St, Leeds

C2 Tots-Gear 5 Low, Oxford

C3 Super-Brat 30 New St Luton

C6 Tiny-Togs 1 Old Rd, Luton

Cust_No Name Address

C1 Nippers Ltd 25 High St, Leeds

C6 Tiny-Togs 1 Old Rd, Luton

C6 Tiny-Togs 1 Old Rd, Luton

Order_No Date Cust_No

01 1/9/87 C1

09 1/9/87 C6

010 1/9/87 C6

Page 7: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

Step 2: Search through the Customer table Cut out the Name column in the above table Cut out and throw away any duplicate row.

Cust_No Name Address

C1 Nippers Ltd 25 High St, Leeds

C6 Tiny-Togs 1 Old Rd, Luton

C6 Tiny-Togs 1 Old Rd, Luton

Name

Nippers Ltd

Tiny-Togs

Page 8: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

Relational algebra operator and expressions • RESTRICTION• PROJECT• PRODUCT• UNION• DIFFERENCE

• Relational algebra includes a set of operators to define new relations (tables).

IntersectionDivisionJoin

• unary operator -- operate on one relation• binary operator – operate on two relations.

Page 9: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

RESTRICT• RESTRICT is an operator on a single relation (table).

• RESTRICT is sometimes called ‘SELECT’.

RESTRICT SALES_ORDER WHERE CUST_NO=‘C6’Order_No Date Cust_No

01 1/9/87 C1

02 2/5/87 C3

09 1/9/87 C6

010 1/9/87 C6

09 1/9/87 C6

010 1/9/87 C6

Restrict R where CR-- relation, C – condition.

Page 10: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

RESTRICT – more examplesRESTRICT PRODUCT WHERE (COLOUR=‘white’ OR

COLOUR=‘blue’)Product

Prod_No Name Colour

P1 Pantaloons Blue

P2 Pantaloons Khaki

P3 Socks Harebell

P4 Socks White

P5 Pantaloons White

Prod_No Name Colour

P1 Pantaloons Blue

P4 Socks White

P5 Pantaloons White

RESTRICT PRODUCT WHERE NOT(Colour = ‘blue’) AND (Name =’Sock’ OR Name =’Pantaloons’) ??

Page 11: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

PROJECTION• PROJECT is an operator to cut out the attributes.

PROJECT Product on Colour

PROJECT R ON XR– relation, X – attribute(s).

Product

Prod_No Name Colour

P1 Pantaloons Blue

P2 Pantaloons Khaki

P3 Socks Harebell

P4 Socks White

P5 Pantaloons White

Colour

Blue

Khaki

Harebell

White

White

Page 12: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

UNION• UNION comprise all the tuples from two relations.

PRODUCT UNION <P7, Socks, Blue>

R UNION SR, S– relations.

Product

Prod_No Name Colour

P1 Pantaloons Blue

P2 Pantaloons Khaki

P3 Socks Harebell

P4 Socks White

P5 Pantaloons White

 

P7 Socks Blue

Page 13: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

DIFFERENCE

• DIFFERENCE - take the tuples of R according to S.

• SALES_ORDER MINUS <02, 2/5/87, C3>

R MINUS SR, S– relations.

 

Sales_OrderOrder_No Date Cust_No

01 1/9/87 C1

02 2/5/87 C3

09 1/9/87 C6

010 1/9/87 C6

Order_No Date Cust_No

01 1/9/87 C1

09 1/9/87 C6

010 1/9/87 C6

Page 14: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

Cartesian Product R TIMES SR, S– relations.

Product_Name

Name

Pantaloons

Socks

Colour

Blue

Harebell

Product_Colour

TIMES

Name Colour

Pantaloons Blue

Pantaloons Harebell

Socks Blue

Socks Harebell

Page 15: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

INTERSECTInclude the tuples that exist in two relations.

R INTERSECT SR, S– relations.

 

Product_in_Stock

Prod_No

P1

P3

Prod_No

P3

P5

Product_On_Order

INSTERSECT

Prod_No

P3

Page 16: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

DIVIDE DIVIDE R BY SR, S– relations.

Language

English

French

Langs

DIVIDE

Employ_No

E3

E4

Salesman_Lang

Employee_No Language

E1 French

E3 English

E3 French

E4 English

E4 French

E4 German

E5 English

E5 German

E5 Swedish

Page 17: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part JOIN

 

Natural JOINEQUI-JOINTHETA-JOINSEMI-JOINOUTER-JOIN

To construct a new relation according to the specific similarity between the different relations of a database.

JOINs may be of the following types:

Page 18: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

JOIN Example

 

Product

Prod_No Name Colour

P1 Pantaloons Blue

P2 Pantaloons Khaki

P3 Socks Harebell

P4 Socks White

P5 Pantaloons White

Customer

Cust_No Name Address

C1 Nippers Ltd 25 High St, Leeds

C2 Tots-Gear 5 Low, Oxford

C3 Super-Brat 30 New St Luton

C6 Tiny-Togs 1 Old Rd, Luton

Sales_Order

Order_No Date Cust_No

01 1/9/87 C1

02 2/5/87 C3

09 1/9/87 C6

010 1/9/87 C6

Sales_Order_line

Order_No Prod_No Quantity

01 P1 100

02 P1 100

02 P4 200

09 P1 50

010 P1 50

Page 19: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

NATURAL JOIN

 

NATURAL JOIN is to combine two relations on the basis of all the attributes which occur in both of them.

JOIN Sales_Order AND Customer

Order_No Date Cust_No Name Address

01 1/9/87 C1 Nippers Ltd 25 High St, Leeds

02 2/5/87 C3 Tots-Gear 5 Low, Oxford

09 1/9/87 C6 Tiny-Togs 1 Old Rd, Luton

010 1/9/87 C6 Tiny-Togs 1 Old Rd, Luton

Page 20: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

EQUI-JOIN

EQUI-JOIN combines tuples from two relations, with the specified attributes have the same value for the associating tuples.

JOIN R AND S WHERE R.A=S.B

It combines tuples in R and S where the values of attributes (A in R and B in S) are equal.

Page 21: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

EQUI-JOINDept _Employee

Department Employee_No Grade

D1 E1 II

D1 E2 IB

D1 E3 IA

D1 E4 III

D2 E5 III

D2 E9 II

Management

Manager Subordinate

E1 E2

E1 E3

E2 E4

E5 E9

Department Employee_No Grade Manager Subordinate

D1 E1 II E1 E2

D1 E1 II E1 E3

D1 E2 IB E2 E4

D2 E5 III E5 E9

JOIN Dept_Employee AND ManagementWHERE Dept_Employee.Employee_No = Management.Manager

Page 22: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

THETA-JOIN

In a THETA-JOIN, the condition is not restricted to equality. Any comparison operator may be used.

In EQUI-JOIN, tuples are joined where the values of specified attributes are equal.

e.g. >, <, >=, and so on…

Page 23: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

THETA-JOIN Example

JOIN Payment AND Invoice WHERE Payment.Date > Invoice.Date.

Payment.Invoice_No

Payment.Date

Invoice.Invoice_No

Invoice.Date

Invoice.Amount

I4 880301 I1 880101 1000

I4 880301 I3 880101 150

I4 880301 I4 880301 200

Invoice_No Data

I1 880101

I4 880901

Invoice_No Date Amount

I1 880101 1000

I3 880101 150

I4 880301 200

Payment Invoice

Page 24: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

THETA-JOIN Example

JOINT Payment AND Invoice WHERE ((Payment.Date>= Invoice.Date) AND (Payment.Invoice_No = Invoice.Invoice_No))

Payment.Invoice_No

Payment.Date

Invoice.Invoice_No

Invoice.Date

Invoice.Amount

I4 880301 I4 880301 200

Payment.Invoice_No

Payment.Date

Invoice.Invoice_No

Invoice.Date

Invoice.Amount

I4 880301 I1 880101 1000

I4 880301 I3 880101 150

I4 880301 I4 880301 200

Page 25: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

SEMI-JOIN SEMI-JOIN of two relations is the set of all the tuples of the first, which will join with tuple of the second.

Customer

Cust_No Name Address

C1 Nippers Ltd 25 High St, Leeds

C2 Tots-Gear 5 Low, Oxford

C3 Super-Brat 30 New St Luton

C6 Tiny-Togs 1 Old Rd, Luton

Sales_Order

Order_No Date Cust_No

01 1/9/87 C1

02 2/5/87 C3

09 1/9/87 C6

010 1/9/87 C6

SJOIN Customer AND Sales_Order

Page 26: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

SEMI-JOIN Customer

Cust_No Name Address

C1 Nippers Ltd 25 High St, Leeds

C2 Tots-Gear 5 Low, Oxford

C3 Super-Brat 30 New St Luton

C6 Tiny-Togs 1 Old Rd, Luton

Sales_Order

Order_No Date Cust_No

01 1/9/87 C1

02 2/5/87 C3

09 1/9/87 C6

010 1/9/87 C6

SJOIN Customer AND Sales_Order

 

Cust_No Name Address

C1 Nippers Ltd 25 High St, Leeds

C3 Tots-Gear 5 Low, Oxford

C6 Tiny-Togs 1 Old Rd, Luton

 

SEMI-NATURAL JOIN SEMI-EQUI JOINSEMI-THETA JOINSJOIN R AND S WHERE C

Page 27: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

OUTER-JOIN

OUTER-JOIN is an information-preserving version of the join.

OJOIN Sales_Order AND Customer

 

Order_No Date Cust_No Name Address

01 1/9/87 C1 Nippers Ltd 25 High St, Leeds

Null Null C2 Tots-Gear 5 Low, Oxford

02 2/5/87 C3 Tots-Gear 5 Low, Oxford

09 1/9/87 C6 Tiny-Togs 1 Old Rd, Luton

010 1/9/87 C6 Tiny-Togs 1 Old Rd, Luton

Page 28: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

Complex Expression

 

Have a close read pp. 57-59.

 

Page 29: Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases

2.5 The manipulative part

Summary

 

Section 2.4.16

 

Reading: Section 2.4

Exercise: 2.5