Professional Documents
Culture Documents
Lecture - QueryLanguages
Lecture - QueryLanguages
Contents
• Introduction to Querying
– ANSI-SPARC architecture (again!)
– Data Access Methods
– Query Languages
• Relational Query Languages
– Relational Algebra
CET341 Database Query Languages
Conceptual DBMS
Level
Internal
Level
CET341 Database Query Languages
Query Languages
Relational Completeness
Relational Algebra
• Relational algebra defines a set of
operators that may work on relations.
• Relations are just sets of data. As such,
relational algebra deals with set theory.
• The operators in relational algebra are very
similar to traditional algebra except that
they apply to sets.
CET341 Database Query Languages
Relational Algebra
• Relational algebra provides several main
operators:
– Union
– Difference
– Intersection
– Product
– Projection
– Selection (Restriction)
– Join (many variations)
CET341 Database Query Languages
Union Operator
Union Example
– returns set of all rows from two tables. The tables have
to have the same characteristics e.g.
A B
F_NAME UNION F_NAME Yields F_NAME
Georgina Jane Georgina
Jane William Jane
Elaine Elaine
Peter Peter
William
CET341 Database Query Languages
Union Compatible
• Relations are union compatible if they:
1. Share the same number of attributes (columns)
2. Each attribute comes from the same domain
• Relations in the union operator must be union
compatible
– As must DIFFERENCE and INTERSECT
• Not enforced in SQL
– Also UNION and INTERSECT are associative
– i.e. A UNION B == B UNION A
– but DIFFERENCE is not associative
CET341 Database Query Languages
• Result of A UNION X
Id Name
1 Fred
2 Sally
3 Bill
Difference Operator
• The difference operator produces
a third relation that contains the A
tuples that appear in the first
relation, but not the second
• This is similar to a subtraction
• This is denoted A – B or B
A[f_name] - B[f_name]
A-B
CET341 Database Query Languages
Difference Example
– returns set of all rows in one table (A) that are not found
in the other (B)
A B
Difference Example 2
Intersection Operator
Intersection Example
• Returns set of all rows belonging to A and B relations, i.e.
rows that appear in BOTH tables
A B
F_NAME INTERSECT F_NAME Yields F_NAME
Georgina Jane Jane
Jane William Sue
Elaine Sue
Peter Dennis
Sue
CET341 Database Query Languages
Multi-column Examples
Multi-column Examples
Multi-column Examples
Product Operation
Product Example
A B A PRODUCT B
P_CODE STORE SHELF P_CODE STORE SHELF
AA 23 J AA 23 J
BB 24 K AA 24 K
25 L AA 25 L
BB 23 J
BB 24 K
BB 25 L
CET341 Database Query Languages
Project Operator
• A projection operation
produces a second relation
that is a subset of the first.
• The subset is in terms of
A
columns, not tuples
• The resulting relation will
contain a limited number of
columns. However, every
tuple will be listed. PROJECT x,y, z (A)
A[f_name, s_name] x,y,z (A)
CET341 Database Query Languages
Project Example
– returns selected attributes for all rows in relation A
A A[f_name,s_name]
23 J 25 L
24 K
25 L
CET341 Database Query Languages
Join Operator
Advanced DB 3 COM348 01
A
DB Systems 2 COM220 01
SAD 2 COM221 05
OOD 3 COM379 08
Advanced DB 3 COM348 01
A
DB Systems 2 COM220 01
SAD 2 COM221 05
OOD 3 COM379 08
Further Reading
• Date, chapter 6
• Connolly & Begg chapter 5 (5th, 6th ed.)
• For brief introductory overviews read:
Ritchie, C., Relational Database Principles, Continuum,
1998 (reprinted 2000), pp 33-37
Rolland, F.D., The Essence of Databases, Prentice
Hall, 1998, pp48-63