Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

CET341 Database Query Languages

Database Query Languages


CET341 Database Query Languages

Contents

• Introduction to Querying
– ANSI-SPARC architecture (again!)
– Data Access Methods
– Query Languages
• Relational Query Languages
– Relational Algebra
CET341 Database Query Languages

Introduction to Query Languages


Host language Host language Host language
+Data sublanguage +Data sublanguage +Data sublanguage

External External External


View 1 View 2 View n

Conceptual DBMS
Level

Internal
Level
CET341 Database Query Languages

Data Access Methods

• Query language is the Data Sublanguage part of


the ANSI SPARC architecture
• As well as SQL, there are many other ways of
querying a database
– SQL can be embedded in a host language, e.g. JDBC,
C#, PHP, ODBC, …
– Query By Example (as used in Access)
– Query By Forms (e.g. Oracle Forms, Oracle APEX)
CET341 Database Query Languages

Query Languages

• SQL is by far the most well know query language,


but there are others…
– Relational Algebra – a standard by which other
relational query languages are compared
– Relational Calculus – an equivalent to the algebra
– QUEL – a structured form of the relational calculus
– SQL – a ‘mix’ of algebra/calculus which has become the
‘de-facto’ standard for relational and the standard for
object-relational databases
– OQL – a standard for object-oriented databases
CET341 Database Query Languages

Relational Completeness

• A standard by which relational query languages


are measured
– A language is said to be relationally complete if it can
implement (at least) all operations of the relational
algebra
• Algebra (obviously is relationally complete)
• Calculus, SQL and QUEL are all also relationally complete
– Not the same as computational completeness i.e. a
language that can express any computable function, e.g.
Java, C#, etc.
CET341 Database Query Languages

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

• The union operator adds


tuples from one relation to
another relation to form a A
third relation
• A union operation will
result in a combined
relation B
• The Union of relations A
and B is denoted as A  B A UNION B
or A[p_code]  B[p_code] A B
CET341 Database Query Languages

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

Union Compatible Examples

• Decide which of these • A Id Name

relations are union 1 Fred


3 Bill
compatible with
relation X Id Name Age
• B
• X Id Name
3 Bill 25
5 Betty 18
1 Fred
2 Sally • C Id Name
AB03 Bill
EF05 Betty
CET341 Database Query Languages

Union Compatible Example

• Result of A UNION X

Id Name
1 Fred
2 Sally
3 Bill

– Remember, order of tuples is unimportant


CET341 Database Query Languages

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

F_NAME DIFFERENCE F_NAME Yields F_NAME


Georgina Jane Georgina
Jane Sue Elaine
Elaine Sarah Peter
Peter
Sue
CET341 Database Query Languages

Difference Example 2

• The order is important for the difference operator:


B A
F_NAME DIFFERENCE F_NAME Yields F_NAME
Jane Georgina Sarah
Sue Jane
Sarah Elaine
Peter
Sue
CET341 Database Query Languages

Intersection Operator

• An intersection operation will


produce a third relation that A
contains the tuples (rows) that
are common to the relations
involved.
• This is similar to the logical B
operator ‘AND’
• Example
(A  B) A INTERSECT B
CET341 Database Query Languages

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

• Rules are the same when tables have multiple


attributes
• E.g. A UNION B =
Id Name Id Name Id Name
1 Fred 1 Fred 1 Fred
1 Barney 2 Joan 1 Barney
2 Sally 3 Bill 2 Sally
2 Joan
3 Bill
CET341 Database Query Languages

Multi-column Examples

• Rules are the same when tables have multiple


attributes
• E.g. A DIFFERENCE B =
Id Name Id Name Id Name
1 Fred 1 Fred 1 Barney
1 Barney 2 Joan 2 Sally
2 Sally 3 Bill
CET341 Database Query Languages

Multi-column Examples

• Rules are the same when tables have multiple


attributes
• E.g. A INTERSECT B =
Id Name Id Name Id Name
1 Fred 1 Fred 1 Fred
1 Barney 2 Joan
2 Sally 3 Bill
CET341 Database Query Languages

Product Operation

• A product operator is a concatenation of


every tuple in one relation with every tuple
in a second relation (Cartesian Product)
• The resulting relation will have x times y
tuples, where…
x = the number of tuples in the first relation
and
y = the number of tuples in the second relation
• This is similar to multiplication A  B
CET341 Database Query Languages

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]

F_NAME S_NAME PHONE_NO F_NAME S_NAME


Georgina Brown 019167854 Georgina Brown
Jane Jones 010368797 Jane Jones
Elaine Smith 098459856 Elaine Smith
Peter Newman 089759897 Peter Newman
Sue Walker 056784567 Sue Walker
Jane Jones 018763214
Notice we have one less
tuple in the result, why?
CET341 Database Query Languages

Select (Restrict) Operator


• The selection operator (sometimes called
restrict) is similar to the projection operator.
It produces a second relation that is a subset
of the first. Uses a condition
• However, the selection operator produces a
subset of tuples (rows) , not attributes A
(columns).
• The resulting relation contains all columns,
but only contains a portion of the tuples.
A WHERE store > 24
store > 24 (A)
CET341 Database Query Languages

Select (Restrict) Example


• Return rows from a relation where qualifier is true.

A A where store > 24


STORE SHELF STORE SHELF

23 J 25 L
24 K
25 L
CET341 Database Query Languages

Join Operator

• The join operator is a combination of the


product, selection, and projection operators.
• There are several variations of the join
operator…
– Natural join
– Theta join
– Equi-join
CET341 Database Query Languages

Join Operator (Cont.)


A join works along these lines:
(a) Form the product of A and B
(b) Perform a selection to eliminate some tuples
Criteria for the selection are specified as part of
the join
(c) Then remove some attributes through projection
(optional)
A JOIN (StaffNo = StaffId) B
A ⋈StaffNo = StaffId B or A*B (if attribute names match)
CET341 Database Query Languages

Join Example (Data version 1)


Mod Name Level Crscode StaffNo

Advanced DB 3 COM348 01
A
DB Systems 2 COM220 01
SAD 2 COM221 05
OOD 3 COM379 08

StaffNo Staff Position


Name
01 Stirk S PL
B 05 Lejk M R
08 Erwin H SL
CET341 Database Query Languages

Join Example (Natural Join)

Mod Name Level Crs Staff Staff Position


code No Name
Advanced 3 COM348 01 Stirk S SL
DB
DB 2 COM220 01 Stirk S. SL
Systems
SAD 2 COM221 05 Lejk M R

OOD 3 COM379 08 Erwin H SL


CET341 Database Query Languages

Join Example (Data version 2)


Mod Name Level Crscode StaffNo

Advanced DB 3 COM348 01
A
DB Systems 2 COM220 01
SAD 2 COM221 05
OOD 3 COM379 08

StaffId Staff Position


Name
01 Stirk S PL
B 05 Lejk M R
08 Erwin H SL
Notice that our attribute in relation
B is now called StaffId
CET341 Database Query Languages Notice that we now have two attributes in the result

Join Example (Equi-join)

Mod Name Level Crs Staff Staff Staff Position


code No Id Name
Adv. DB 3 COM348 01 01 Stirk S PL

DB Systems 2 COM220 01 01 Stirk S PL

SAD 2 COM221 05 05 Lejk M R

OOD 3 COM379 08 08 Erwin H SL


CET341 Database Query Languages

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

You might also like