Professional Documents
Culture Documents
DB Ch06
DB Ch06
IS222
DATA MANIPULATION
LANGUAGES
Chapter 6 Outline
Relational
SQL Optimized
Algebra Execution
Query RA Plan
(RA) Plan
Relational
SQL Optimized
Algebra Execution
Query RA Plan
(RA) Plan
SQL:
Returns all tuples which
SELECT *
satisfy a condition FROM Students
Notation: c(R) WHERE gpa > 3.5;
Examples
– Salary > 40000 (Employee)
RA:
– name = “Smith” (Employee)
The condition c can be
=, <, , >, , <>
Select
To process a selection,
– Look at each tuple
– See if we have a match (based on the condition)
The Degree of the resulting relation is the same
as the degree of the relation i.e resultant relation
as same number of attribute as base relation
σ is Commutative: σc1 (σc2(R)) = σc2 (σc1(R))
σsalary>30000 (σDno=5(R)) = σDno=5 (σsalary>30000 (R))
Selection Example
Intersection
Minus
Cartesian Product
UNION Requirement
Union is Commutative
RS=SR
Two relation has same number of column
Slide 1-30
Union Example
CS
Business
CS Business
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
UNION: Example
DEP5_EMPS
RESULT
RESULT1 RESULT2
CS Business
CID ProgID Cred_Hrs CourseTitle
C4567 P9873 4 Business 1
SET DIFFERENCE
SET DIFFERENCE (also called MINUS or
EXCEPT) is denoted by –
–It is denoted by X
Course x Registration
Course Registration
CID CourseTitle SID Name CourseId
– FEMALE_EMPS SEX=’F’(EMPLOYEE)
– EMPNAMES FNAME, LNAME, SSN (FEMALE_EMPS)
– EMP_DEPENDENTS EMPNAMES x DEPENDENT
Rows = 3 * 7= 21
Out of 21 rows only one row is valid, which can be selected by using
select operator Slide 1-50
Example of applying CARTESIAN PRODUCT
To keep only combinations where the
DEPENDENT is related to the EMPLOYEE,
we add a SELECT operation as follows
ACTUAL_DEPS SSN=ESSN(EMP_DEPENDENTS)
Join
Division
JOIN
JOIN is a special form of cross product of two tables.
– In Cartesian product we join a tuple of one table with the every
tuples of the second table.
But in join there is a special requirement of relationship
between tuples.
– For example if there is a relation STUDENT and a relation
BOOK then it may be required to know that how many books
have been issued to any particular student.
– Now in this case the primary key of STUDENT that is stId is a
foreign key in BOOK table through which the join can be
made.
JOIN allows us to process relationships among relations.
JOIN
JOIN Operation (denoted by )
– Join is a special form of cross product of two tables. In
Cartesian product we join a tuple of one table with the tuples
of the second table.
– The problem in CARTESIAN PRODUCT is that all
combinations of tuples are included in the result
The sequence of CARTESIAN PRODECT followed by SELECT is
used quite commonly to identify and select related tuples from two
relations
– The resulting relation state has one tuple for each combination of
tuples—r from R and s from S, but only if they satisfy the join
condition r[Ai]=s[Bj]
– Hence, if R has nR tuples, and S has nS tuples, then the join result
will generally have less than nR * nS tuples.
JOIN: Example
Example: Suppose that we want to retrieve the name of the manager
of each department.
– To get the manager’s name, we need to combine each DEPARTMENT
tuple with the EMPLOYEE tuple whose SSN value matches the
MGRSSN value in the department tuple.
R S Q
AB C D C D E A B C D E
3 6 2 3 4 1 9 3 6 2 3 7
4 8 4 1 2 3 7 4 8 4 1 9
– The implicit join condition includes each pair of attributes with the same
name, “AND”ed together:
R.C=S.C AND R.D = S.D
DEPARTMENT.DNUMBER=DEPT_LOCATIONS.DNUMBER
INNER JOIN
The JOIN operations described earlier match
tuples that satisfy the join condition.
– For example, for a NATURAL JOIN operation R *
S, only tuples from R that have matching tuples in
S—and vice versa—appear in the result.
Hence, tuples without a matching (or related)
tuple are eliminated from the JOIN result.
Tuples with NULL values in the join attributes are
also eliminated.
This type of join, where tuples with no match are
eliminated, is known as an Inner join.
– All the previous JOIN are Inner join
OUTER JOINS
FACULTY COURSE
Denoted by ÷
– Example: retrieve the names of employees who
work on all the projects that ‘John Smith’
works on
Apply to relations R(Z) ÷ S(X)
– Attributes of R are a subset of the attributes of S
Example of DIVISION
Query tree
– Represents the input relations of query as leaf
nodes of the tree
– Represents the relational algebra operations as
internal nodes
Q2: For every project located in “Stafford”, list the project
number, the controlling department number, and the
department manager’s last name, address, and Bdate
Relational Algebra Query Trees
List all the managers that work in the sales department.
(emp.deptno = dept.deptno)
Intermediate
operations
X
Leaves
EMP DEPT
Relational Algebra Query Trees
Alternative‘query tree’ for the query to list all the managers that work
in the sales department:
emp.deptno = dept.deptno
EMP DEPT
Relational Algebra Query Trees
Alternative‘query tree’ for the query to list all the managers that work
in the sales department:
emp.deptno = dept.deptno
EMP DEPT
Additional Relational Operations
– Ʒ SUM Salary (EMPLOYEE) retrieves the sum of the Salary from the
EMPLOYEE relation