Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter (6) The Relational Algebra and Relational Calculus Objectives

Similar presentations


Presentation on theme: "Chapter (6) The Relational Algebra and Relational Calculus Objectives"— Presentation transcript:

1 Chapter (6) The Relational Algebra and Relational Calculus Objectives Learn about more complicated relational algebra that is used to manipulate relations and specifying queries.

2 Basic Relational Algebra Operations
A data model must include a set of operations to manipulate the data. A basic set of relational model operations constitute the relational algebra. These operations enable the user to specify basic retrieval requests. The result of a retrieval is a new relation, which may have been formed from one or more relations. A sequence of relational algebra operations forms a relational algebra expression, whose result will also be a relation. The relational algebra operations are two types: 1) set operations from mathematical set theory (UNION, INTERSECTION, SET DIFFERENCE, and CARTESIAN PRODUCT). 2) operations developed specifically for relational databases (SELECT, PROJECT, and JOIN, etc…).

3 The SELECT Operation The SELECT operation is used to select a subset of the tuples from a relation that satisfy a selection condition. The SELECT operation works like a filter that allows the tuples, which satisfy a specific qualification to pass through. Example: Select from EMPLOYEE those who work in DNO = 4 Select from EMPLOYEE those with Salary > 30,000 ……… The general form of SELECT is:

4 The SELECT Operation – cont.
The selection condition is a Boolean expression and R is generally a relational algebra expression which will result in a relation. The relation resulting from the SELECT operation has the same attributes as R. The Boolean expression may have one of the following forms: <attribute name> <comparison op> <constant value> OR <attribute name> <comparison op> <attribute name> attribute name: the name of an attribute R comparison op: one of the operators; Note: If the domains of the attributes are not ordered values (numeric or date) then we only can use An example is Color={red, blue, green, …}.

5 The SELECT Operation – cont.
The SELECTION process: The selection condition is applied independently to each tuple t in R (substitute each occurrence of an attribute Ai in the selection condition with its value in the tuple t[Ai]. If the condition evaluates to true, the tuple is selected. Choices for Boolean operations are; AND, OR, and NOT. Can you determine under what conditions these are true or false? The SELECT operator is unary (can be applied to only a single relation). In addition, the selection operation is applied to each tuple individually. The SELECT operation is commutative:

6 The SELECT Operation – cont.
We can also combine a cascade of SELECT operations into a single SELECT operation with conjunctive (AND) condition: Example: Select Senior Students, majoring in CS, with GPA higher than 3.0, who are from NC. In the Employee database: 1. Select all male employees who are working in department 5 with salary more than $30,000. 2. Select all employees who are working in Research department in Sugarland.

7 General form: The PROJECT Operation
The SELECT operation selects some of the rows from the table while discarding other rows. The PROJECT operation, on the other hand, selects certain columns from the table and discards the other columns. Example: To list each employee’s first and last name and salary, we can use the PROJECT operation as follows: General form:

8

9 The PROJECT Operation – cont.
If the attribute list includes only non-key attributes of R, duplicates are likely to occur. The PROJECT operation removes any duplicate tuples. This is known as duplicate elimination. Example: The result is shown on the previous page in Table (c). The tuple <F,25000> appears only once. The number of tuples in a relation resulting from a PROJECT operation is always less than or equal to the number of tuples in R. If the projection list is a superkey of R – that is, it includes some key of R – the resulting relation has the same number of tuples. Is this always true? This is true as long as <list2> contains the attributes in <list1>. Note: commutativity DOES NOT hold on PROJECT.

10 Sequence of Operations and the RENAME Operation
Sometimes we apply several relational algebra operations one after another. In such cases we have two choices: - write the operations as a single relational algebra expression by nesting the operations, or - apply one operation at a time and create intermediate result relations. In the later case, we must name the relations that hold the intermediate results. Example: list First name, Last name, and salary of all employees who work in department number 5. How do we do this? SELECT all employees in department 5, then PROJECT their First name, Last name and Salaries.

11

12 Sequence of Operations and the RENAME Operation – cont.
We can show the sequence of operations, giving a name to each intermediate relation: We can also use this technique to rename the attributes in the intermediate and result relations. See Figure 9.b.

13 The RENAME Operation The general format of the RENAME operation is: To rename both the relation and its attributes we will use: To rename the relation only: To rename the attributes only:

14 Set Theoretic Operations
These are the set of standard mathematical operations. Example: Retrieve the SSN of all employees who either work in department 5 OR directly supervise an employee who works in department 5. Two relations participating in a UNION must be union compatible, i.e., they must have the same degrees and same domains.

15 Set Theoretic Operations – cont.
Other theoretic operators are: UNION, INTERSECTION, and SET DIFFERENCE. These are all binary operators. They are defines as: UNION: The result of this operation, denoted by , 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. INTERSECTION: The result of this operation, denoted by , is a relation that includes all tuples that are in both R and S. SET DIFFERENCE: 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 UNION and INTERSECTION are commutative operations. That is: Example: List the First name and Last name of those who are either a student or an employee. Fig b. List those who are both student and instructor. Fig c.

16 Figure 11. Two Union Compatible Relations STUDENT INSTRUCTOR STUDENT INSTRUCTOR INSTRUCTOR-STUDENT STUDENT - INSTRUCTOR

17 Set Theoretic Operations – cont.
Both union and intersection can be treated as n-ray operations applicable to any number of relations as both are associative operations; that is: The DIFFERENCE operation is not commutative; that is, List the name of students who are not instructors. Fig d. What is shown on Fig e?

18 The CARTESIAN PRODUCT Operation (CROSS PRODUCT or CROSS JOIN)
This is denoted by X, which is also a binary set operation, but the relations on which it is applied do not have to be union compatible. In general, we have: R(A1, A2, …,An) X S(B1, B2, …,Bm) = Q(A1, A2, …,An,B1, B2, …,Bm) Where, Q with n+m attributes in the order shown above. Example: For each female employee, list the names of her dependents:

19 Example: For each female employee, list the names of her dependents:

20

21

22 The JOIN Operations The join operation is denoted by , is used to combine related tuples from two relations into single tuples. Example: List all department names and managers of those departments. What do we need to do to get this?

23 Now use JOIN and re-write the two lines before the RESULT:
Example: For each female employee, list the names of her dependents: Now use JOIN and re-write the two lines before the RESULT:

24 Replace these two lines:
With this:

25 The JOIN Operations – cont.
The general form of JOIN operations is: This will result in a relation Q with n+m attributes. What is the difference between CARTESIAN PRODUCT and JOIN? JOIN: will combine all tuples that satisfy the join condition. CARTESIAN PRODUCT: will combine all tuples. A general join condition is of the form: <condition> AND <condition> AND …<condition> Where each condition is of the form Ai Bj, Ai is an attribute of R, Bj is an attribute of S, Ai and Bj have the same domain, and refers to one of the operations: A JOIN operation with such a general join condition is called a THETA JOIN.

26 The JOIN Operations – cont.
The most common JOIN involves join condition with equality comparisons only. Such a JOIN is called EQUIJOIN. What do you observer in Figure 7.13 (MGSSN & SSN)? In order to avoid repetition of same entities we use NATURAL JOIN which is denoted by *. The standard definition of NATURAL JOIN requires that the two join attributes (or each pair of join attributes) have the same name in both relations. If that is not the case, the renaming operation is applied first: In the above example, the attribute DNUM is called the join attribute.

27 The JOIN Operations – cont. Example:
PROJ_DEPT PNAME PNUMBER PLOCATION DNUM DNAME MGRSSN MGRSTARTDATE DEPT_LOCS DNAME DNUMBER MGRSSN MGRSTARTDATE LOCATION How many ways can we JOIN the above relations? The more general but not standard definition for NATURAL-JOIN is: What if there is nothing that can be joined? A JOIN operation can be used in combination. What does the following do?

28 A Complete Set of Relational Algebra Operations
It has been shown that the set of relational algebra operations is complete set. What does it mean? It means that any of the other relational algebra operations can be expressed as a sequence of operations from this set. Example: This how the INTERSECTION operation is represented in terms of others. Show the JOIN operation in terms of CARTESIAN PRODUCT and SELECT. A NATURAL JOIN can be specified as a CARTESIAN PRODUCT preceded by RENAME and followed by SELECT and PROJECT operations.

29 The DIVISION Operations
This is useful for a special kind of query. Example: Retrieve the names of employees who work on all the projects that ‘John Smith’ works on. What are the steps that you will take to do this? ? The DIVISION operator can be expressed as a sequence of as follow:

30 Example: Retrieve the names of employees who work on all the projects that ‘John Smith’ works on.

31

32 TR DIVIDED by S:

33 Example: R divided by S  R is T1 and S is T2
Compute T1: project b from R : [b1 b2 b3 b4] Compute T2: project b from ( (SXT1) – R) SXT1 = a1b1, a2b1, a3b1, a1b2, a2b2, a3b2, a1b3, a2b3, a3b3, a1b4, a2b4, a3b4 SXT1 – R = a2b2, a1b3 T2 = Project b from the above: b2, b3 Compute T: T1 – T2 [b1, b4]

34 Aggregate Functions and Grouping
The mathematical aggregate functions on collection of values from the database cannot be expressed using basic relational algebra. Example: Finding the average or total salary of all employees. Common operations are: SUM, AVERAGE, MAXUMUM, and MINIMUM. The COUNT is another useful one. Where the <grouping attributes> is the list of attributes of the relation specified in R. <function list> is a list (<function> <attribute>) pairs. <function> is one of the allowed functions (AVERAGE, MAXIMUM,…).

35 Aggregate Functions and Grouping - cont
Example: List each department number, number of employees in the department, and their average salary:

36 Aggregate Functions and Grouping - cont Example:
List each department number, number of employees in the department, and their average salary: Did we need to use the renaming? If we do not use it, then the resulting relation that correspond to the function list will each be the concatenation of the function name with the attribute name in the form <function>_<attribute>. How about duplications?

37 Recursive Closure Operations
This operation also cannot be specified using relational algebra. A good example is the relation between an employee and a supervisor. A supervisor may supervise some employees who also are supervising others and so on. It is straightforward to specify, using relational algebra, all employees who are supervised by an employee e at a specific level. It is difficult to specify all supervisees at all levels. Example: Find the SSNs of all employees e’ directly supervised – at level one – by employee e whose name is ‘James Borg’: To retrieve all employees supervised at level 1 and 2 by ‘James Borg’: To get both sets of employees supervised at levels 1 and 2 by him:

38 OUTER JOIN Operation The JOIN operations match tuples that satisfy the join condition. For instance in R*S, only tuples from R that have matching tuples in S and vice versa appear in the result. The non-matched tuples are eliminated. The OUTER JOIN allow us to keep all tuples whether or not they have matching tuples in the other relation. Example: Suppose we want to have the list of all employee names and also the name of the departments they manage.

39 OUTER JOIN Operation Example: Suppose we want to have the list of all employee names and also the name of the departments they manage. We can apply a LEFT OUTER JOIN to retrieve the result: The LEFT OUTER JOIN operation keeps every tuple in the first relation R in statement: R (LEFT OUTER JOIN) S. If no matching tuple was found, then the attributes of S in the result will have null for those unmatched tuples. Note we also have the RIGHT OUTER JOIN and FULL OUTER JOIN

40

41 OUTER UNION Operation The UNION operation requires union compatibility of participating relations. OUTER UNION operation will take the UNION of tuples in two relations that are partially compatible. That means only some of the attributes are union compatible. The attributes that are not union compatible from either relation are kept in the result, and tuples that have no values for these attributes are padded with null values. Example: What is the OUTER UNION of STUDENT and FACULTY given below: STUDENT Name SSN Department Advisor FACULTY Name SSN Department Rank R(Name, SSN, Department, Advisor, Rank) and all tuples from both are included. Student will have null for rank whereas faculty will have null for the advisor attributes.

42 Can you specify this query in another way?

43

44

45


Download ppt "Chapter (6) The Relational Algebra and Relational Calculus Objectives"

Similar presentations


Ads by Google