Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Relational Algebra and Calculus

Similar presentations


Presentation on theme: "The Relational Algebra and Calculus"— Presentation transcript:

1

2 The Relational Algebra and Calculus
Chapter 6 The Relational Algebra and Calculus

3 Relational Algebra Algebra Relational Algebra
Mathematical system consisting of: Operands --- variables or values from which new values can be constructed. Operators --- symbols denoting procedures that construct new values from given values. Relational Algebra An algebra whose operands are relations or variables that represent relations. The result can be used as a query language to specify basic retrieval requests.

4 Core Relational Algebra
Unary Operations: Selection : picking certain rows [tuples]. Projection : picking certain columns. Renaming of relations and attributes. Binary Operations: Union , intersection , and difference - . require both operands have the same relation schema. Products X and joins : compositions of relations.

5 Relational Algebra Operations
© Pearson Education Limited 1995, 2005

6 Selection (or Restriction)
SELECT operation is used to select a subset of the tuples from a relation that satisfy a selection condition. Example: To select the EMPLOYEE tuples whose department number is four or those whose salary is greater than $30,000 the following notation is used: DNO = 4(EMPLOYEE) SALARY > 30,000(EMPLOYEE) Select operation is denoted by <selection condition>(R) The symbol  (sigma) is used to denote the select operator the selection condition is a Boolean expression specified on the attributes of relation R

7 Selection Example s(DNO=4 AND SALARY>25000) OR (DNO=5 AND SLARY>30000) (EMPLOYEE)

8 LNAME, FNAME,SALARY (EMPLOYEE)
Projection PROJECT operation selects certain columns from the table Creates a vertical partitioning. Example: To list each employee’s first and last name and salary, the following is used: LNAME, FNAME,SALARY (EMPLOYEE) The general form of the project operation is <attribute list>(R) where  (pi) is the symbol used to represent the project operation <attribute list> is the desired list of attributes from the attributes of relation R. The project operation removes any duplicate tuples, if any.

9 Projection Example pLNAME, FNAME,SALARY (EMPLOYEE)
pSEX, SALARY (EMPLOYEE)

10 Renaming p FNAME, LNAME, SALARY (s DNO=5 (EMPLOYEE))
We may want to apply several relational algebra operations one after the other. we can write them as a single relational algebra expression by nesting or we can apply one operation at a time and create intermediate relations In the latter case, we must give names to the relations that hold the intermediate results. Example: To retrieve the first name, last name, and salary of all employees who work in department number 5: p FNAME, LNAME, SALARY (s DNO=5 (EMPLOYEE)) OR We can explicitly show the sequence of operations, giving a name to each intermediate relation: TEMP ¬s DNO=5 (EMPLOYEE) R ¬ p FNAME, LNAME, SALARY (TEMP)

11 Renaming Example (a) pFNAME, LNAME, SALARY(sDNO=5(EMPLOYEE)) (b) Using intermediate relations and renaming of attributes.

12 Renaming (cont.) The rename operator is r
The general Rename operation can be expressed by any of the following forms: r S (B1, B2, …, Bn ) ( R) is a renamed relation S based on R with column names B1, B1,…..Bn. r S ( R) is a renamed relation S based on R (which does not specify column names). r (B1, B2, …, Bn ) ( R) is a renamed relation with column names B1, B2, …..Bn which does not specify a new relation name.

13 UNION Operation The result of this operation, denoted by R È S, is a relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated. Example: To retrieve the social security numbers of all employees who either work in department 5 or directly supervise an employee who works in department 5: DEP5_EMPS ¬ sDNO=5 (EMPLOYEE) RESULT1 ¬ p SSN(DEP5_EMPS) RESULT2(SSN) ¬ p SUPERSSN(DEP5_EMPS) RESULT ¬ RESULT1 È RESULT2 The union operation produces the tuples that are in either RESULT1 or RESULT2 or both. The two operands must be “type compatible”.

14 UNION Example1 RESULT ¬ RESULT1  RESULT2

15 UNION Operation (cont.)
Type Compatibility The operand relations R1(A1, A2, ..., An) and R2(B1, B2, ..., Bn) must have the same number of attributes, and the domains of corresponding attributes must be compatible; that is, dom(Ai)=dom(Bi) for i=1, 2, ..., n. The resulting relation for R1ÈR2,R1 Ç R2, or R1-R2 has the same attribute names as the first operand relation R.

16 UNION Example2 STUDENTÈINSTRUCTOR

17 INTERSECTION OPERATION
The result of this operation, denoted by R Ç S, is a relation that includes all tuples that are in both R and S. The two operands must be "type compatible" Example: The result of the intersection operation (figure below) includes only those who are both students and instructors. STUDENT ÇINSTRUCTOR

18 Set Difference (or MINUS) Operation
The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S. The two operands must be "type compatible”. Example: The figure shows the names of students who are not instructors, and the names of instructors who are not students. STUDENT-INSTRUCTOR INSTRUCTOR-STUDENT


Download ppt "The Relational Algebra and Calculus"

Similar presentations


Ads by Google