Lecture 09
Lecture 09
CST 2002:
DATABASE DESIGN AND
IMPLEMENTATIONLECTURE 9
16 January 2024
1
16 January 2024
2
READINGS
Casteel, chapter 12
NORMALISATION
– EMBEDDED REP
EATING GROUP
TICKET(ticket id, name, ((route
id, name, route cost, price charged,
((sector id, sector name, sector
distance )) )), status id, status
description)
Here we have a repeating
group within a repeating group
A ticket may have many
different routes and a route may have
many sectors. We must retain historical
details for the price charged (by route)
for each ticket.
16 January 2024
3
FIRST NORMAL
FORM (1NF)
TICKET(ticket id#, passenger
name, status id, status description)
TICKET_ROUTE(ticket id#, rou
te id#,route name, route cost, price
charged)
TICKET_ROUTE_SECTOR (tic
ket id#, route id#,sector id#, sector
name, sector distance)
16 January 2024
4
SECOND NORMAL
FORM (2NF)
TICKET(ticket id#, passenger
name, status id, status description)
TICKET_ROUTE(ticket id#, rou
te id#, price charged)
ROUTE(route id#, route name,
route cost)
TICKET_ROUTE_SECTOR (tic
ket id#, route id#, sector id#)
SECTOR(sector id#, sector
name, sector distance)
16 January 2024
5
THIRD NORMAL
FORM (3NF)
TICKET(ticket id#, passenger
name, status id#)
TICKET_ROUTE(ticket id#, rou
te id#, price charged)
ROUTE(route id#, route name,
route cost)
TICKET_ROUTE_SECTOR (tic
ket id#, route id#, sector id#)
SECTOR(sector id#, sector
name, sector distance)
STATUS(status id#, status
description)
WHAT IF THERE WAS NO
PRICE CHARGED?
16 January 2024
6
3NF WITH NO
PRICE CHARGED
TICKET(ticket id#, passenger
name, status id#)
ROUTE(route id#, route name,
route cost)
TICKET_ROUTE_SECTOR (tic
ket id#, route id#, sector id#)
SECTOR(sector id#, sector
name, sector distance)
STATUS(status id#, status
description)
The relation
TICKET_ROUTE_SECTOR also tells
us which routes exist for a ticket. The
attribute price charged was the only
reason for retaining TICKET_ROUTE
16 January 2024
7
Subqueries and
Their Uses
Subquery – a query nested
inside another query
Used when a query is based
on an unknown value
Requires SELECT and FROM
clauses
Must be enclosed in
parentheses
Place on right side of
comparison operator
Oracle 11g: SQL
8
Types of Subqueries
Single-Row
Subquery in a
WHERE Clause
• Used for comparison against
individual data
Single-Row
Subquery in a
HAVING Clause
• Required when returned value is
compared to grouped data
Single-Row
Subquery in a
SELECT Clause
• Replicates subquery value for each
row displayed
Oracle 11g: SQL
Multiple-Row Subqu
eries
Return more than one row of
results
Require use of IN, ANY, ALL,
or EXISTS operators
Oracle 11g: SQL
14
15
Multiple-Column
Subquery in a
FROM Clause
• Creates a temporary table
Multiple-Column
Subquery in a
WHERE Clause
NULL Values
• When a subquery might return
NULL values, use NVL function
Nested Subqueries
(continued)
• Innermost is resolved first (A), then
the second level (B), then the outer query
(C)
28
Oracle 11g: SQL
MERGE Statement
With a MERGE statement, a
series of DML actions can occur with a
single SQL statement
Conditionally updates one data
source based on another
Oracle 11g: SQL
29
MERGE Statement
(continued)