Sie sind auf Seite 1von 26

Database Design and

Development
Topic 5:
Data Retrieval (2)

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.2

Scope and Coverage


This topic will cover:

V1.0

Referential integrity in relational databases


Types of joins
Retrieving data using joins
Retrieving data using sub-queries

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.3

Learning Outcomes
By the end of this topic students will be able to:
Outline the concept of referential integrity and say
why it is important in a relational database
Understand how to retrieve data from one or more
tables using join
Understand how to retrieve data from one or more
tables using sub-queries

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.4

Referential Integrity
A foreign key must refer to a candidate key (usually
a primary key) elsewhere in the database or must
be null.
Why is this important?

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.5

Keys - 1

Student ID

Name

28

Guy Smith

23

Sarah
Anusiem

Primary keys
Student ID

Modules Code

28

Net

23

Med 1

28

OS

23

Med 2

V1.0

Address

Course
History

12 New Street
Lagos

Computing

Module Code

Name

Med1

Medieval History 1

OS

Operating Systems

Med2

Medieval History 2

NET

Networks

TCE

Twentieth Century
History

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.6

Keys - 2
Foreign keys

Student ID

Modules Code

28

Net

23

Med 1

28

OS

23

Med 2

V1.0

Student ID

Name

28

Guy Smith

23

Sarah
Anusiem

Address

Course
History

12 New Street
Lagos

Computing

Module Code

Name

Med1

Medieval History 1

OS

Operating Systems

Med2

Medieval History 2

NET

Networks

TCE

Twentieth Century
History

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.7

Recap Parts of a Select Statement


What does Select do

Select branchID, Count(staff_id)


From workers
Where branchType = Main
Group by branchID
Having Count (staff_id) > 1
Order by branchID
What does the Order by do?

V1.0

What is the
From doing?
What is the Where
soing?

Groups by some column


Value

What does Having


do?

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.8

What columns will appear

Select branchID, Count(staff_id)


Which table
From workers
Where branchType = Main
Condition
Group by branchID
Groups by some column
Value
Having Count (staff_id) > 1
Order by branchID
Specifies the order of the result

V1.0

Restricts what will


Be grouped

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.9

Two Tables
Create table departments
(dept_no number(5 ) not null,
department_name varchar(30),
location varchar2(3)
primary key dept_no);
Create table workers
(emp_no number(5) not null,
first_name varchar(30),
last_name varchar(30),
job_title varchar(30),
age number(3),
dept_no number(5),
primary key emp_no,
foreign key (dept_no) references departments)

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.10

Joining Tables in an SQL Query


Select d.department_name, w.first_name,
w.last_name, w.job_title
from departments d, workers w
where d.dept_no = w.dept_no;

What does this query do?

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.11

The Join Operation


Used to retrieve data from more than one table
Simple join
Multi-table join
Outer-join

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.12

Simple Join on Two Tables


Select d.department_name, d.location,
w.first_name, w.last_name, w.job_title
from departments d, workers w
where d.dept_no = w.dept_no
and d.location = 'Cairo';
Use the attribute that connects the tables in the
database. In this case dept_no.

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.13

Joining more than two Tables


Select d.department_name, d.location,
w.first_name, w.last_name, w.job_title, j.salary
from departments d, workers w, jobs
where d.dept_no = w.dept_no
and w.job_title = j.job_title;
Retrieves the salary from a separate table jobs
which is linked via job_title

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.14

Outer Join
Derives from an operation in relational algebra
When joining two tables what happens if there is no
matching row in one of the tables.

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.15

V1.0

EmployeeID

Name

DeptNo

Ron Howard

Satpal Singh

Arfir Rahman

Jody Kodogo

DeptNo

DeptName

History

English Literature

Mathematics

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.16

EmployeeID

Name

DeptNo

Ron Howard

Satpal Singh

Arfir Rahman

Jody Kodogo

DeptNo

DeptName

History

English Literature

Mathematics

33

Chemistry

Select E.employeeID, D.DeptNo, D.DeptName


From Employees E, Departments D
Where E.deptNo = D.deptNo;
What will be the result of this query?

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.17

V1.0

EmployeeID

Name

DeptNo

Ron Howard

Satpal Singh

Arfir Rahman

Jody Kodogo

Select E.employeeID, D.DeptNo,


D.DeptName
From Employees E, Departments D
Where E.deptNo = D.deptNo;

DeptNo

DeptName

History

English Literature

Mathematics

33

Chemistry

EmployeeID

DeptNo

DeptName

History

English Literature

Mathematics

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.18

Types of Outer Join


LEFT OUTER JOIN all the rows from the first (left)
table that are unmatched in the second (right) table.
RIGHT OUTER JOIN all the of the second (right)
table that hare unmatched rows in the first (left)
table

FULL OUTER JOIN all unmatched rows.

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.19

Left Outer Join


Select E.employeeID, D.DeptNo, D.DeptName
From Employees E LEFT JOIN Departments D
ON E.deptNo = D.deptNo;

EmployeeID

DeptNo

DeptName

History

English Literature

Mathematics

5
7

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.20

Right Outer Join


Select E.employeeID, D.DeptNo, D.DeptName
From Employees E RIGHT JOIN Departments D
ON E.deptNo = D.deptNo;

V1.0

EmployeeID

DeptNo

DeptName

History

English Literature

Mathematics

33

Chemistry

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.21

Full Outer Join


Select E.employeeID, D.DeptNo, D.DeptName
From Employees E LEFT JOIN Departments D
ON E.deptNo = D.deptNo;

EmployeeID

DeptNo

DeptName

History

English Literature

Mathematics

33

Chemistry

5
7

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.22

Sub-queries
Select d.department_name, d.location
From departments d, workers w
Where d.dept_no = w.dept_no
And w.age =
(select max(w2.age)
From workers w2);

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.23

Correlated Sub-query
SELECT p.product_name FROM product p
WHERE p.product_id = (SELECT o.product_id
FROM order_items o
WHERE o.product_id = p.product_id);
This will select product_names from products where
all the products exist on a table called older_items.

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.24

Learning Outcomes
By the end of this unit students will be able to:
Outline the concept of referential integrity and say
why it is important in a relational database
Understand how to retrieve data from one or more
tables using join
Understand how to retrieve data from one or more
tables using sub-queries
Have we met them?

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.25

References
Connolly and Begg, Database Systems a Practical
Approach to Design, implementation and Management
Chapters 4 and 5 Fourth Edition
Benyon-Davis, Database Systems. Chapters 12 and13
Third Edition
Dietrich, Suzanne W, Understanding Relational
Database Query Languages Chapter 5 1st Edition 2001
Beginner SQL Tutorial: http://beginner-sqltutorial.com/sql-subquery.htm Retrieved 01-June-2011

V1.0

NCC Education Limited

Data Retrieval (2) Topic 5 - 5.26

Topic 5 Data Retrieval (2)


Any Questions?

V1.0

NCC Education Limited

Das könnte Ihnen auch gefallen