Notes of Database Management System
UNIT -2 RELATIONAL ALGEBRA A query language is a language in which a user requests information from the database. These languages are usually on a level higher than that of a standard programming language. Query languages can be categorized as either procedural or nonprocedural. In a procedural language, the user instructs the system to perform a sequence of operations on the database to compute the desired result. In a nonprocedural language, the user describes the desired information without giving a specific procedure for obtaining that information. The relational algebra is a procedural query language. It consists of a set of operations that take one or two relations as input and produce a new relation as their result. The fundamental operations in the relational algebra are select, project, union, set difference, Cartesian product, and rename. In addition to the fundamental operations, there are several other operations—namely, set intersection, natural join, division, and assignment. Standard Ways: –
–
Two formal "languages": •
Relational Algebra - allows the description of queries as a series of operations;
•
Relational Calculus - allows the description of the desired result.
One real language: •
SQL - the standard relational database manipulation language.
It is a collection of operations on relations .If you apply relational algebra on some relations, the result will be another relation You can say a relation- a table. So, operation on tables = another table
Relational Algebra –Unary Relational Operations –Relational Algebra Operations From Set Theory –Binary Relational Operations –Additional Relational Operations
Relational Calculus –Tuple Relational Calculus –Domain Relational Calculus
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 1
Notes of Database Management System Refreshment of Memory:
A relation is a set of attributes with values for each attribute such that: Each attribute value must be a single value only (atomic). All values for a given attribute must be of the same type (or domain). Each attribute name must be unique. The order of attributes is insignificant No two rows (tuples) in a relation can be identical. The order of the rows (tuples) is insignificant. Relational Algebra Operations:
Principal relation operations: –
select - pick rows from a relation by some condition;
–
project - pick columns by name;
–
join - connect two relations usually by a Foreign Key.
–
Division – divide any two queries.
Set theory operations: –
union - make the table containing all the rows of two relations;
–
intersection - pick the rows which are common to two relations;
–
difference - pick the rows which are in one table but not another;
–
Cartesian product - pair off each of the tuples in one relation with those in another - creating a double sized row for each pair.
–
Rename – to get the same relation with new name
Note: All of the operations return relations
Set theory operations: Referenced Tables -
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 2
Notes of Database Management System UNION -
RUS means, a union operation on R and S that will produce another table which will have all the rows of R and S except the duplicates There are 2 Sally in R and S, so, just take 1 from them. DIFFERENCE –
R-S means, a difference operation on R and S that will produce another table which will have all the rows which is in R but not in S. As Sally is in R and in S, so, she has been omitted. INTERSECTION –
R∩S means, an intersection operation on R and S that will produce another table which will have all the rows which are in both R and S. As Sally is in R and in S, so, she has been selected.
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 3
Notes of Database Management System More Union –
More Intersection –
More Difference –
1. Attributes of relations need not be identical to perform union, intersection and difference operations. 2. However, they must have the same number of attributes and the domains for corresponding attributes must be identical. 3. Union, Intersection and difference operators may only be applied to Union Compatible relations. 4. Union and Intersection are commutative operations RUS= SUR && R∩S = S∩R
5. Difference operation is NOT commutative. R - S not equal S - R Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 4
Notes of Database Management System Things to do--
Find out RUT , Find out R∩T
, Find out R-T
,
Find out T-R
Cartesian Product— 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, then A×B isA1-B1 A1-B2 A1-B3 A1-B4 , A2-B1 A2-B2 A2-B3 A2-B4 , A3-B1 A3-B2 A3-B3 A3-B4.
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 5
Notes of Database Management System RENAME – describe later
Principal relation operations: Unary operations: SELECTIONSelection and Projection are unary operators. The selection operator is sigma (σ) .The selection operation acts like a filter on a relation by returning only a certain number of rows. The resulting relation will have the same number of attributes as the original relation. The resulting relation may have fewer rows than the original relation. The rows to be returned are dependent on a condition that is part of the selection operator. σC (R) Returns only those rows in R that satisfy condition C . A condition C can be made up of any combination of comparison or logical operators that operate on the attributes of R. In general, the select operation is denoted by
σ<selection condition>(R) where the symbol σ(sigma) is used to denote the select operator, and the selection condition is a Boolean expression specified on the attributes of relation R. Properties: The SELECT operation σ<selection condition>(R) produces a relation S that has the same schema as R –The SELECT operation σ iscommutative; i.e.,
σ(σ< condition2> (R)) = σ (σ< condition1> ( R))
–A cascaded SELECT operation may be applied in any order; i.e., σ(σ< condition2> (σ(R)) = σ (σ< condition3> (σ< condition1> ( R)))
–A cascaded SELECT operation may be replaced by a single selection with a conjunction of all the conditions; i.e., σ(σ< condition2> (σ(R)) = σ AND < condition2> AND < condition3> ( R))) Example:
Table named EMP
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 6
Notes of Database Management System Select only those Employees in the CS department ::
σ Dept = ‘CS’ (EMP)
Select only those Employees with last name Smith who are assistant professors
σ Name = ‘Smith’ Λ Rank = ‘Assistant’ (EMP)
Select only those Employees who are either Assistant Professors or in the Economics department
σ Dept = ‘Econ’ V
Rank = ‘Assistant’
(EMP)
Select only those Employees who are not in the CS department or Adjuncts
σ ¬(Dept = ‘CS’
V Rank = ‘Adjunct’)
(EMP)
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 7
Notes of Database Management System
Things to do:
σ(Rank = 'Adjunct' σ Dept = 'CS' (
Λ Dept = 'CS')
(EMP)
Rank = 'Associate'
EMP )
σ Rank = 'Associate' ( σ Rank = 'Associate'
Dept = 'CS' EMP
Dept = 'CS'
)
(EMP)
σ Age > 26 (R U S) Projection: Projection is also a Unary operator. The Projection operator is pi (π) Projection limits the columns that will be returned from the original relation. The general syntax is: π attributes R Where attributes is the list of attributes to be displayed and R is the relation. The resulting table will have the same number of rows as the original relation (unless there are duplicate rows produced). The number of columns of the resulting relation may be equal to or less than that of the original relation.
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 8
Notes of Database Management System Project only the names and departments of the employees:
π name, dept (EMP)
Show the names of all employees working in the CS department:
π
name
(σ Dept = 'CS' (EMP) )
Show the name and rank of those Employees who are not in the CS department or Adjuncts
π name, rank ( σ ¬(Rank = 'Adjunct' V
Dept = 'CS')
(EMP) )
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 9
Notes of Database Management System Things to do:
π name, rank ( σ ¬(Rank = 'Adjunct' π fname, age (σ
Age > 22
Λ Dept = 'CS')
(EMP) )
(R U S) ) |||| σ office > 300 ( π name, rank (EMP))
Additional Relational Operations Aggregate Functions We can also apply Aggregate functions to columns and rows 1. SUM 2. MINIMUM 3. MAXIMUM 4. AVERAGE, MEAN, MEDIAN 5. COUNT Aggregate functions are sometimes written using the Projection operator or the Tau character (τ)
1. 2. 3. 4.
Find the minimum Salary: τ MIN (salary) (EMP) Find the average Salary: τ AVG (salary) (EMP) Count the number of employees in the CS department: τ COUNT (name) ( σ Dept = 'CS' (EMP) ) Find the total payroll for the Economics department: τ SUM (salary) (σ Dept = 'Econ' (EMP) )
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 10
Notes of Database Management System
JOIN OPERATION:
Join operations bring together two tables and combine their rows and columns in a specific fashion.
The generic join operator called the Theta Join is
It takes as arguments the columns from the two tables that are to be joined.
The join condition can be
When the join condition operator is = then we call this an Equijoin
Note that the columns in common are repeated. Example:
Find all information on every employee including their department info.
EMP
emp. Dept = depart. Dept
DEPART
Take a look, one table has 5 rows, the other has 4 rows, the resultant has 5 rows. Because deparment name “Hist” gives no information of about his employee.
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 11
Notes of Database Management System
Find all information on every employee including department info where the employee works in an office numbered less than the department main office
EMP
(emp. office < depart. mainoffice) Λ (emp. dept = depart. dept)
DEPART
NATURAL JOIN: Notice in the generic (Theta) join operation, any attributes in common (such as dept above) are repeated. The Natural Join operation removes these duplicate attributes. The natural join operator is: *
We can also assume using * that the join condition will be = on the two attributes in common. Emp * depart ( There are 2 Dept columns, one is omitted!)
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 12
Notes of Database Management System
OUTER JOIN: In the Join operations so far, only those tuples from both relations that satisfy the join condition are included in the output relation. The Outer join includes other rows as well according to a few rules. Three types of outer joins: 1. Left Outer Join
includes all rows in the left hand relation and includes only those matching rows
from the right hand relation. Denotion 2. Right Outer Join
includes all rows in the right hand relation and includes only those matching
rows from the left hand relation. Denotion 3. Full Outer Join includes all rows in the left hand relation and from the right hand relation. Left Outer Join:
PEOPLE
people . food = menu . food
MENU
Right Outer Join:
PEOPLE
people . food = menu . food
MENU
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 13
Notes of Database Management System Full Outer Join:
PEOPLE
people . food = menu . food
MENU
OUTER UNION:
In this case, just go from row of table 1 to row of table 2. Take common columns just once. if matching criteria don’t match, then put NULL, else include in the table Repeat the same but this time go from row of table 2 to row of table 1.
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 14
Notes of Database Management System
MORE EXAMPLES ( Korth i.e. Banking Schema)
Mrs Mousmi Ajay Chaurasia ,LECTURER, Dept. Of INFORMATION TECHNOLOGY
Page 15