Professional Documents
Culture Documents
DDD Topic 5 - Data Retrieval
DDD Topic 5 - Data Retrieval
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
Referential Integrity
• A foreign key must refer to a candidate key (usually
a primary key) elsewhere in the database or must
be null.
23 Med 2
Keys - 2…
Student ID Name Address Course
Order by branchID
Restricts what will
Specifies the order of the result Be grouped
Two Tables
Create table departments
(dept_no number(5 ) not null,
department_name varchar(30),
location varchar2(3)
primary key dept_no);
• Simple join
• Multi-table join
• Outer-join
Outer Join
• Derives from an operation in relational algebra
DeptNo DeptName
1 History
3 English Literature
7 Mathematics
DeptNo DeptName
1 History
3 English Literature
7 Mathematics
33 Chemistry
DeptNo DeptName
1 History
3 English Literature
7 Mathematics
33 Chemistry
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);
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);
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?
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-sql-tutorial.com/sql-subquery.htm
Retrieved 01-June-2011
Any Questions?