Beruflich Dokumente
Kultur Dokumente
Development
Topic 5:
Data Retrieval (2)
V1.0
V1.0
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
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
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
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
V1.0
What is the
From doing?
What is the Where
soing?
V1.0
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
V1.0
V1.0
V1.0
V1.0
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
V1.0
EmployeeID
Name
DeptNo
Ron Howard
Satpal Singh
Arfir Rahman
Jody Kodogo
DeptNo
DeptName
History
English Literature
Mathematics
EmployeeID
Name
DeptNo
Ron Howard
Satpal Singh
Arfir Rahman
Jody Kodogo
DeptNo
DeptName
History
English Literature
Mathematics
33
Chemistry
V1.0
V1.0
EmployeeID
Name
DeptNo
Ron Howard
Satpal Singh
Arfir Rahman
Jody Kodogo
DeptNo
DeptName
History
English Literature
Mathematics
33
Chemistry
EmployeeID
DeptNo
DeptName
History
English Literature
Mathematics
V1.0
EmployeeID
DeptNo
DeptName
History
English Literature
Mathematics
5
7
V1.0
V1.0
EmployeeID
DeptNo
DeptName
History
English Literature
Mathematics
33
Chemistry
EmployeeID
DeptNo
DeptName
History
English Literature
Mathematics
33
Chemistry
5
7
V1.0
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
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
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
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
V1.0