Example Query: 6.6.3, 8.3.16
Query
Get SNAME who supply ALL parts.
# Assume that S2 does not supply P3
SELECT DISTINCT S.SNAME
FROM S # 5. then S2 will not be in final result set
WHERE NOT EXISTS # 4. then this will be FALSE
(
# 3. then P3 will be in result set
SELECT *
FROM P
WHERE NOT EXISTS # 2. then this will be TRUE for P3
(
# 1. since S2 does not supply P3, then result will be empty
SELECT *
FROM SP
WHERE SP.S# = S.S#
AND SP.P# = P.P# )
)
) ;
GOOD GUYS: TRUE on first NOT EXISTS, FALSE on second NOT EXISTS
Example Query: 7.3.3, 6.6.1 [318]
Query
Get SNAME who supply P2.
Algebra:
Tuple Calculus:
{
'
}
Domain Calculus:
{
'
}
Example Query: 7.3.7, 6.6.6 [319]
Query
Get SNAME (S#) who do NOT supply P2.
A shipment does not exist where the supplier supplies P2.
Algebra:
SQL: NOT IN (NOT EXISTS)
Tuple Calculus (Warning: the following are not SAFE):
{
'
}
{
') }
Remember:
{
') }
Domain Calculus:
{
'
}
{
') }
Example Query: 7.3.6, 6.6.3 [320]
Query
Get S# who supply ALL parts.
Algebra:
Algebra:
SQL: NOT IN, NOT IN (NOT EXISTS, NOT EXISTS)
Tuple Calculus:
{
}
Remember:
{
}
Domain Calculus:
{
}
{
}
A part does not exist for which the supplier does not have a shipment.
Divide in Calculus [321]
Define schema
Tuple Calculus:
{
}
Domain Calculus:
{
}
Safety of Expressions [322]
Evaluation:
Limited (universe restricted to database instance):
Unlimited (no restriction):
Goal: same result relation independent of limited or unlimited
Example:
Limited: empty relation
Unlimited: infinite relation
Solution:
where
Safety of Expressions [323]
Example:
')
Limited: maybe true
Unlimited: always true
Solution:
')
Example:
'}
Limited: finite relation
Unlimited: infinite relation
Solution:
'}
Solution:
'
Safety of Expressions [324]
Example:
')
Limited: maybe true
Unlimited: always false
Wrong Solution:
')
Solution:
')
but this could be vacuously true (empty
) or
Example:
')
but there are no 9999's
Solution: