Chapter 4: SQL
Chapter 4: SQL
Chapter 4: SQL
■ Basic Structure
■ Set Operations
■ Aggregate Functions
■ Null Values
■ Nested Subqueries
■ Derived Relations
■ Views
■ Modification of the Database
■ Joined Relations
■ Data Definition Language
■ Embedded SQL
■ SQL is based on set and relational operations with
certain modifications and enhancements
■ A typical SQL query has the form:
select A1, A2, ..., An
from r1, r2, ..., rm
where P
★ Ais represent attributes
★ ris represent relations
★ P is a predicate.
■ This query is equivalent to the relational algebra
expression.
■ ∏A1, A2, ..., An(σρ (r1 x r1 x ... x rm))
■ The result of an SQL query is a relation.
select distinct T.branchname
from branch as T, branch as S
Where T .assets > S. assets and S. branchcity = “Brooklyn”
r1 = {(1, a) (2,a)} r2 = {(2), (3), (3)}
■ Then ΠB(r1) would be {(a), (a)}, while ΠB(r1) x r2 would
be
{(a,2), (a,2), (a,3), (a,3), (a,3), (a,3)}
■ SQL duplicate semantics:
select A1,, A2, ..., rm
from r1, r1, ..., rm
where P
is equivalent to the multiset version of the expression:
Π A1,, A2, ..., An(σρ I(r1 x r2 x ... x rm))
■ Find the number of depositors for each branch.
select branchname, count (distinct customername)
from depositor, account
where depositor.accountnumber = account.account
number
group by branchname
Note: Attributes in select clause outside of aggregate
functions must appear in group by list
■ F in r ⇔ ∃ t ∈ r (t = F)
0
(5 in 4 ) = true
5
0
(5 in 4 ) = false
6
0
(5 not in 4 ) = true
0
(5< some 5 ) = true
(read: 5 < some tuple in the relation)
6
0
(5< some 5 ) = false
0
(5 = some 5 ) = true
0
(5 ≠ some 5 ) = true (since 0 ≠ 5)
(= some) ≡ in
However, (≠ some) ≡ not in
Database System Concepts 4.28 ©Silberschatz, Korth and Sudarshan
Example Query
■ Find all branches that have greater assets than some branch
located in Brooklyn.
select branchname
from branch
where assets >some
(select assets
from branch
where branchcity = “Brooklyn”
0
(5< all 5 ) = false
6
6
(5< all 10 ) = true
4
(5 = all 5 ) = false
4
(5 ≠ all 6 ) = true (since 5 ≠ 4 and 5 ≠ 6)
(≠ all) ≡ not in
However, (= all) ≡ in
Database System Concepts 4.30 ©Silberschatz, Korth and Sudarshan
Example Query
■ Find the names of all branches that have greater assets than all
branches located in Brooklyn.
select branchname
from branch
where assets > all
(select assets
from branch
where branchcity = “Brooklyn”)
■ The unique construct tests whether a subquery has any
duplicate tuples in its result.
■ Find all customers who have only one account at the
Perryridge branch.
select T customername
from depositor as T
where unique (
select R customername
from account, depositor as R
where T customername = R customername and
R accountnumber = account.accountnumber
and
account.branchname = “Perryridge”)
select distinct T customername
from depositor T
where not unique (
select R customername
from account, depositor as R
where T customername = R customername and
R accountnumber account.accountnumber and
account.branchname = “Perryridge”)
create view v as <query expression>
where:
★ <query expression> is any legal expression
★ The view name is represented by v
insert into account (branchname, balance, accountnumber)
values (“Perryridge”, 1200, A9732)
■ Add a new tuple to account with balance set to null
insert into account
values (“Perryridge”, A777, null)
update account
set balance = balance ∗ 1.05
where balance ≤ 10000
★ The order is important
★ Can be done better using the case statement (Exercise 4, 11)
Join Types Join Conditions
inner join natural
left outer join on <predicate>
right outer join using (A1, A2, ..., An)
full outer join
■ Relation loan
Relation borrower
customername loannumber
Jones L170
Smith L230
Hayes L155
■ loan inner join borrower on
loan.loannumber = borrower.loannumber
branchname loannumber amount customername loannumber
■ loan natural inner join borrower
Fine 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
Allows the specification of not only a set of relations but also
information about each relation, including:
■ The schema for each relation.
■ The domain of values associated with each attribute.
■ Integrity constraints
■ The set of indices to be maintained for each relations.
■ Security and authorization information for each relation.
■ The physical storage structure of each relation on disk.
■ An SQL relation is defined using the create table
command:
create table r (A1 D1, A2 D2, ..., An Dn,
(integrityconstraint1),
...,
(integrityconstraintk))
★ r is the name of the relation
★ each Ai is an attribute name in the schema of relation r
★ Di is the data type of values in the domain of attribute Ai
■ Example:
create table branch
(branchname char(15) not null,
branchcity char(30),
assets integer)
alter table r drop A
when A is the name of an attribute of relation r.
From within a host language, find the names and account
numbers of customers with more than the variable amount
dollars in some account.
■ Specify the query in SQL and declare a cursor for it
EXEC SQL
declare c cursor for
select customername, accountnumber
from depositor, account
where depositor accountnumber = account.account
number
and account.balance > amount
ENDEXEC
■ Allows programs to construct and submit SQL queries at
run time.
■ Example of the use of dynamic SQL from within a C
program.
char * sqlprog = “update account set balance = balance
* 1.05
where accountnumber = ?”
EXEC SQL prepare dynprog from :sqlprog;
char account [10} = “A101”;
EXEC SQL execute dynprog using :account;
■ The dynamic SQL program contains a ?, which is a place
holder for a value that is provided when the SQL program
is executed.