0% found this document useful (0 votes)
3 views

Lecture 09

Uploaded by

engelschen12138
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 09

Uploaded by

engelschen12138
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Welcome to

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

Oracle 11g: SQL


9
Single-Row
Subqueries
 Can only return one result to
the outer query
 Operators include =, >, <, >=,
<=, < >
Oracle 11g: SQL
10
11

Single-Row
Subquery in a
WHERE Clause
• Used for comparison against
individual data

Oracle 11g: SQL


12

Single-Row
Subquery in a
HAVING Clause
• Required when returned value is
compared to grouped data

Oracle 11g: SQL


13

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

ANY and ALL


Operators
• Combine with arithmetic
operators

Oracle 11g: SQL


Multiple-Row
Subquery in a
WHERE Clause

Note: Could use IN operator or =ANY


Oracle 11g: SQL
16
Multiple-Row
Subquery in a
WHERE Clause
(continued)

Oracle 11g: SQL


17
Multiple-Row
Subquery in a
HAVING Clause

Oracle 11g: SQL


18
Multiple-Column
Subqueries
 Return more than one column
in results
 Can return more than one row
 Column list on the left side of
operator must be in parentheses
 Use the IN operator for
WHERE and HAVING clauses
Oracle 11g: SQL
19
20

Multiple-Column
Subquery in a
FROM Clause
• Creates a temporary table

Oracle 11g: SQL


21

Multiple-Column
Subquery in a
WHERE Clause

• Returns multiple columns for


evaluation
Oracle 11g: SQL
22

NULL Values
• When a subquery might return
NULL values, use NVL function

Oracle 11g: SQL


Uncorrelated
Subqueries
 Processing sequence
 Inner query is executed first
 Result is passed to outer query
 Outer query is executed
Oracle 11g: SQL
23
Correlated
Subqueries
 Inner query is executed once
for each row processed by the outer
query
 Inner query references the row
contained in the outer query
Oracle 11g: SQL
24
Correlated Subqueries
(continued)

Oracle 11g: SQL


25
Nested Subqueries
 Maximum of 255 subqueries if
nested in the WHERE clause
 No limit if nested in the FROM
clause
 Innermost subquery is resolved
first, then the next level, etc.
Oracle 11g: SQL
26
27

Nested Subqueries
(continued)
• Innermost is resolved first (A), then
the second level (B), then the outer query
(C)

Oracle 11g: SQL


Subquery in a DML
action

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)

Oracle 11g: SQL


30
MERGE Statement
(continued)
• The following explains each part of the
previous MERGE statement:
• MERGE INTO books_1 a: The BOOKS_1
table is to be changed and a table alias of “a” is
assigned to this table
• USING books_2 b: The BOOKS_2 table
will provide the data to update and/or insert into
BOOKS_1 and a table alias of “b” is assigned to this
table
• ON (a.isbn = b.isbn): The rows of the two
tables will be joined or matched based on isbn
• WHEN MATCHED THEN: If a row match
based on ISBN is discovered, execute the UPDATE
action in this clause. The UPDATE action instructs
the system to modify only two columns (Retail and
Category)
• WHEN NOT MATCHED THEN: If no
match is found based on the ISBN (a books exists
in BOOKS_2 that is not in BOOKS_1), then perform
the INSERT action in this clause
Oracle 11g: SQL
31
MERGE with WHERE
conditions

Oracle 11g: SQL


32
MERGE with
DELETE

Oracle 11g: SQL


33

You might also like