Chapter 3: SQL
Chapter 3: SQL
Chapter 3: SQL
Data Definition
Basic Query Structure
Set Operations
Aggregate Functions
Null Values
Nested Subqueries
Complex Queries
Views
Modification of the Database
Joined Relations**
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.2
History
IBM Sequel language developed as part of System R project at the
SQL-86
SQL-89
SQL-92
SQL:2003
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.3
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.4
length n.
int. Integer (a finite subset of the integers that is machine-dependent).
smallint. Small integer (a machine-dependent subset of the integer
domain type).
numeric(p,d). Fixed point number, with user-specified precision of p digits,
digits.
More are covered in Chapter 4.
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.5
Example:
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.6
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.7
relation:
alter table r add A D
where A is the name of the attribute to be added to relation r and D
is the domain of A.
z
All tuples in the relation are assigned null as the value for the
new attribute.
relation:
alter table r drop A
where A is the name of an attribute of relation r
z
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.8
A ,A ,K,A ( P (r1 r2 K rm ))
1
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.9
select branch_name
from loan
In the relational algebra, the query would be:
branch_name (loan)
NOTE: SQL names are case insensitive (i.e., you may use upper- or
lower-case letters.)
z
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.10
select.
Find the names of all branches in the loan relations, and remove
duplicates
select distinct branch_name
from loan
The keyword all specifies that duplicates not be removed.
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.11
select *
from loan
The select clause can contain arithmetic expressions involving the
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.12
To find all loan number for loans made at the Perryridge branch with
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.13
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.14
select
from borrower, loan
Find the name, loan number and loan amount of all customers
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.15
old-name as new-name
Find the name, loan number and loan amount of all customers; rename the
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.16
Tuple Variables
Tuple variables are defined in the from clause via the use of the as
clause.
Find the customer names and their loan numbers for all customers
Find the names of all branches that have greater assets than
some branch located in Brooklyn.
select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and S.branch_city = Brooklyn
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.17
String Operations
SQL includes a string-matching operator for comparisons on character
strings. The operator like uses patterns that are described using two
special characters:
z
Find the names of all customers whose street includes the substring
Main.
select customer_name
from customer
where customer_street like %Main%
Match the name Main%
like Main\% escape \
SQL supports a variety of string operations such as
z
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.18
Perryridge branch
select distinct customer_name
from borrower, loan
where borrower loan_number = loan.loan_number and
branch_name = Perryridge
order by customer_name
We may specify desc for descending order or asc for ascending
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.19
Duplicates
In relations with duplicates, SQL can define how many copies of
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.20
Duplicates (Cont.)
Example: Suppose multiset relations r1 (A, B) and r2 (C) are as
follows:
r1 = {(1, a) (2,a)}
A ,A ,K,A ( P (r1 r2 K rm ))
1
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.21
Set Operations
The set operations union, intersect, and except operate on relations
retain all duplicates use the corresponding multiset versions union all,
intersect all and except all.
Suppose a tuple occurs m times in r and n times in s, then, it occurs:
z
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.22
Set Operations
Find all customers who have a loan, an account, or both:
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.23
Aggregate Functions
These functions operate on the multiset of values of a column of
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.24
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.25
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.26
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.27
Null Values
It is possible for tuples to have a null value, denoted by null, for some
of their attributes
null signifies an unknown value or that a value does not exist.
The predicate is null can be used to check for null values.
z
Example: Find all loan number which appear in the loan relation
with null values for amount.
select loan_number
from loan
where amount is null
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.28
or
null = null
unknown
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.29
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.30
Nested Subqueries
SQL provides a mechanism for the nesting of subqueries.
A subquery is a select-from-where expression that is nested within
another query.
A common use of subqueries is to perform tests for set membership, set
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.31
Example Query
Find all customers who have both an account and a loan at the bank.
Find all customers who have a loan at the bank but do not have
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.32
Example Query
Find all customers who have both an account and a loan at the
Perryridge branch
select distinct customer_name
from borrower, loan
where borrower.loan_number = loan.loan_number and
branch_name = Perryridge and
(branch_name, customer_name ) in
(select branch_name, customer_name
from depositor, account
where depositor.account_number =
account.account_number )
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.33
Set Comparison
Find all branches that have greater assets than some branch located
in Brooklyn.
select distinct T.branch_name
from branch as T, branch as S
where T.assets > S.assets and
S.branch_city = Brooklyn
Same query using > some clause
select branch_name
from branch
where assets > some
(select assets
from branch
where branch_city = Brooklyn)
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.34
0
5
6
) = true
(5 < some
0
5
) = false
(5 = some
0
5
) = true
(5 some
0
5
) = true (since 0 5)
(5 < some
(= some) in
However, ( some) not in
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.35
Example Query
Find the names of all branches that have greater assets than all
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.36
(5 < all
0
5
6
) = false
(5 < all
6
10
) = true
(5 = all
4
5
) = false
(5 all
4
6
( all) not in
However, (= all) in
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.37
nonempty.
exists r r
not exists r r =
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.38
Example Query
Find all customers who have an account at all branches located in
Brooklyn.
select distinct S.customer_name
from depositor as S
where not exists (
(select branch_name
from branch
where branch_city = Brooklyn)
except
(select R.branch_name
from depositor as T, account as R
where T.account_number = R.account_number and
S.customer_name = T.customer_name ))
Note that X Y = X Y
Note: Cannot write this query using = all and its variants
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.39
branch.
select T.customer_name
from depositor as T
where unique (
select R.customer_name
from account, depositor as R
where T.customer_name = R.customer_name and
R.account_number = account.account_number and
account.branch_name = Perryridge )
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.40
Example Query
Find all customers who have at least two accounts at the Perryridge
branch.
select distinct T.customer_name
from depositor as T
where not unique (
select R.customer_name
from account, depositor as R
where T.customer_name = R.customer_name and
R.account_number = account.account_number and
account.branch_name = Perryridge)
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.41
Derived Relations
SQL allows a subquery expression to be used in the from clause
Find the average account balance of those branches where the average
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.42
With Clause
The with clause provides a way of defining a temporary view whose
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.43
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.44
Views
In some cases, it is not desirable for all users to see the entire logical
model (that is, all the actual relations stored in the database.)
Consider a person who needs to know a customers loan number but
has no need to see the loan amount. This person should see a relation
described, in SQL, by
(select customer_name, loan_number
from borrower, loan
where borrower.loan_number = loan.loan_number )
A view provides a mechanism to hide certain data from the view of
certain users.
Any relation that is not of the conceptual model but is made visible to a
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.45
View Definition
A view is defined using the create view statement which has the
form
create view v as < query expression >
where <query expression> is any legal SQL expression. The view
name is represented by v.
Once a view is defined, the view name can be used to refer to the
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.46
Example Queries
A view consisting of branches and their customers
select customer_name
from all_customer
where branch_name = Perryridge
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.47
v2
A view relation v is said to be recursive if it depends on itself.
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.48
View Expansion
A way to define the meaning of views defined in terms of other views.
Let view v1 be defined by an expression e1 that may itself contain uses
of view relations.
View expansion of an expression repeats the following replacement
step:
repeat
Find any view relation vi in e1
Replace the view relation vi by the expression defining vi
until no more view relations are present in e1
As long as the view definitions are not recursive, this loop will
terminate
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.49
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.50
Example Query
Delete the record of all accounts with balances below the average at
the bank.
delete from account
where balance < (select avg (balance )
from account )
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.51
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.52
savings account. Let the loan number serve as the account number for the
new savings account
insert into account
select loan_number, branch_name, 200
from loan
where branch_name = Perryridge
insert into depositor
select customer_name, loan_number
from loan, borrower
where branch_name = Perryridge
and loan.account_number = borrower.account_number
The select from where statement is evaluated fully before any of its
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.53
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.54
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.55
Update of a View
Create a view of all loan data in the loan relation, hiding the amount
attribute
create view branch_loan as
select branch_name, loan_number
from loan
Add a new tuple to branch_loan
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.56
create view v as
select branch_name from account
insert into v values (L-99, Downtown, 23)
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.57
Joined Relations**
Join operations take two relations and return as a result another
relation.
These additional operations are typically used as subquery
tuple in the other relation (based on the join condition) are treated.
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.58
Relation borrower
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.59
loan.loan_number = borrower.loan_number
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.60
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.61
Find all customers who have either an account or a loan (but not both)
at the bank.
select customer_name
from (depositor natural full outer join borrower )
where account_number is null or loan_number is null
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.62
End of Chapter 3
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.64
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.65
Figure 3.4:
The loan and borrower relations
Database System Concepts, 5th Ed., slide version 5.0, June 2005
3.66