You are on page 1of 10

Topic 8 – SQL 2 Databases

Databases

Topic 8:
SQL 2

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.2

Scope and Coverage


This topic will cover:
• Creating tables
• More on the select statement
• Fixing errors and optimisation

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.3

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

V1.0 © NCC Education Limited

V1.0 Visuals Handout – Page 1


Topic 8 – SQL 2 Databases

SQL 2 Topic 8 - 8.4

Data Definition - 1
Create table departments

(dept_no integer not null,


d
department_name
t t varchar(30),
h (30)
location varchar2(3)
primary key dept_no);

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.5

Data Definition - 2
The table name

Create table departments Here the columns


are defined. The
datatypes
(dept_no integer not null, are specified along
with the length in
d
department_name
t t varchar(30),
h (30)
brackets.
location varchar2(3) If a column is
specified as NOT
primary key dept_no); NULL, then it
is mandatory and
must be populated
when a
new row is created.

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.6

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

V1.0 © NCC Education Limited

V1.0 Visuals Handout – Page 2


Topic 8 – SQL 2 Databases

SQL 2 Topic 8 - 8.7

Foreign Key
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(dept_no));

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.8

Modifying Tables Using SQL


• Add an extra column
• Drop a column from a table
• Modify the maximum length of the table
• Add a new constraint
• Drop a constraint
• Set a default for a column
• Drop a default for a column

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.9

Example of Adding a Column


Using Alter Table
ALTER TABLE job_type
ADD salary FLOAT;

V1.0 © NCC Education Limited

V1.0 Visuals Handout – Page 3


Topic 8 – SQL 2 Databases

SQL 2 Topic 8 - 8.10

Data Manipulation
• Select
• Order By
• Aggregate functions
• Group by
• Subqueries
• Joins

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.11

Select
• SELECT first_name

• FROM Students

• WHERE Student_type = ‘Overseas’;

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.12

All the Columns or Some of Them


• Select *
• from Students
• Where student_type = ‘Overseas’;

• Select student_id, first_name, last_name,


• From Students
• Where student_type = ‘Overseas’;

V1.0 © NCC Education Limited

V1.0 Visuals Handout – Page 4


Topic 8 – SQL 2 Databases

SQL 2 Topic 8 - 8.13

Order By - 1
• Select first_name, last_name, stu_id

• From Students

• Where student_type = ‘Overseas’

• Order by last_name;

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.14

Order By - 2
• Select first_name, last_name, stu_id

• From Students

• Where student_type = ‘Overseas’

• Order by last_name desc;

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.15

Aggregate Functions - 1
• Count
• Sum
• Avg
• Min
• Max

V1.0 © NCC Education Limited

V1.0 Visuals Handout – Page 5


Topic 8 – SQL 2 Databases

SQL 2 Topic 8 - 8.16

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

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.17

Example of Aggregate Function


Select branchID, Count(staff_id)
From workers
Where branchType = ‘Main’
Group by branchID
Having Count (staff_id) > 1
Order by branchID

• This counts the number of members of staff in main


branches where there are more than 1 staff member.
It groups them by the branchID.

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.18

Group By
• As shown it the previous slide...

• This clause is used with an aggregate function and


groups the results by some attribute
attribute.

V1.0 © NCC Education Limited

V1.0 Visuals Handout – Page 6


Topic 8 – SQL 2 Databases

SQL 2 Topic 8 - 8.19

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

• In this case, only select groups where it has been


calculated by the count function that there are more
than one member of staff.

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.20

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

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.21

Joins
Select d.department_name, w.first_name,
w.last_name
From departments d, workers w
Where d.dept
p _no = w.dept
p _no;;

V1.0 © NCC Education Limited

V1.0 Visuals Handout – Page 7


Topic 8 – SQL 2 Databases

SQL 2 Topic 8 - 8.22

Fixing Errors
• Not specifying join condition properly
• Syntax errors
• Spelling errors for keywords
• Columns not existing on tables
• Data-types being mixed up

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.23

Query Optimisation
• Making sure a query runs as efficiently and as
quickly as possible

• Performance

• Examining path a query takes through a database

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.24

TOAD
• Tools that are used to help optimise queries

• Available at http://www.quest.com/toad/

V1.0 © NCC Education Limited

V1.0 Visuals Handout – Page 8


Topic 8 – SQL 2 Databases

SQL 2 Topic 8 - 8.25

Just Do It!
• Like driving a car...

• The best way to become proficient with SQL is to


use it.

• Laboratory sessions

• Assignment

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.26

Learning Outcomes – Have We


Met Them?
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

V1.0 © NCC Education Limited

SQL 2 Topic 8 - 8.27

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
g
Management, 4th Edition. Addison Wesley.
y Chapters
p 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/

V1.0 © NCC Education Limited

V1.0 Visuals Handout – Page 9


Topic 8 – SQL 2 Databases

SQL 2 Topic 8 - 8.28

Topic 8 – SQL 2

Any Questions?

V1.0 © NCC Education Limited

V1.0 Visuals Handout – Page 10

You might also like