Chapter 5
Chapter 5
CHAPTER 5 - OBJECTIVES
Purpose and importance of SQL.
How to retrieve data from database using
SELECT and:
Use
2
Pearson Education Limited 1995, 2005
CHAPTER 5 - OBJECTIVES
Join
tables together.
Perform
set operations
EXCEPT).
(UNION,
INTERSECT,
using
INSERT,
3
Pearson Education Limited 1995, 2005
OBJECTIVES OF SQL
4
Pearson Education Limited 1995, 2005
OBJECTIVES OF SQL
OBJECTIVES OF SQL
6
Pearson Education Limited 1995, 2005
OBJECTIVES OF SQL
OBJECTIVES OF SQL
8
Pearson Education Limited 1995, 2005
HISTORY OF SQL
In 1974, D. Chamberlin (IBM San Jose
Laboratory) defined language called Structured
English Query Language (SEQUEL).
A revised version, SEQUEL/2, was defined in
1976 but name was subsequently changed to
SQL for legal reasons.
9
Pearson Education Limited 1995, 2005
HISTORY OF SQL
Still pronounced see-quel, though official
pronunciation is S-Q-L.
IBM subsequently produced a prototype DBMS
called System R, based on SEQUEL/2.
Roots
of SQL, however, are in SQUARE
(Specifying Queries as Relational Expressions),
which predates System R project.
10
Pearson Education Limited 1995, 2005
HISTORY OF SQL
In late 70s, ORACLE appeared and was probably
first commercial RDBMS based on SQL.
In 1987, ANSI and ISO published an initial
standard for SQL.
In 1989, ISO published an addendum that defined
an Integrity Enhancement Feature.
In 1992, first major revision to ISO standard
occurred, referred to as SQL2 or SQL/92.
In 1999, SQL:1999 was released with support for
object-oriented data management.
In late 2003, SQL:2003 was released.
11
Pearson Education Limited 1995, 2005
IMPORTANCE OF SQL
SQL
has
become
part
of
application
architectures such as IBMs Systems Application
Architecture.
It
is strategic choice of many large and
influential organizations (e.g. X/OPEN).
SQL is Federal Information Processing Standard
(FIPS) to which conformance is required for all
sales of databases to American Government.
12
Pearson Education Limited 1995, 2005
IMPORTANCE OF SQL
13
Pearson Education Limited 1995, 2005
14
Pearson Education Limited 1995, 2005
Each
15
Pearson Education Limited 1995, 2005
LITERALS
17
Pearson Education Limited 1995, 2005
SELECT STATEMENT
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM
TableName [alias] [, ...]
[WHERE condition]
[GROUP BY
columnList] [HAVING
condition]
[ORDER BY
columnList]
18
Pearson Education Limited 1995, 2005
SELECT STATEMENT
FROM
Specifies table(s) to be used.
WHERE
Filters rows.
GROUP BY Forms groups of rows with same
HAVING
SELECT
ORDER BY
column value.
19
Pearson Education Limited 1995, 2005
SELECT STATEMENT
20
Pearson Education Limited 1995, 2005
EXAMPLE 5.1
ROWS
21
Pearson Education Limited 1995, 2005
EXAMPLE 5.1
ROWS
22
Pearson Education Limited 1995, 2005
23
Pearson Education Limited 1995, 2005
24
Pearson Education Limited 1995, 2005
25
Pearson Education Limited 1995, 2005
26
Pearson Education Limited 1995, 2005
EXAMPLE
FIELDS
5.4
CALCULATED
27
EXAMPLE
FIELDS
5.4
CALCULATED
AS monthlySalary
28
Pearson Education Limited 1995, 2005
EXAMPLE
5.5
COMPARISON
SEARCH CONDITION
List all staff with a salary greater than 10,000.
SELECT staffNo, fName, lName, position, salary
FROM Staff
WHERE salary > 10000;
29
Pearson Education Limited 1995, 2005
EXAMPLE
5.6
COMPOUND
COMPARISON SEARCH CONDITION
List addresses of all branch offices in London or
Glasgow.
SELECT *
FROM Branch
WHERE city = London OR city = Glasgow;
30
Pearson Education Limited 1995, 2005
EXAMPLE 5.7
CONDITION
RANGE
SEARCH
31
Pearson Education Limited 1995, 2005
EXAMPLE 5.7
CONDITION
RANGE
SEARCH
32
Pearson Education Limited 1995, 2005
EXAMPLE 5.7
CONDITION
RANGE
SEARCH
34
Pearson Education Limited 1995, 2005
35
Pearson Education Limited 1995, 2005
36
Pearson Education Limited 1995, 2005
37
Pearson Education Limited 1995, 2005
EXAMPLE 5.10
CONDITION
NULL
SEARCH
viewDate
FROM Viewing
WHERE propertyNo = PG4 AND
comment IS NULL;
38
Pearson Education Limited 1995, 2005
EXAMPLE 5.10
CONDITION
NULL
SEARCH
39
EXAMPLE 5.11
ORDERING
SINGLE COLUMN
40
Pearson Education Limited 1995, 2005
EXAMPLE 5.11
ORDERING
SINGLE COLUMN
41
Pearson Education Limited 1995, 2005
42
Pearson Education Limited 1995, 2005
43
Pearson Education Limited 1995, 2005
FROM PropertyForRent
ORDER BY type, rent DESC;
44
Pearson Education Limited 1995, 2005
45
Pearson Education Limited 1995, 2005
SELECT
STATEMENT
AGGREGATES
dalam
kolom
kolom
kolom
kolom
46
SELECT
STATEMENT
AGGREGATES
47
Pearson Education Limited 1995, 2005
SELECT
STATEMENT
AGGREGATES
48
Pearson Education Limited 1995, 2005
SELECT
STATEMENT
AGGREGATES
49
Pearson Education Limited 1995, 2005
50
EXAMPLE
5.14
COUNT(DISTINCT)
USE
OF
51
52
Pearson Education Limited 1995, 2005
EXAMPLE 5.16
AVG
53
54
Pearson Education Limited 1995, 2005
branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;
56
Pearson Education Limited 1995, 2005
57
Pearson Education Limited 1995, 2005
RESTRICTED GROUPINGS
HAVING CLAUSE
60
Pearson Education Limited 1995, 2005
SUBQUERIES
61
Pearson Education Limited 1995, 2005
62
Pearson Education Limited 1995, 2005
FROM Staff
WHERE branchNo = B003;
63
Pearson Education Limited 1995, 2005
64
Pearson Education Limited 1995, 2005
66
Pearson Education Limited 1995, 2005
67
Pearson Education Limited 1995, 2005
SUBQUERY RULES
SUBQUERY RULES
Ketika
subquery
adalah
operan
dalam
perbandingan, subquery harus muncul di sisi
kanan.
Sebuah subquery tidak boleh
sebagai operan dalam ekspresi.
digunakan
69
Pearson Education Limited 1995, 2005
71
Pearson Education Limited 1995, 2005
72
Pearson Education Limited 1995, 2005
74
Pearson Education Limited 1995, 2005
76
Pearson Education Limited 1995, 2005
MULTI-TABLE QUERIES
MULTI-TABLE QUERIES
78
Pearson Education Limited 1995, 2005
79
Pearson Education Limited 1995, 2005
80
Pearson Education Limited 1995, 2005
83
Pearson Education Limited 1995, 2005
87
Pearson Education Limited 1995, 2005
COMPUTING A JOIN
Prosedur untuk menghasilkan hasil join adalah:
1. Bentuk produk Cartesian tabel disebutkan
dalam klausa FROM.
2. Jika ada klausul WHERE, menerapkan kondisi
pencarian untuk setiap baris dari tabel produk,
mempertahankan baris yang memenuhi kondisi.
3. Untuk setiap baris yang tersisa, menentukan
nilai setiap item dalam daftar SELECT untuk
menghasilkan satu baris dalam tabel hasil.
88
Pearson Education Limited 1995, 2005
COMPUTING A JOIN
4.
Jika
DISTINCT
telah
ditetapkan,
menghilangkan duplikasi baris dari tabel hasil.
OUTER JOINS
Jika salah satu baris dari sebuah tabel
bergabung tak tertandingi, baris dihilangkan
dari hasil tabel.
Outer join operasi mempertahankan baris yang
tidak memenuhi kondisi join.
Pertimbangkan tabel berikut:
90
Pearson Education Limited 1995, 2005
OUTER JOINS
91
Pearson Education Limited 1995, 2005
OUTER JOINS
Tabel hasil memiliki dua baris di mana kota
yang sama.
Tidak ada baris yang sesuai dengan cabang di
Bristol dan Aberdeen.
Untuk menyertakan baris yang tak tertandingi
dalam hasil tabel, menggunakan Outer
bergabung.
92
Pearson Education Limited 1995, 2005
93
Pearson Education Limited 1995, 2005
94
Pearson Education Limited 1995, 2005
95
Pearson Education Limited 1995, 2005
96
Pearson Education Limited 1995, 2005
97
Pearson Education Limited 1995, 2005
98
Pearson Education Limited 1995, 2005
100
Pearson Education Limited 1995, 2005
EXAMPLE
EXISTS
5.31
QUERY
USING
EXAMPLE
EXISTS
5.31
QUERY
USING
102
Pearson Education Limited 1995, 2005
EXAMPLE
EXISTS
5.31
QUERY
USING
EXAMPLE
EXISTS
5.31
QUERY
USING
104
Pearson Education Limited 1995, 2005
UNION,
INTERSECT,
DIFFERENCE (EXCEPT)
AND
105
Pearson Education Limited 1995, 2005
UNION,
INTERSECT,
DIFFERENCE (EXCEPT)
AND
UNION,
INTERSECT,
DIFFERENCE (EXCEPT)
AND
107
Pearson Education Limited 1995, 2005
FROM Branch
WHERE city IS NOT NULL) UNION
(SELECT city
FROM PropertyForRent
WHERE city IS NOT NULL);
108
Pearson Education Limited 1995, 2005
Or
(SELECT *
FROM Branch
WHERE city IS NOT NULL)
UNION CORRESPONDING BY city
(SELECT *
FROM PropertyForRent
WHERE city IS NOT NULL);
109
Pearson Education Limited 1995, 2005
110
Pearson Education Limited 1995, 2005
INTERSECT
(SELECT city FROM PropertyForRent);
111
Pearson Education Limited 1995, 2005
Or
(SELECT * FROM Branch)
INTERSECT CORRESPONDING BY city
(SELECT * FROM PropertyForRent);
112
Bisa menulis
INTERSECT:
ulang
query
ini
tanpa
operator
SELECT b.city
FROM Branch b PropertyForRent p
WHERE b.city = p.city;
Or:
Or
Or
INSERT
INSERT INTO TableName [ (columnList) ]
VALUES (dataValueList)
116
Pearson Education Limited 1995, 2005
INSERT
dataValueList harus sesuai columnList sebagai
berikut:
jumlah item dalam setiap daftar harus sama;
harus korespondensi langsung dalam posisi
item dalam dua daftar;
tipe data dari setiap item dalam dataValueList
harus sesuai dengan jenis data kolom yang
sesuai.
117
Pearson Education Limited 1995, 2005
Assistant,
M,
118
Pearson Education Limited 1995, 2005
EXAMPLE 5.36
DEFAULTS
INSERT
USING
INSERT SELECT
120
Pearson Education Limited 1995, 2005
StaffPropCount(staffNo,
propCnt)
Populate StaffPropCount
PropertyForRent tables.
fName,
using
lName,
Staff
and
121
Pearson Education Limited 1995, 2005
122
UPDATE
UPDATE TableName
SET columnName1 = dataValue1
[, columnName2 = dataValue2...]
[WHERE searchCondition]
124
Pearson Education Limited 1995, 2005
UPDATE
Klausa WHERE adalah opsional:
jika dihilangkan, bernama kolom diperbarui
untuk semua baris dalam tabel;
jika ditentukan, hanya baris yang memenuhi
SearchCondition diperbarui.
New dataValue (s) harus sesuai dengan tipe
data untuk kolom yang sesuai.
125
Pearson Education Limited 1995, 2005
EXAMPLE
ROWS
5.38/39
UPDATE
ALL
126
Pearson Education Limited 1995, 2005
127
Pearson Education Limited 1995, 2005
DELETE
DELETE FROM TableName
[WHERE searchCondition]
atau
jika
dari
Jika
yang
128
Pearson Education Limited 1995, 2005
EXAMPLE
5.41/42
SPECIFIC ROWS
DELETE
Hapus
semua
tampilan
dengan
berhubungan dengan PG4 properti.
yang
129
Pearson Education Limited 1995, 2005