Slide 6- 1 Chapter 6 The Relational Algebra and Calculus Relational Algebra 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. Slide 6- 3 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. Slide 6- 4 Relational Algebra Operations © Pearson Education Limited 1995, 2005 Slide 6- 5 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 Slide 6- 6 Selection Example (DNO=4 AND SALARY>25000) OR (DNO=5 AND SLARY>30000) (EMPLOYEE) Slide 6- 7 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. Slide 6- 8 Projection Example LNAME, FNAME,SALARY (EMPLOYEE) SEX, SALARY (EMPLOYEE) Slide 6- 9 Renaming 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: FNAME, LNAME, SALARY ( DNO=5 (EMPLOYEE)) OR We can explicitly show the sequence of operations, giving a name to each intermediate relation: TEMP DNO=5 (EMPLOYEE) R FNAME, LNAME, SALARY (TEMP) Slide 6- 10 Renaming Example (a) FNAME, LNAME, SALARY(DNO=5(EMPLOYEE)) (b) Using intermediate relations and renaming of attributes . Slide 6- 11 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. Slide 6- 12 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 DNO=5 (EMPLOYEE) RESULT1 SSN(DEP5_EMPS) RESULT2(SSN) 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”. Slide 6- 13 UNION Example1 RESULT RESULT1 RESULT2 Slide 6- 14 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 R1R2,R1 R2, or R1R2 has the same attribute names as the first operand relation R. Slide 6- 15 UNION Example2 STUDENTINSTRUCTOR Slide 6- 16 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 Slide 6- 17 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 Slide 6- 18
© Copyright 2026 Paperzz