Professional Documents
Culture Documents
Chapter 6 RelationalQueryLanguage
Chapter 6 RelationalQueryLanguage
Chapter 6 RelationalQueryLanguage
Chapter 6
Relational Query Languages
https://sites.google.com/site/iotictcourses
2
Relational Algebra
• Relational algebra is the basic set of operations that enables a
user to specify basic retrieval requests.
• The algebra operations produce new relations, which can be
further manipulated using operations of the same algebra.
• Sequence of relational algebra operations forms a relational
algebra expression Represents a database query (retrieval
request).
• RA defines one or more new relations without changing the
original relation.
• The output from one operation can become the input to
another operation (nesting of RA operations)
3
• The basic type of Relational Algebra operations:
– Selection ( ) Selects a subset of rows from a relation.
– Projection ( ) Deletes unwanted columns from a relation.
– Renaming: assigning intermediate relation for a single
operation
– Cross-Product ( x ) Allows to concatenate(join or link) a
tuple from one relation with all the tuples from the other
relation.
– Set-Difference (-) Tuples in relation R1, but not in relation R2.
– Union ( ) Tuples in relation R1, or in relation R2.
– Intersection () Tuples in relation R1 and in relation R2
– Join Tuples joined from two relations based on a condition
• Join and intersection are derivable from the rest.
• Using these operations, we can build up sophisticated
database queries.
4
1. Selection operation:
– Selects subset of tuples/rows in a relation that satisfy
the selection condition.
– Schema of result identical to schema of (only) input
relation.
– Result relation can be the input for another relational
algebra operation! (Operator composition.) 5
• Notation: <Selection Condition> <Relation Name>
• Example: Find all Employees with skill type of Database.
< SkillType =”Database”> (Employee)
6
2. Projection
– Selects certain attributes while discarding the
other from the base relation.
– The PROJECT creates a vertical partitioning.
– Deletes attributes that are not in projection list.
7
8
3. Rename Operation
• Applies several relational algebra operations one
after the other.
• Two ways of expression:
1. A single algebraic expression:
9
4. Set Operations
10
I. Union Operation
11
5. Cartesian Product operation
– Combines tuples from two relations in a combinatorial
fashion.
– If R1 has n tuples & R2 has m tuples, then the resulting
relation will have n* m tuples.
12
• To extract employee information about Managers of
each department from the above result relation the
algebra query and the resulting relation will be:
13
6. Join Operation
• Select related tuples from two relations.
• In the JOIN operation:
– Cartesian & Selection Operations are used together.
14
Relational Calculus
• Relational calculus expressions creates a new relation
over:
– rows of the stored db relations (in tuple calculus) or
– columns of the stored relations (in domain calculus).
• In a RC, Unlike RA, there is no ordering of operations to
specify how to retrieve the query result.
• Relational calculus is nonprocedural or declarative
language. While Relational Algebra procedural or
sequential language.
15
• There are two types of Relational Calculus operations:
1. Tuple Relational Calculus
2. Domain Relational Calculus
16
• Example: To find the first and last names of all employees
whose skill level is greater than 5, we can write the following
tuple calculus expression:
17
• Example2: Find all employees whose skill level is >= 8.
{E | Employee(E) E.SkillLevel >= 8}
18
Quantifiers in Relational Calculus
1. Existential quantifier (‘there exists’)
– A rule should be true for at least one instance.
– Example: An employee with skill level >= 8 will be:
{E | Employee(E) (E)(E.SkillLevel >= 8)}
– There exist at least one tuple of the relation employee where the value for the
SkillLevel >= 8.
2. Universal quantifier (‘for all’)
– Implies every instance of a relation in the database:
– An employee with skill level >= 8 will be:
{E | Employee(E) (E)(E.SkillLevel >= 8)}
– For all tuples in relation employee, the values for the SkillLevel are greater than or equal
to 8.
19
Domain Relational Calculus
• Domain calculus differs from tuple calculus in the type of
variables used in formulas:
– variables range over tuples in tuple calculus
– variables range over single values from domains of
attributes in Domain calculus
• Example:
– Query1: list Employee Name
{Fname, Lname| (Employee (EID,FName, LName)}
20
• Query2: Find the list of Employees who work in the
department of IS
– Domain relational Calculus expression for the query: {EID, Fname,
Lname|( DName, EDID, DID) (Employee(EID, FName, LName)
Department(DID, DName, DMangID) DID=EDID DName=’IS’)},
Where DName, EDID, DID DName, EDID, DID
• Query3:List the names of employees that do not manage
any department.
– {Fname, Lname|(EID)(Employee(EID, Fname,Lname)
(~(DMangId)(Dept(DID, Dname, DMangId) (EID=DMangId))))}
21