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

## Presentation on theme: "Relational Model Part 2 Manipulative part. 2.5 The manipulative part The manipulative part defines the operations you can perform on the relational databases."— Presentation transcript:

Relational Model Part 2 Manipulative part

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’.

2.5 The manipulative part relational algebra is defined on a relation: X union (, ). Given X = (, ) then X union (, ) is: (,,, ).

2.5 The manipulative part Product Prod_NoNameColour P1PantaloonsBlue P2PantaloonsKhaki P3SocksHarebell P4SocksWhite P5PantaloonsWhite Customer Cust_NoNameAddress C1Nippers Ltd25 High St, Leeds C2Tots-Gear5 Low, Oxford C3Super-Brat30 New St Luton C6Tiny-Togs1 Old Rd, Luton Sales_Order Order_NoDateCust_No 011/9/87C1 022/5/87C3 091/9/87C6 0101/9/87C6 Sales_Order_line Order_NoProd_NoQuantity 01P1100 02P1100 02P4200 09P150 010P150 Why need relational algebra? Question: ‘ Extract from the database to know the name of customers who have placed orders on 1/9/87 ’.

2.5 The manipulative part Extract from the database to know the name of customers who have placed orders on 1/9/87. Order_NoDateCust_No 011/9/87C1 022/5/87C3 091/9/87C6 0101/9/87C6 Step 1: Search through the Sale_Order table - cut out each row where the date is 1/9/87. Order_NoDateCust_No 011/9/87C1 091/9/87C6 0101/9/87C6

2.5 The manipulative part Step 2: Search through the Customer table the value in the Cust_No column = Cust_No column. Cust_NoNameAddress C1Nippers Ltd25 High St, Leeds C2Tots-Gear5 Low, Oxford C3Super-Brat30 New St Luton C6Tiny-Togs1 Old Rd, Luton Cust_NoNameAddress C1Nippers Ltd25 High St, Leeds C6Tiny-Togs1 Old Rd, Luton C6Tiny-Togs1 Old Rd, Luton Order_NoDateCust_No 011/9/87C1 091/9/87C6 0101/9/87C6

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_NoNameAddress C1Nippers Ltd25 High St, Leeds C6Tiny-Togs1 Old Rd, Luton C6Tiny-Togs1 Old Rd, Luton Name Nippers Ltd Tiny-Togs

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). Intersection Division Join unary operator -- operate on one relation binary operator – operate on two relations.

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_NoDateCust_No 011/9/87C1 022/5/87C3 091/9/87C6 0101/9/87C6 091/9/87C6 0101/9/87C6 Restrict R where C R-- relation, C – condition.

2.5 The manipulative part RESTRICT – more examples RESTRICT PRODUCT WHERE (COLOUR=‘white’ OR COLOUR=‘blue’) Product Prod_NoNameColour P1PantaloonsBlue P2PantaloonsKhaki P3SocksHarebell P4SocksWhite P5PantaloonsWhite Prod_NoNameColour P1PantaloonsBlue P4SocksWhite P5PantaloonsWhite RESTRICT PRODUCT WHERE NOT(Colour = ‘blue’) AND (Name =’Sock’ OR Name =’Pantaloons’) ??

2.5 The manipulative part PROJECTION PROJECT is an operator to cut out the attributes. PROJECT Product on Colour PROJECT R ON X R– relation, X – attribute(s). Product Prod_NoNameColour P1PantaloonsBlue P2PantaloonsKhaki P3SocksHarebell P4SocksWhite P5PantaloonsWhite Colour Blue Khaki Harebell White

2.5 The manipulative part UNION UNION comprise all the tuples from two relations. PRODUCT UNION R UNION S R, S– relations. Product Prod_NoNameColour P1PantaloonsBlue P2PantaloonsKhaki P3SocksHarebell P4SocksWhite P5PantaloonsWhite P7SocksBlue

2.5 The manipulative part DIFFERENCE DIFFERENCE - take the tuples of R according to S. SALES_ORDER MINUS R MINUS S R, S– relations. Sales_Order Order_NoDateCust_No 011/9/87C1 022/5/87C3 091/9/87C6 0101/9/87C6 Order_NoDateCust_No 011/9/87C1 091/9/87C6 0101/9/87C6

2.5 The manipulative part Cartesian Product R TIMES S R, S– relations. Product_Name Name Pantaloons Socks Colour Blue Harebell Product_Colour TIMES NameColour PantaloonsBlue PantaloonsHarebell SocksBlue SocksHarebell

2.5 The manipulative part INTERSECT Include the tuples that exist in two relations. R INTERSECT S R, S– relations. Product_in_Stock Prod_No P1 P3 Prod_No P3 P5 Product_On_Order INSTERSECT Prod_No P3

2.5 The manipulative part DIVIDE DIVIDE R BY S R, S– relations. Language English French Langs DIVIDE Employ_No E3 E4 Salesman_Lang Employee_NoLanguage E1French E3English E3French E4English E4French E4German E5English E5German E5Swedish

2.5 The manipulative part JOIN Natural JOIN EQUI-JOIN THETA-JOIN SEMI-JOIN OUTER-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:

2.5 The manipulative part JOIN Example Product Prod_NoNameColour P1PantaloonsBlue P2PantaloonsKhaki P3SocksHarebell P4SocksWhite P5PantaloonsWhite Customer Cust_NoNameAddress C1Nippers Ltd25 High St, Leeds C2Tots-Gear5 Low, Oxford C3Super-Brat30 New St Luton C6Tiny-Togs1 Old Rd, Luton Sales_Order Order_NoDateCust_No 011/9/87C1 022/5/87C3 091/9/87C6 0101/9/87C6 Sales_Order_line Order_NoProd_NoQuantity 01P1100 02P1100 02P4200 09P150 010P150

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_NoDateCust_NoNameAddress 011/9/87C1Nippers Ltd25 High St, Leeds 022/5/87C3Tots-Gear5 Low, Oxford 091/9/87C6Tiny-Togs1 Old Rd, Luton 0101/9/87C6Tiny-Togs1 Old Rd, Luton

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.

2.5 The manipulative part EQUI-JOIN Dept _Employee DepartmentEmployee_NoGrade D1E1II D1E2IB D1E3IA D1E4III D2E5III D2E9II Management ManagerSubordinate E1E2 E1E3 E2E4 E5E9 DepartmentEmployee_NoGradeManagerSubordinate D1E1IIE1E2 D1E1IIE1E3 D1E2IBE2E4 D2E5IIIE5E9 JOIN Dept_Employee AND Management WHERE Dept_Employee.Employee_No = Management.Manager

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…

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 I4880301I18801011000 I4880301I3880101150 I4880301I4880301200 Invoice_NoData I1880101 I4880901 Invoice_NoDateAmount I18801011000 I3880101150 I4880301200 Payment Invoice

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 I4880301I4880301200 Payment. Invoice_No Payment. Date Invoice. Invoice_No Invoice. Date Invoice. Amount I4880301I18801011000 I4880301I3880101150 I4880301I4880301200

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_NoNameAddress C1Nippers Ltd25 High St, Leeds C2Tots-Gear5 Low, Oxford C3Super-Brat30 New St Luton C6Tiny-Togs1 Old Rd, Luton Sales_Order Order_NoDateCust_No 011/9/87C1 022/5/87C3 091/9/87C6 0101/9/87C6 SJOIN Customer AND Sales_Order

2.5 The manipulative part SEMI-JOIN Customer Cust_NoNameAddress C1Nippers Ltd25 High St, Leeds C2Tots-Gear5 Low, Oxford C3Super-Brat30 New St Luton C6Tiny-Togs1 Old Rd, Luton Sales_Order Order_NoDateCust_No 011/9/87C1 022/5/87C3 091/9/87C6 0101/9/87C6 SJOIN Customer AND Sales_Order Cust_NoNameAddress C1Nippers Ltd25 High St, Leeds C3Tots-Gear5 Low, Oxford C6Tiny-Togs1 Old Rd, Luton SEMI-NATURAL JOIN SEMI-EQUI JOIN SEMI-THETA JOIN SJOIN R AND S WHERE C

2.5 The manipulative part OUTER-JOIN OUTER-JOIN is an information-preserving version of the join. OJOIN Sales_Order AND Customer Order_NoDateCust_NoNameAddress 011/9/87C1Nippers Ltd25 High St, Leeds Null C2Tots-Gear5 Low, Oxford 022/5/87C3Tots-Gear5 Low, Oxford 091/9/87C6Tiny-Togs1 Old Rd, Luton 0101/9/87C6Tiny-Togs1 Old Rd, Luton

2.5 The manipulative part Complex Expression Have a close read pp. 57-59.

2.5 The manipulative part Summary Section 2.4.16 Reading: Section 2.4 Exercise: 2.5

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

Similar presentations