Professional Documents
Culture Documents
DB Topic 8
DB Topic 8
Topic 8:
SQL 2
Learning Outcomes
By the end of this topic, students will be able to:
Understand the syntax of the create statement
Understand the construction of more complex
selections
Recognise the issues around error messaging and
query optimisation
Data Definition - 1
Create table departments
Data Definition - 2
The table name
Primary Key
Create table departments
Create table departments (dept_no integer not null primary
(dept_no integer not null, key,
department_name department_name varchar(30),
varchar(30), location varchar(3));
location varchar2(3)
primary key dept_no);
Primary Key
defined in
different
places
Foreign Key
Create table workers
Data Manipulation
Select
Order By
Aggregate functions
Group by
Subqueries
Joins
Select
SELECT first_name
FROM Students
Order By - 1
Select first_name, last_name, stu_id
From Students
Order by last_name;
Order By - 2
Select first_name, last_name, stu_id
From Students
Aggregate Functions - 1
Count
Sum
Avg
Min
Max
Aggregate Functions - 2
Count returns number of values in a column
Sum returns the sum total of values of a column
Avg returns the mean average of values in
column
Min returns the lowest value in a column
Max returns the highest value in a column
Group By
As shown it the previous slide...
Having Clause
Modifies the group by clause
Select branchID, Count(staff_id)
From workers
Where branchType = Main
Group by branchID
Having Count (staff_id) > 1
Order by branchID
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);
Joins
Select d.department_name, w.first_name,
w.last_name
From departments d, workers w
Where d.dept_no = w.dept_no;
Fixing Errors
Not specifying join condition properly
Syntax errors
Spelling errors for keywords
Columns not existing on tables
Data-types being mixed up
Query Optimisation
Making sure a query runs as efficiently and as
quickly as possible
Performance
TOAD
Tools that are used to help optimise queries
Available at http://www.quest.com/toad/
Just Do It!
Like driving a car...
Laboratory sessions
Assignment
References
Benyon-Davis, P. (2003). Database Systems, 3rd
edition. Palgrave Macmillan. Chapters 11, 12 & 13.
Connolly, T. & Begg, C. (2004). Database Systems: A
Practical Approach to Design, Implementation, and
Management, 4th Edition. Addison Wesley. Chapters 5,
6 & 7.
Dietrich, S. W. (2001). Understanding Relational
Database Query Languages, 1st edition. Prentice Hall.
Chapter 5.
TOAD website http://www.quest.com/toad/
Topic 8 SQL 2
Any Questions?