Date post: | 19-Jan-2015 |
Category: |
Technology |
Upload: | rushdi-shams |
View: | 92 times |
Download: | 1 times |
Rushdi Shams, Dept of CSE, KUET 1
Database Database SystemsSystems
Relational AlgebraRelational Algebra
Version 1.0Version 1.0
2Rushdi Shams, Dept of CSE, KUET
What is Relational What is Relational AlgebraAlgebra
It is a collection of operations on It is a collection of operations on relationsrelations
If you apply relational algebra on If you apply relational algebra on some relations, the result will be some relations, the result will be another relationanother relation
You can say a relation- a table.You can say a relation- a table. So, operation on tables=another tableSo, operation on tables=another table
That is what relational algebra does!That is what relational algebra does!
3Rushdi Shams, Dept of CSE, KUET
Memory RefreshmentMemory Refreshment A relation is a set of attributes with values for A relation is a set of attributes with values for
each attribute such that: each attribute such that: Each attribute value must be a single value Each attribute value must be a single value
only (atomic). only (atomic). All values for a given attribute must be of the All values for a given attribute must be of the
same type (or domain). same type (or domain). Each attribute name must be unique. Each attribute name must be unique. The order of attributes is insignificant The order of attributes is insignificant No two rows (tuples) in a relation can be No two rows (tuples) in a relation can be
identical. identical. The order of the rows (tuples) is insignificant. The order of the rows (tuples) is insignificant.
4Rushdi Shams, Dept of CSE, KUET
2 types of operation2 types of operation
1.1. Set theory operations:Set theory operations:Union, Intersection, Difference and Union, Intersection, Difference and Cartesian product. Cartesian product.
2.2. Specific Relational Operations:Specific Relational Operations:Selection, Projection, Join, Division Selection, Projection, Join, Division
We will take a look into set theory We will take a look into set theory operations, and then we will dig operations, and then we will dig into specific relational operationsinto specific relational operations
5Rushdi Shams, Dept of CSE, KUET
Our example tablesOur example tables
There are 2 tables- There are 2 tables- R and S, we will R and S, we will play with them!! play with them!!
6Rushdi Shams, Dept of CSE, KUET
UnionUnion
RUS means, a union RUS means, a union operation on R and operation on R and S that will produce S that will produce another table which another table which will have all the will have all the rows of R and S rows of R and S except the except the duplicatesduplicates
There are 2 Sally in There are 2 Sally in R and S, so, just R and S, so, just take 1 from themtake 1 from them
7Rushdi Shams, Dept of CSE, KUET
DifferenceDifference
R-S means, a R-S means, a difference operation difference operation on R and S that will on R and S that will produce another produce another table which will table which will have all the rows have all the rows which is in R but which is in R but not in S.not in S.
As Sally is in R and As Sally is in R and in S, so, she has in S, so, she has been omitted.been omitted.
8Rushdi Shams, Dept of CSE, KUET
IntersectionIntersection
R∩S means, an R∩S means, an intersection intersection operation on R and operation on R and S that will produce S that will produce another table which another table which will have all the will have all the rows which are in rows which are in both R and S.both R and S.
As Sally is in R and As Sally is in R and in S, so, she has in S, so, she has been selected.been selected.
9Rushdi Shams, Dept of CSE, KUET
More UnionMore Union
10Rushdi Shams, Dept of CSE, KUET
More IntersectionMore Intersection
11Rushdi Shams, Dept of CSE, KUET
More DifferenceMore Difference
12Rushdi Shams, Dept of CSE, KUET
ConstraintsConstraints You see tables, and start performing set You see tables, and start performing set
theory operations on them- MISTAKE!! theory operations on them- MISTAKE!! You should remember the following You should remember the following constraints before you perform set theory constraints before you perform set theory operations on tables.operations on tables.
1.1. Attributes of relations need Attributes of relations need notnot be be identical to perform union, intersection identical to perform union, intersection and difference operations. and difference operations.
2.2. However, they However, they must havemust have the same the same number of attributes and the domains for number of attributes and the domains for corresponding attributes must be corresponding attributes must be identical. identical.
13Rushdi Shams, Dept of CSE, KUET
Other things to Other things to rememberremember
Union, Intersection and difference Union, Intersection and difference operators may only be applied to operators may only be applied to Union Compatible relations. Union Compatible relations.
Union and Intersection are Union and Intersection are commutative operationscommutative operationsRUS= SURRUS= SURR∩S = S∩R R∩S = S∩R
Difference operation is Difference operation is NOTNOT commutative.commutative.R - S not equal S - R R - S not equal S - R
14Rushdi Shams, Dept of CSE, KUET
Class PerformanceClass Performance
Find out RUTFind out RUT Find out R∩TFind out R∩T Find out R-TFind out R-T Find out T-RFind out T-R
15Rushdi Shams, Dept of CSE, KUET
Cartesian ProductCartesian Product It provides all the combinations of rows from two tables.It provides all the combinations of rows from two tables. If A and B are 2 tables and A has 3 rows and B has 4 rows, If A and B are 2 tables and A has 3 rows and B has 4 rows,
then Athen A×B is-×B is-A1-B1A1-B1A1-B2A1-B2A1-B3A1-B3A1-B4A1-B4A2-B1A2-B1A2-B2A2-B2A2-B3A2-B3A2-B4A2-B4A3-B1A3-B1A3-B2A3-B2A3-B3A3-B3A3-B4A3-B4
16Rushdi Shams, Dept of CSE, KUET
Cartesian Product Cartesian Product (continued)(continued)
17Rushdi Shams, Dept of CSE, KUET
More ProductMore Product
18Rushdi Shams, Dept of CSE, KUET
SelectionSelection
Selection and Projection are Selection and Projection are unaryunary operators. operators.
The selection operator is The selection operator is sigma (sigma (σσ)) The selection operation acts like a The selection operation acts like a
filter on a relation by filter on a relation by returning only returning only a certain number of rows. a certain number of rows.
The The resulting relation will have the resulting relation will have the same number of attributessame number of attributes as the as the original relation. original relation.
19Rushdi Shams, Dept of CSE, KUET
Selection (continued)Selection (continued) The resulting relation The resulting relation maymay have fewer have fewer
rows than the original relation. rows than the original relation. The rows to be returned are dependent The rows to be returned are dependent
on a condition that is part of the selection on a condition that is part of the selection operator. operator.
σσC C (R) Returns only those rows in R that (R) Returns only those rows in R that satisfy condition satisfy condition CC
A condition C can be made up of any A condition C can be made up of any combination of comparison or logical combination of comparison or logical operators that operate on the attributes operators that operate on the attributes of R.of R.
20Rushdi Shams, Dept of CSE, KUET
Selection ExampleSelection Example
We have a table named EMP.We have a table named EMP. We will play with this table named EMP We will play with this table named EMP
for a while!for a while!
21Rushdi Shams, Dept of CSE, KUET
Selection Example Selection Example (continued)(continued)
Select only those Employees in the CS Select only those Employees in the CS department department
σσ Dept = ‘CS’Dept = ‘CS’ (EMP) (EMP)
22Rushdi Shams, Dept of CSE, KUET
Selection Example Selection Example (continued)(continued)
Select only those Employees with last Select only those Employees with last name Smith who are assistant professorsname Smith who are assistant professors
σσ Name = ‘Smith’ Name = ‘Smith’ ΛΛ Rank = ‘Assistant’ Rank = ‘Assistant’ (EMP) (EMP)
23Rushdi Shams, Dept of CSE, KUET
Selection Example Selection Example (continued)(continued)
Select only those Employees who are Select only those Employees who are either Assistant Professors or in the either Assistant Professors or in the Economics department Economics department
σσ Dept = ‘Econ’ V Rank = ‘Assistant’Dept = ‘Econ’ V Rank = ‘Assistant’ (EMP) (EMP)
24Rushdi Shams, Dept of CSE, KUET
Selection Example Selection Example (continued)(continued)
Select only those Employees who are not Select only those Employees who are not in the CS department or Adjuncts in the CS department or Adjuncts
σσ ¬¬(Dept = ‘CS’ V Rank = ‘Adjunct’)(Dept = ‘CS’ V Rank = ‘Adjunct’) (EMP) (EMP)
25Rushdi Shams, Dept of CSE, KUET
More SelectionMore Selection
26Rushdi Shams, Dept of CSE, KUET
Class PerformanceClass Performance
σσ(Rank = 'Adjunct' (Rank = 'Adjunct' ΛΛ Dept = 'CS') Dept = 'CS') (EMP) (EMP)
σσ Rank = 'Associate'Rank = 'Associate' ΛΛ Dept = 'CS' Dept = 'CS' EMP ) EMP )
σσ Dept = 'CS'Dept = 'CS' VV Rank = 'Associate'Rank = 'Associate' EMP ) EMP )
σσ Age > 26Age > 26 (R U S) (R U S)
27Rushdi Shams, Dept of CSE, KUET
ProjectionProjection Projection is also a Projection is also a UnaryUnary operator. operator. The Projection operator is The Projection operator is pi (pi (ππ)) Projection limits the columns that will be returned Projection limits the columns that will be returned
from the original relation. from the original relation. The general syntax is: The general syntax is: ππ attributesattributes R R
Where attributes is the list of attributes to be Where attributes is the list of attributes to be displayed and R is the relation. displayed and R is the relation.
The resulting table will have the same number of The resulting table will have the same number of rows as the original relation (unless there are rows as the original relation (unless there are duplicate rows produced). duplicate rows produced).
The number of columns of the resulting relation The number of columns of the resulting relation may be equal to or less than that of the original may be equal to or less than that of the original relation. relation.
28Rushdi Shams, Dept of CSE, KUET
Projection ExampleProjection Example
Project only the names and departments Project only the names and departments of the employees of the employees
ππ name, deptname, dept (EMP) (EMP)
29Rushdi Shams, Dept of CSE, KUET
Combining selection & Combining selection & projectionprojection
Show the names of all employees working Show the names of all employees working in the CS department in the CS department
ππ name name ( (σσ Dept = 'CS'Dept = 'CS' (EMP) ) (EMP) )
30Rushdi Shams, Dept of CSE, KUET
Combining selection & Combining selection & projection (continued)projection (continued)
Show the name and rank of those Employees Show the name and rank of those Employees who are not in the CS department or who are not in the CS department or Adjuncts Adjuncts
ππ name, rankname, rank ( ( σσ ¬¬(Rank = 'Adjunct' V Dept = 'CS')(Rank = 'Adjunct' V Dept = 'CS') (EMP) ) (EMP) )
31Rushdi Shams, Dept of CSE, KUET
More ProjectionMore Projection
32Rushdi Shams, Dept of CSE, KUET
Class PerformanceClass Performance
ππ name, rankname, rank ( ( σσ ¬¬(Rank = 'Adjunct' (Rank = 'Adjunct' ΛΛ Dept = 'CS') Dept = 'CS') (EMP) ) (EMP) )
ππ fname, agefname, age ( (σσ Age > 22Age > 22 (R U S) ) (R U S) )
σσ office > 300office > 300 ( ( ππ name, rankname, rank (EMP)) (EMP))
33Rushdi Shams, Dept of CSE, KUET
Aggregate FunctionsAggregate Functions
We can also apply Aggregate functions to We can also apply Aggregate functions to columns and rowscolumns and rows
1.1. SUM SUM
2.2. MINIMUM MINIMUM
3.3. MAXIMUM MAXIMUM
4.4. AVERAGE, MEAN, MEDIAN AVERAGE, MEAN, MEDIAN
5.5. COUNT COUNT Aggregate functions are sometimes written Aggregate functions are sometimes written
using the Projection operator or the using the Projection operator or the Tau Tau character (character (ττ))
34Rushdi Shams, Dept of CSE, KUET
Aggregate Functions Aggregate Functions (continued)(continued)
Find the minimum SalaryFind the minimum Salary
ττ MIN (salary)MIN (salary) (EMP) (EMP) Find the average SalaryFind the average Salary
ττ AVG (salaryAVG (salary) (EMP) ) (EMP) Count the number of employees in the CS departmentCount the number of employees in the CS department
ττ COUNT (name)COUNT (name) ( ( σσ Dept = 'CS'Dept = 'CS' (EMP) ) (EMP) ) Find the total payroll for the Economics departmentFind the total payroll for the Economics department
ττ SUM (salary)SUM (salary) ( (σσ Dept = 'Econ'Dept = 'Econ' (EMP) ) (EMP) )
35Rushdi Shams, Dept of CSE, KUET
JoinJoin Join operations bring together two tables and Join operations bring together two tables and
combine their rows and columns in a specific combine their rows and columns in a specific fashion. fashion.
The generic join operator called the The generic join operator called the Theta JoinTheta Join is is It takes as arguments the columns from the two It takes as arguments the columns from the two
tables that are to be joined. tables that are to be joined. The join condition can be The join condition can be When the join condition operator is When the join condition operator is = = then we then we
call this an Equijoincall this an Equijoin Note that the columns in common are repeated. Note that the columns in common are repeated.
36Rushdi Shams, Dept of CSE, KUET
Join (continued)Join (continued)
Now, we have two tables Emp and depart.Now, we have two tables Emp and depart. We will play with these 2 tables for a We will play with these 2 tables for a
while!while!
37Rushdi Shams, Dept of CSE, KUET
Join (continued)Join (continued)
Find all information on every employee Find all information on every employee including their department infoincluding their department info
EMP EMP emp.Dept = depart.Deptemp.Dept = depart.Dept DEPART DEPART
38Rushdi Shams, Dept of CSE, KUET
Join (continued)Join (continued)
Take a look, one table has 5 rows, the Take a look, one table has 5 rows, the other has 4 rows, the resultant has 5 other has 4 rows, the resultant has 5 rows!rows!
39Rushdi Shams, Dept of CSE, KUET
Join (continued)Join (continued)
Find all information on every employee including Find all information on every employee including department info where the employee works in an department info where the employee works in an office numbered less than the department main officeoffice numbered less than the department main office
EMP EMP (emp.office < depart.mainoffice) (emp.office < depart.mainoffice) ΛΛ (emp.dept = depart.dept) (emp.dept = depart.dept) DEPART DEPART
40Rushdi Shams, Dept of CSE, KUET
Natural JoinNatural Join
Notice in the generic (Theta) join Notice in the generic (Theta) join operation, any attributes in common operation, any attributes in common (such as dept above) are repeated. (such as dept above) are repeated.
The Natural Join operation removes The Natural Join operation removes these duplicate attributes. these duplicate attributes.
The natural join operator is: The natural join operator is: ** We can also assume using We can also assume using ** that the that the
join condition will be join condition will be == on the two on the two attributes in common. attributes in common.
41Rushdi Shams, Dept of CSE, KUET
Natural Join (continued)Natural Join (continued)
Emp * departEmp * depart There are 2 Dept columns, one is omitted!There are 2 Dept columns, one is omitted!
42Rushdi Shams, Dept of CSE, KUET
Outer JoinOuter Join In the Join operations so far, only those tuples In the Join operations so far, only those tuples
from both relations that satisfy the join from both relations that satisfy the join condition are included in the output relation. condition are included in the output relation.
The Outer join includes other rows as well The Outer join includes other rows as well according to a few rules. according to a few rules.
Three types of outer joins: Three types of outer joins: 1.1. Left Outer Join includes all rows in the left Left Outer Join includes all rows in the left
hand relation and includes only those matching hand relation and includes only those matching rows from the right hand relation. rows from the right hand relation.
2.2. Right Outer Join includes all rows in the right Right Outer Join includes all rows in the right hand relation and includes only those matching hand relation and includes only those matching rows from the left hand relation. rows from the left hand relation.
3.3. Full Outer Join includes all rows in the left Full Outer Join includes all rows in the left hand relation and from the right hand relation. hand relation and from the right hand relation.
43Rushdi Shams, Dept of CSE, KUET
Outer Join (continued)Outer Join (continued)
Now we have another 2 tables.Now we have another 2 tables. We will play with them for next few slides!We will play with them for next few slides!
44Rushdi Shams, Dept of CSE, KUET
Left Outer JoinLeft Outer Join
PEOPLE PEOPLE people.food = menu.foodpeople.food = menu.food MENU MENU
45Rushdi Shams, Dept of CSE, KUET
Right Outer JoinRight Outer Join
PEOPLE PEOPLE people.food = menu.foodpeople.food = menu.food MENU MENU
46Rushdi Shams, Dept of CSE, KUET
Full Outer JoinFull Outer Join
PEOPLE PEOPLE people.food = menu.foodpeople.food = menu.food MENU MENU
47Rushdi Shams, Dept of CSE, KUET
Outer UnionOuter Union
In this case, just go from row of table 1 to row of table 2.In this case, just go from row of table 1 to row of table 2. Take common columns just once.Take common columns just once. if matching criteria don’t match, then put NULL, else if matching criteria don’t match, then put NULL, else
include in the tableinclude in the table Repeat the same but this time go from row of table 2 to Repeat the same but this time go from row of table 2 to
row of table 1row of table 1
48Rushdi Shams, Dept of CSE, KUET
ReferencesReferences
Database Management Systems Database Management Systems by Prof. Holowczak, Zicklin by Prof. Holowczak, Zicklin School of Business, Baruch School of Business, Baruch College, City University of New College, City University of New YorkYork
Database Systems: Design, Database Systems: Design, Implementation & Management Implementation & Management by Rob & Coronel, 6by Rob & Coronel, 6thth Edition Edition