Sie sind auf Seite 1von 79

Relational Query

Languages

Professor Navneet Goyal


Department of Computer Science & Information Systems
BITS, Pilani
Relational Query
Languages
n A major strength of the relational
model: supports simple, powerful
querying of data.
n Queries can be written intuitively,
and the DBMS is responsible for
efficient evaluation.
n The key: precise semantics for
relational queries.
n Allows the optimizer to extensively re-
order operations, and still ensure that
the answer does not change.
© Prof. Navneet Goyal, BITS, Pilani
Relational Query
Languages
n Query languages: Allow manipulation
and retrieval of data from a database.
n Relational model supports simple,
powerful QLs:
n Strong formal foundation based on logic.
n Allows for much optimization.
n Query Languages != programming
languages!
n QLs not expected to be “Turing complete”.
n QLs not intended to be used for complex
calculations.
n QLs support easy, efficient access to large data
sets.
© Prof. Navneet Goyal, BITS, Pilani
Formal Relational
Query Languages
n Two mathematical Query
Languages form the basis for “real”
languages (e.g. SQL), and for
implementation:
n Relational Algebra: More operational,
very useful for representing execution
plans.
n Relational Calculus: Lets users
describe what they want, rather than
how to compute it. (Non-operational,
declarative.)
© Prof. Navneet Goyal, BITS, Pilani
The SQL Query
Language
n Developed by IBM’s System-R Project
in the 1970s
n Need for a standard since it is used by
many vendors
n Standards:
n SQL-86
n SQL-89 (minor revision)
n SQL-92 (major revision)
n SQL-99 (major extensions, current
standard)
n Find Out what is SQL-MM!!

© Prof. Navneet Goyal, BITS, Pilani


The SQL Query
Language
n SQL has been influenced by both
Relational Algebra (RA) & Relational
Calculus (RC)
n More so by RC, particularly Tuple
relational Calculus (TRC)
n The other variant of RC is Domain
Relational Calculus (DRC) which has
greatly influenced Query By Example
(QBE)

© Prof. Navneet Goyal, BITS, Pilani


The SQL Query
Language
n SQL consists of:
n DDL (Data Definition Language)
• Create conceptual schema
n DML (Data Manipulation Language)
• Relational operators
• Insert, Delete, Update
n VDL (View Definition Language)
• Specify user views & their mapping to the
conceptual schema (in most DBMSs, done by
DDL)
n SDL (Storage Definition Language)
• File organization
• Indexes
© Prof. Navneet Goyal, BITS, Pilani
The SQL Query
Language
n SDL is being removed from SQL
n DMLs
n High-level or nonprocedural (declarative)
• Can be entered at the SQL > or can be embedded in a
general purpose programming language
• Can specify & retrieve many records in a single DML
statement & are hence called Set-at-a-time DMLs
n Low-level or procedural
• Must be embedded in a general purpose
programming language
• Typically retrieves individual records or objects from
the DB & processes each separately
• Therefore it needs PL constructs like looping
• Record-at-a-time DMLs

© Prof. Navneet Goyal, BITS, Pilani


The SQL Query
Language
DMLs
n Whenever DML statements are embedded in a PL,
that language is called as the host language and
the DML is called the Data Sublanguage
n In object DBs, the host language & data
sublanguage form one integrated language – for
eg. C++ with some extensions to support
database functionality
n Some RDBMSs also provide integrated languages –
for eg. ORACLE’s PL/SQL.

© Prof. Navneet Goyal, BITS, Pilani


Operations on
Relations
n Restrict
n Project
n Join
n Divide Relational Operations
n Union Set Operations
n Intersection
n Difference
n Product

© Prof. Navneet Goyal, BITS, Pilani


Relational Algebra
n Collection of operations on relations
n 8 operators + Rename operator
n Codd’s 8 operators do not form a minimal
set
n Some of them are not primitive
n Join, Intersect, & Divide can be defined in
terms of other 5
n None of these 5 can be defined in terms of
the remaining 4
n MINIMAL SET
n Join, Intersect, & Divide are important &
should be supported directly

© Prof. Navneet Goyal, BITS, Pilani


Relational Algebra
n Some common DB requests can
not be performed with the RA
operations
n Additional operations are
developed
n Aggregate functions & additional
types of joins

© Prof. Navneet Goyal, BITS, Pilani


Relational Algebra
IMPORTANCE OF RA
n Provides a formal foundation for
relational model operations
n Plays a pivotal role in query
optimization in RDBMSs
n Some of its concepts are
implemented in SQL
n Expressive power of RA is used as a
metric of how powerful a relational
DB query language is!!

© Prof. Navneet Goyal, BITS, Pilani


Relational Completeness
n A relational DB query language is
said to be Relationally Complete, if
it can express all queries that we can
express in RA
n Relational DB query languages are
expected to be relationally complete
n Commercial query languages like
SQL, support features that allow us
to express some queries that are not
possible in RA

© Prof. Navneet Goyal, BITS, Pilani


Restrict & Project

© Prof. Navneet Goyal, BITS, Pilani


Union, Intersection &
Difference

© Prof. Navneet Goyal, BITS, Pilani


Union, Intersection &
Difference
Union Compatibility: r U s is valid if:
n Relations r & s have the same arity
n Domains of the ith attribute of r is the
same as the domain of the ith attribute of
s, ⍱ i.
Note that r & s can be either database
relations or derived relations

© Prof. Navneet Goyal, BITS, Pilani


Product & Divide
A X A X A X X
DIVIDE BY
B Y A Y A Y Z
C B X A Z
B Y B X
A
PRODUCT C X C Y
C Y

© Prof. Navneet Goyal, BITS, Pilani


Divide
DIVIDE

S1
S1 P1
P2 S2
S1 P2
S3
S2 P3
P2 S4
S2 P4
P4
S2 P1 S1

S2 P2 S4

S3 P2 P1
S2
S4 P2 P2
S4 P4 P4

Shipment Parts
© Prof. Navneet Goyal, BITS, Pilani
Join
A1 B1 B1 C1
A2 B1 B2 C2
A3 B2 B3 C3
A1 B1 C1
A2 B1 C1
NATURAL
JOIN A3 B2 C2

© Prof. Navneet Goyal, BITS, Pilani


Closure
n A relation is closed under
relational and set operators
n The result of these operators on
relation(s) is another relation
n Output from one operation can
become input to another
n Nested Expressions possible

© Prof. Navneet Goyal, BITS, Pilani


Example Schema
R1 S1 sid sname rating age
sid bid day 22 dustin 7 45.0
22 101 10/10/96 31 lubber 8 55.5
58 103 11/12/96 58 rusty 10 35.0
S2 sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
© Prof. Navneet Goyal, BITS, Pilani
sname rating
Projection yuppy 9
lubber 8
guppy 5
n Deletes attributes that are not in
rusty 10
projection list.
n Schema of result contains exactly π sname,rating(S2)
the fields in the projection list,
with the same names that they
had in the (only) input relation.
n Projection operator has to
eliminate duplicates! (Why??) age
n Note: real systems typically
35.0
don’t do duplicate elimination
unless the user explicitly asks
55.5
for it. (Why not?) π age(S2)
© Prof. Navneet Goyal, BITS, Pilani
sid sname rating age
Selection 28 yuppy 9 35.0
58 rusty 10 35.0
n Selects rows that
satisfy selection
condition.
σ rating >8(S2)
n No duplicates in
result! (Why?)
n Schema of result
identical to schema of sname rating
(only) input relation.
n Result relation can be yuppy 9
the input for another rusty 10
relational algebra
operation! (Operator
composition.) π sname,rating(σ rating >8(S2))

© Prof. Navneet Goyal, BITS, Pilani


Union, Intersection,
Set-Differencesid sname rating age
n All of these operations take 22 dustin 7 45.0
two input relations, which 31 lubber 8 55.5
must be union-compatible:
58 rusty 10 35.0
n Same number of fields
n `Corresponding’ fields
44 guppy 5 35.0
have the same 28 yuppy 9 35.0
domain/data type
S1∪ S2

sid sname rating age


sid sname rating age 31 lubber 8 55.5
22 dustin 7 45.0 58 rusty 10 35.0
S1− S2 S1∩ S2
© Prof. Navneet Goyal, BITS, Pilani
Cross-Product
n Each row of S1 is paired with each row of R1.
n Result schema has one field per field of S1 and R1, with
field names `inherited’ if possible.
n Conflict: Both S1 and R1 have a field called sid.
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 101 10/10/96
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 22 101 10/10/96
31 lubber 8 55.5 58 103 11/12/96
58 rusty 10 35.0 22 101 10/10/96
58 rusty 10 35.0 58 103 11/12/96

§ Renaming operator: ρ (C(1→ sid1, 5 → sid 2), S1× R1)


© Prof. Navneet Goyal, BITS, Pilani
Joins
n Condition Join: R !" c S = σ c ( R × S)
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 58 103 11/12/96
31 lubber 8 55.5 58 103 11/12/96
S1 !" R1
S1. sid < R1. sid
n Result schema same as that of cross-
product.
n Fewer tuples than cross-product, might
be able to compute more efficiently
n Sometimes called a theta-join.
© Prof. Navneet Goyal, BITS, Pilani
Joins
n Equi-Join: A special case of condition join
where the condition c contains only equalities
sid sname rating age bid day
22 dustin 7 45.0 101 10/10/96
58 rusty 10 35.0 103 11/12/96
S1 !" R1
sid
n Result schema similar to cross-product, but
only one copy of fields for which equality is
specified.
n Natural Join: Equijoin on all common fields.
© Prof. Navneet Goyal, BITS, Pilani
Examples of Division A/B
sno pno pno pno pno
s1 p1 p2 p2 p1
s1 p2 p4 p2
s1 p3
B1
p4
B2
s1 p4
s2 p1 sno B3
s2 p2 s1
s3 p2 s2 sno
s4 p2 s3 s1 sno
s4 p4 s4 s4 s1

A A/B1 A/B2 A/B3


© Prof. Navneet Goyal, BITS, Pilani
Find names of sailors who’ve
reserved boat #103
n Solution 1: π sname((σ Reserves) !" Sailors)
bid =103

n Solution 2: ρ (Temp1,σ Reserves)


bid =103
Query
Optimizer
ρ (Temp2,Temp1"! Sailors)
π sname (Temp2)

n Solution 3: π sname(σ (Reserves"! Sailors))


bid =103
© Prof. Navneet Goyal, BITS, Pilani
Find names of sailors who’ve
reserved a red boat
n Information about boat color only available
in Boats; so need an extra join:

π sname ((σ Boats) !" Re serves !" Sailors)


color =' red '
Query
Optimizer
v A more efficient solution:
π sname(π ((π σ Boats)"! Re s)"! Sailors)
sid bid color ='red '
A query optimizer can find this, given the first solution!

© Prof. Navneet Goyal, BITS, Pilani


Find sailors who’ve reserved a
red or a green boat
n Can identify all red or green boats, then find
sailors who’ve reserved one of these boats:

ρ (Tempboats, (σ Boats))
color =' red ' ∨ color =' green '
π sname(Tempboats !" Re serves !" Sailors)

v Can also define Tempboats using union! (How?)


v What happens if ∨ is replaced by ∧ in this query?

© Prof. Navneet Goyal, BITS, Pilani


Find sailors who’ve reserved a
red and a green boat
n Previous approach won’t work! Must
identify sailors who’ve reserved red boats,
sailors who’ve reserved green boats, then
find the intersection

ρ (Tempred, π ((σ Boats) !" Re serves))


sid color =' red '

ρ (Tempgreen,π ((σ Boats)"! Reserves))


sid color =' green'
π sname((Tempred ∩ Tempgreen) !" Sailors)

© Prof. Navneet Goyal, BITS, Pilani


Find the names of sailors
who’ve reserved all boats
n Uses division; schemas of the input
relations to / must be carefully chosen:

ρ (Tempsids,(π Reserves)/(π Boats))


sid ,bid bid
π sname (Tempsids !" Sailors)

v To find sailors who’ve reserved all ‘Interlake’ boats:

..... /π (σ Boats)
bid bname =' Interlake'
© Prof. Navneet Goyal, BITS, Pilani
Summary
n The relational model has rigorously
defined query languages that are
simple and powerful.
n Relational algebra is more
operational; useful as internal
representation for query evaluation
plans.
n Several ways of expressing a given
query; a query optimizer should
choose the most efficient version.

© Prof. Navneet Goyal, BITS, Pilani


Summary
n Role of RA in query optimization

© Prof. Navneet Goyal, BITS, Pilani


Role of RA in QO
n Parsing and translation
n Translate the query into its internal form.
n Translation is similar to the work performed
by the parser of a compiler
n Parser checks syntax, verifies relations
n Parse tree representation
n This is then translated into RA expression

© Prof. Navneet Goyal, BITS, Pilani


Role of RA in QO
n Query Execution Plan
n In SQL, a query can be expressed is several ways
n Each SQL query can itself be translated into RA expression
in many ways
n An RA expression only partially tells you how to evaluate a
query
n Several ways to evaluate RA expression
n Annotate RA expression with instructions specifying how to
evaluate each operation
n Annotation may state the algorithm to be used for a specific
operation or the particular index to use
n Annotated RAE is called an evaluation primitive
n Sequence of primitive operations is a QEP
© Prof. Navneet Goyal, BITS, Pilani
Role of RA in QO
n Example
select balance
from account
where balance < 2500
n RAEs
n σbalance<2500(∏balance(account))
n ∏balance(σbalance<2500(account))
n E.g., we can use an index on balance to find
accounts with balance < 2500,
n or can perform complete relation scan and
discard accounts with balance ≥ 2500

© Prof. Navneet Goyal, BITS, Pilani


Role of RA in QO

Query Execution Plan


© Prof. Navneet Goyal, BITS, Pilani
Role of RA in QO

© Prof. Navneet Goyal, BITS, Pilani


Role of RA in QO

© Prof. Navneet Goyal, BITS, Pilani


Summary
n What we studied about RA is not used in
commercial systems!!!
n We assumed a SET model
n Commercial systems use a BAG model
n What are BAGS?
n Why BAGS?

© Prof. Navneet Goyal, BITS, Pilani


Relational Operations on
BAGS
n So far we discussed relational operations
on relations (sets of tuples)
n In commercial systems, a relation is
allowed to have duplicate tuples
n If a set is allowed to have multiple
occurrences of a member, it is called a
BAG or MULTISET
n Set – A relation without duplicate tuples
n Bag – A relation that may or may not have
duplicate tuples

© Prof. Navneet Goyal, BITS, Pilani


WHY BAGS?

n Allowing relations to be bags


can speed up operations on
relations
n For Example, allowing the
result to be a bag could speed
up union of two relations
n Duplicate elimination not
required
© Prof. Navneet Goyal, BITS, Pilani
WHY BAGS? A B C
1 2 5
n Another Example: 3 4 6
Projection of relations
n Each projected tuple 1 2 7
must be compared with
other projected tuples
1 2 8
to make sure that each
projection tuple A B
appears only once
A B
n If bags are accepted as 1 2
results, we simply need 1 2
to project each tuple & 3 4
add it to the result 3 4
1 2
1 2
© Prof. Navneet Goyal, BITS, Pilani
WHY BAGS? A B C
1 2 5
3 4 6
If we are 1 2 7
projecting a 1 2 8
relation to take
an aggregate A B A B
(say AVG), we 1 2 1 2
could not use 3 4 3 4
the set model
AVG=2 1 2
1 2
AVG=1.5
© Prof. Navneet Goyal, BITS, Pilani
Union, Intersection, &
Difference of BAGS
UNION
n Add the no. of occurrences of each
tuple
n If R is a bag in which tuple t appears
n times, & S is a bag in which tuple t
appears m times, then in the bag
R∪S, t appears n+m times
n Either n or m or both could be zero

© Prof. Navneet Goyal, BITS, Pilani


Union, Intersection, &
Difference of BAGS
INTERSECTION
n If R is a bag in which tuple t
appears n times, & S is a bag in
which tuple t appears m times,
then in the bag R∩S, t appears
min(n,m) times

© Prof. Navneet Goyal, BITS, Pilani


Union, Intersection, &
Difference of BAGS
DIFFERENCE
n If R is a bag in which tuple t appears n
times, & S is a bag in which tuple t appears
m times, then in the bag R-S, tuple t
appears max(0, n-m) times
n If t appears in R more times than it
appears in S, then in R-S, t appears the no.
of times it appears in R, minus the no. of
times it appears in S
n If n=m, then t does not appear in R-S at all
n Occurrences of t in S each cancel one
occurrence in R

© Prof. Navneet Goyal, BITS, Pilani


Union, Intersection, &
Difference of BAGS
R A B S A B
1 2 1 2
3 4 3 4
1 2 3 4
1 2 5 6
Write down R∪S, R∩S, R-S, & S-R

© Prof. Navneet Goyal, BITS, Pilani


Union, Intersection, &
Difference of BAGS
R∪S A B R∩S A B R-S A B S-R A B
1 2 1 2 1 2 3 4
3 4 3 4 1 2 5 6
1 2
1 2
1 2
3 4
3 4
5 6

© Prof. Navneet Goyal, BITS, Pilani


Projection of BAGS

R A B C
1 2 5
3 4 6 πA, B (R)
1 2 7
A B
1 2 8
1 2
Write down πA, B (R) 3 4
1 2
1 2
© Prof. Navneet Goyal, BITS, Pilani
Selection on BAGS

R A B C
1 2 5
3 4 6 σC>=6 (R)
1 2 7
A B C
1 2 7
3 4 6
Write down σC>=6 (R) 1 2 7
1 2 7

© Prof. Navneet Goyal, BITS, Pilani


Product of BAGS

R A B S B C
2 3
1 2
4 5
1 2 4 5
A R.B S.B C
1 2 2 3
Write down RxS 1 2 2 3
1 2 4 5
1 2 4 5
1 2 4 5
© Prof. Navneet Goyal, BITS, Pilani
Natural Join of BAGS

R A B S B C
1 2 2 3
1 2 4 5
4 5

A B C
Write down R "! S 1 2 3
1 2 3

© Prof. Navneet Goyal, BITS, Pilani


Theta-Join of BAGS

R A B S B C
1 2 2 3
1 2 4 5
4 5

A R.B S.B C
1 2 4 5
Write down R "! S
1 2 4 5
R.B < S.B
1 2 4 5
1 2 4 5
© Prof. Navneet Goyal, BITS, Pilani
Extended Operators of
RA
n Duplicate-Elimination Operator δ
n Aggregation Operators
• Sum
• Count
• Average
• Max
• Min
n Grouping Operator γ
n Sorting Operator τ
n Extended Projection
n Outerjoin Operator

© Prof. Navneet Goyal, BITS, Pilani


Duplicate-Elimination
Operator δ
n Converts a bag to a set
δ(R) A B
R A B
1 2 1 2
3 4 3 4
1 2
1 2

© Prof. Navneet Goyal, BITS, Pilani


Aggregate Functions &
Operations
n Aggregation function takes a collection of values and
returns a single value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
n Aggregate operation in relational algebra: General Form

E is any relational-algebra expression


n G1, G2 …, Gn is a list of attributes on which to group
(can be empty)
n Each Fi is an aggregate function
n Each Ai is an attribute name
© Prof. Navneet Goyal, BITS, Pilani
Aggregate Operation –
Example
Relation r: A B C

α α 7
α β 7
β β 3
β β 10

g sum(c) (r) sum(C )

27

© Prof. Navneet Goyal, BITS, Pilani


Aggregate Operation –
Example
Relation account grouped by branch-name:
branch_name account_number balance
Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

branch_name g sum(balance) (account)


branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700

© Prof. Navneet Goyal, BITS, Pilani


Aggregate Functions
(Cont.)
n Result of aggregation does not
have a name
n Can use rename operation to give it a
name
n For convenience, we permit renaming
as part of aggregate operation

branch_name g sum(balance) as sum_balance (account)

© Prof. Navneet Goyal, BITS, Pilani


Generalized Projection
n Extends the projection operation by allowing
arithmetic functions to be used in the projection
list.

n E is any relational-algebra expression


n Each of F1, F2, …, Fn are are arithmetic
expressions involving constants and attributes in
the schema of E.
n Given relation credit_info(customer_name, limit,
credit_balance), find how much more each person
can spend:
∏customer_name, limit – credit_balance (credit_info)
© Prof. Navneet Goyal, BITS, Pilani
Generalized Projection
Cust_name limit Credit_bal
Curry 2000 1750
Hayes 1500 1500
Jones 6000 700
Smith 2000 400

Given relation credit_info(customer_name, limit,


credit_balance), find how much more each person can spend:
∏customer_name, (limit – credit_balance) as credit_available (credit_info)

Cust_name Credit_available
Curry 250
Hayes 0
Jones 5300
Smith 1600

© Prof. Navneet Goyal, BITS, Pilani


Outer Join
n An extension of the join operation that
avoids loss of information.
n Computes the join and then adds tuples
form one relation that does not match
tuples in the other relation to the result of
the join.
n Uses null values:
n null signifies that the value is unknown or does
not exist
n All comparisons involving null are (roughly
speaking) false by definition.
• We shall study precise meaning of comparisons
with nulls later
© Prof. Navneet Goyal, BITS, Pilani
Outer Join – Example
Relation loan
loan_number branch_name amount
L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155

© Prof. Navneet Goyal, BITS, Pilani


Outer Join – Example
Inner Join

loan Borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith

Left Outer Join


loan Borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
© Prof. Navneet Goyal, BITS, Pilani
Outer Join – Example
■ Right Outer Join
loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 null null Hayes
■ Full Outer Join
loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
L-155 null null Hayes

© Prof. Navneet Goyal, BITS, Pilani


Outer Join
n Left join, or inner join, select rows
common to the participating tables to
a join
n Selecting elements in a table
regardless of whether they are
present in the second table
n OUTER JOIN is the solution
n In Oracle, we will place an "(+)" in
the WHERE clause on the other side
of the table for which we want to
include all the rows.
© Prof. Navneet Goyal, BITS, Pilani
Outer Join
Store_Information Geography
store_name Sales Date
region_name store_name
East Boston
Los Angeles $1500 Jan-05-1999 East New York
West Los Angeles
San Diego $250 Jan-07-1999 West San Diego

Los Angeles $300 Jan-08-1999

Boston $700 Jan-08-1999


-­ We  want  to  find  out  the  sales  amount  for  all  of  the  stores
-­ If  we  do  a  regular  join,  we  will  not  be  able  to  get  what  we  want  because  we  
will          have  missed  "New  York,"  since  it  does  not  appear  in  the  
Store_Information table   store_name    SALES
SELECT  A1.store_name,  SUM(A2.Sales)  SALES   Boston                      $700
FROM  Geography  A1,  Store_Information  A2   New  York                              
WHERE  A1.store_name  =  A2.store_name  (+)   Los  Angeles    $1800
GROUP  BY  A1.store_name © Prof. Navneet Goyal, BITS, Pilani San  Diego            $250
NVL Function
n In Oracle/PLSQL, the NVL function lets you substitutes a value when a null
value is encountered.
n NVL (string1, replace_with )
n string1 is the string to test for a null value. Replace_with is the value
returned if string1 is null.
n Example #1:
n select NVL (supplier_city, 'n/a')
from suppliers;
n The SQL statement above would return 'n/a' if the supplier_city field
contained a null value. Otherwise, it would return the supplier_city value.
n Example #2:
n select supplier_id,
NVL (supplier_desc, supplier_name)
from suppliers;
n This SQL statement would return the supplier_name field if the
supplier_desc contained a null value. Otherwise, it would return the
supplier_desc.
n Example #3:
n select NVL (commission, 0)
from sales;
n This SQL statement would return 0 if the commission field contained
a null value. Otherwise, it would
© Prof.return the commission
Navneet Goyal, BITS, Pilani field.
Null Values
n It is possible for tuples to have a null value,
denoted by null, for some of their attributes
n null signifies an unknown value or that a value
does not exist or something that is NA
n The result of any arithmetic expression involving
null is null.
n Aggregate functions simply ignore null values
(as in SQL)
n For duplicate elimination and grouping, null is
treated like any other value, and two nulls are
assumed to be the same (as in SQL)

© Prof. Navneet Goyal, BITS, Pilani


Null Values
n Comparisons with null values return the special truth
value: unknown
n If false was used instead of unknown, then not (A < 5)
would not be equivalent to A >= 5
n Three-valued logic using the truth value unknown:
n OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
n AND: (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
n NOT: (not unknown) = unknown
n In SQL “P is unknown” evaluates to true if predicate P
evaluates to unknown
n Result of select predicate is treated as false if it
evaluates to unknown
© Prof. Navneet Goyal, BITS, Pilani
Problem Solving
n Consider the relational schemas:
Email (IDNO, email_id)
Student (IDNO, Name, Hostel, Room)
The EMAIL relation is maintained by IPC, where as the
Student relation is maintained by SWD. It is required
that the information in the two relations be combined
into a new relation with schema:
Student_email (IDNO, email_id, Name, Hostel, Room)
It is observed that there are some IDNOs in the
Student relation that are not in the Email relation.
Write a Relational Algebra expression to create the new
relation such that no IDNO from the Student relation is
left out.

ρ (Student_email, (student left outer join email))

© Prof. Navneet Goyal, BITS, Pilani


Problem Solving
n The student_master table has the schema:
Student_Master_2004 (SUID, IDNO, NAME)
where SUID is a unique identifier for a student
(for eg. f2004123). After completing one year,
students are either transferred to another
discipline/degree or offered a dual degree. The
following table contains the changed IDNO of
students:
Student_Master_NEW_2004 (SUID, IDNO, NAME)
You need to find out all cases of transfer and dual
degree.
Write a relational algebra expression that will
create & populate the following schema:
Dual_Transfer_2004 (SUID, OLD_IDNO,
NEW_IDNO, NAME)

© Prof. Navneet Goyal, BITS, Pilani


Relational Calculus
n Relational Algebra: More
operational, very useful for
representing execution plans.
n Relational Calculus: Lets users
describe what they want, rather
than how to compute it. (Non-
operational, declarative.)

© Prof. Navneet Goyal, BITS, Pilani


Q&A
Thank You

Das könnte Ihnen auch gefallen