Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Chapter 3 - Objectives
Purpose and importance of SQL, the main language
for querying relational databases. How to retrieve data using the SELECT statement. How to insert data using the INSERT statement. How to update data using the UPDATE statement. How to delete data using the DELETE statement. About an alternative language for querying relational databases called QBE.
require, rather than how to get it Essentially free-format Consists of standard English words like SELECT, INSERT, and UPDATE Can be used by range of users.
Objectives of SQL
Ideally database language should let user:
create database and table structures; perform basic tasks like insert, update, delete; perform both simple and complex queries.
Must perform these tasks with minimal user effort. Must be portable.
user-defined words. Reserved words are a fixed part of SQL and must be spelt exactly as required and cannot be split across lines. User-defined words: made up by user and represent names of various database objects such as tables, columns, views.
Pearson Education 2009 6
Literals
Literals are constants used in SQL statements.
All non-numeric literals must be enclosed in single quotes (eg. New York).
All numeric literals must not be enclosed in
condition]
11
12
13
14
15
Calculated Fields
List the monthly salary for all staff, showing the staff number, name, position and monthly salary. SELECT staffNo, name, position, salary/12 FROM Staff;
16
the value of another. Range: test whether value falls within a specified range. Set membership: test whether the value of an expression equals one of a set of values. Pattern match: test whether a string matches a specified pattern. Null: test whether a column has a unknown value.
19
20
21
22
24
Could have expressed this as: SELECT catalogNo, title, genre FROM DVD WHERE genre IN (Sci-Fi, Children);
IN is more efficient when set contains many values.
25
26
LIKE Sally% means the first 5 characters must be Sally followed by anything.
27
28
29
30
MAX
31
numeric fields.
Apart from COUNT(*), each function eliminates nulls first and operates only on remaining non-
null values.
Pearson Education 2009 32
DISTINCT has no effect with MIN/MAX, but may have with SUM/AVG.
33
list and in HAVING clause. If SELECT list includes an aggregate function and there is no GROUP BY clause, SELECT list cannot reference a column out with an aggregate function. For example, following is illegal:
SELECT staffNo, COUNT(salary) FROM Staff;
Pearson Education 2009 34
35
36
Grouping Results
Use GROUP BY clause to get sub-totals. SELECT and GROUP BY closely integrated: each item in SELECT list must be single-valued per group, and SELECT clause may only contain:
column names aggregate functions
constants
expression with combination of above.
37
Grouping Results
All column names in SELECT list must appear in
used only in an
38
39
Column names in HAVING clause must also appear in the GROUP BY list or be contained within an aggregate function.
Pearson Education 2009 40
42
Subselects
Some SQL statements can have a SELECT
43
44
Using a Subquery
Inner SELECT finds distribution center number for distribution center at 8 Jefferson Way (B001).
Outer SELECT then retrieves details of all staff who work at this center. Outer SELECT then becomes:
SELECT staffNo, name, position FROM Staff WHERE branchNo = B001;
Pearson Education 2009 45
46
47
Instead, use subquery to find average salary (42000), and then use outer SELECT to find those staff with salary greater than this average:
SELECT staffNo, name, position FROM Staff WHERE salary > 42000;
48
49
50
subquery must appear on right-hand side. A subquery may not be used as an operand in an expression.
51
Multi-Table Queries
Can use subqueries provided result columns
come from same table. If result columns come from more than one table must use a join. To perform join, include more than one table in FROM clause. Use comma as separator with typically a WHERE to specify join column(s).
Pearson Education 2009 52
Multi-Table Queries
Also possible to use an alias for a table named in FROM clause.
53
54
Simple Join
Only those rows from both tables with identical
values in the actorNo columns (a. actorNo = da. actorNo) included in result.
55
da.actorNo;
FROM Actor JOIN DVDActor USING actorNo FROM Actor NATURAL JOIN DVDActor
56
57
58
for use only with sub-queries. They produce a simple true/false result. EXISTS is true if and only if there exists at least one row in the result table returned by the subquery; it is false if the subquery returns an empty table. For simplicity, it is common for subqueries following one of these keywords to be of the form:
(SELECT * FROM......)
59
61
list of all columns in their original CREATE TABLE order. Any columns omitted must have been declared as NULL or a DEFAULT was specified when table was created.
62
items in two lists; data type of each item in dataValueList must be compatible with data type of corresponding column.
63
66
updatable view. searchCondition is optional; if omitted, all rows are deleted from table. This does not delete table. If searchCondition specified, only those rows that satisfy condition are deleted.
Pearson Education 2009 68
Query-By-Example (QBE)
QBE alternative graphical-based point-and-click way of querying database. One of easiest ways for non-technical users to query database. Query database by illustrating query to be answered using a template.
70
71
72
73
74
75
76
77
78