This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Chapter 4 SQL
4.1 Basic Structure4.2 Set Operations4.3 Aggregate Functions4.4 Null Values4.5 Nested Subqueries4.6 Views 4.7 Complex Queries4.8 Recursion in SQL4.9 Modification of the Database 4.10 Joined Relations4.11 Data-Definition Language4.12 Embedded SQL4.13 Exercises
4.1 Basic Structure
The Basic Structure of an SQL expression consists of three clauses: select, from and where:
① The select clause corresponds to the projection operation of the relational algebra. It is used to list the attributes desired in the result of a query.
② The from clause corresponds to the Cartesian-product operation of the relational algebra. It lists the relations to be scanned in the evaluation of the expression.
③ The where clause corresponds to the selection predicate of the relational algebra. It consists of a predicate involving attributes of the relations that appear in the from clause.
4.1 Basic Structure
A typical SQL query has the form select A1,A2,……An
from r1,r2……rm
where p
Each Ai represents an attribute and each ri a relation. P is a predicate. The query is equivalent to the relational-algebra expression.
∏ A1,A2,……An (p(r1r2…… rm))
4.1.1 The Select Clause
SQL allows duplicates in relations as well as in the results of SQL expressions.
Examples: ① Find the names of all branches in the loan relation
select branch-name from loan
Loan-schema=( branch-name, loan-number ,amount)
loan-number branch-name amount
L-11
L-14
L-15
Perryridge
Perryridge
Mianus
900
1500
2000loan
branch-name
Perryridge
Perryridge
Mianus
4.1.1 The Select Clause
② If you want to force the elimination of duplicates, you can insert the keyword distinct after select.
select distinct branch-namefrom loan
branch-name
Perryridge
Mianus
loan-number branch-name amount
L-11
L-14
L-15
Perryridge
Perryridge
Mianus
900
1500
2000loan
4.1.1 The Select Clause
③ The asterisk symbol “” can be used to denote “all attributes”.
select from loan
Loan-schema=(branch-name, loan-number ,amount)
select branch-name,loan-number,amountfrom loan
loan-number branch-name amount
L-11
L-14
L-15
Perryridge
Perryridge
Mianus
900
1500
2000
loanloan-number branch-name amount
L-11
L-14
L-15
Perryridge
Perryridge
Mianus
900
1500
2000
4.1.1 The Select Clause
④ The select clause can also contain arithmetic expressions involving the operators,+, -, ,and /, and operating on constants or attributes of tuples.
select branch-name,loan-number,amount 10from loan
loan-number branch-name amount
L-11
L-14
L-15
Perryridge
Perryridge
Mianus
900
1500
2000
loan loanbranch-name loan-number amount
L-11
L-14
L-15
Perryridge
Perryridge
Mianus
9000
15000
20000
4.1.2 The Where Clause
SQL uses the logical connectives and, or, and not. The operands of the logical connectives can be expressions involving the comparison operators <, <=, >, >=, =, and <>.
from loanwhere branch-name=“Perryridge” and amount>1200
select loan-number
Examples: ① Find all loan numbers for loans made at the Perryride branch with loan amounts greater that $1200.
Loan-schema=( branch-name, loan-number ,amount)
4.1.2 The Where Clause
Between:② Find the loan number of those loans with loan amounts between $90,000 and $100,000.
select loan-numberfrom loan where amount between 90000 and 100000
Similarly, we can use the not between comparison operator.
Loan-schema=( branch-name, loan-number ,amount)
where amount >= 90000 and amount <= 100000
4.1.3 The from Clause
The from clause by itself defines a Cartesian product of the relations in the clause. Examples:① For all customers who have a loan form the bank, find their names and loan number and loan amount.
Problems:①Two relations in the from clause may have attributes with the same name, in which case a attribute name is duplicated in the result.
②If we used an arithmetic expression in the select clause the resultant attribute does not have a name.
③ Even if a attribute name can be derived from the base relations as in the preceding example, we may want to change the attribute name in the result.
Example: select branch-name, amount 100
Example: loan-number amountloan
loan-number amountloan d
namount
4.1.4 The Rename Operation
SQL provides a way of renaming the attributes of a result relation.
Example:
① If we want the attribute name loan-number to be replaced with the name loan-id. Then we can write.select distinct customer-name, borrower.loan-number as loan-id
from borrower, loan
where borrower.loan-number=loan.loan-number and
branch-name=“Perryridge”
4.1.5 Tuple Variables
Tuple variables are defined in the from clause via the use of the as clause. Tuple variables are most useful for comparing two tuples in the same relation.
Example: ① Find the names of all branches that have assets greater than at
least one branch located in Brooklyn.
Branch-schema=(branch-name, branch-city,assets)
assets Brooklyn
5000
6200
4900
Brighton 5100
Redwood 6800
…
Rye 4800 >
select distinct T.branch-namefrom branch as T, branch as S
where T.assets > S.assets and S.branch-city=“Brooklyn”
4.1.5 Tuple Variables
branch-name branch-city assetsPownal
Redwood
Brighton
Mianus
Brooklyn
Rye
Brooklyn
Palo
5000
4900
6200
5100
branch T
branch-name branch-city assets
branch S
Pownal
Redwood
Brighton
Mianus
Brooklyn
Rye
Brooklyn
Palo
5000
4900
6200
5100
>
4.1.6 String Operations
like: ⑴ Percent(%): The % character matches any substring.
⑵ Underscore( _ ): The _ character matches any character.
Examples:
① “Perry%” matches any string beginning with “Perry”.
② “_ _ _” matches any string of exactly three characters.
4.1.6 String Operations
③ Find the names of all customers whose street address includes the substring “main”
select customer-namefrom customerwhere customer-street like “%main%”
⑶ The escape character is used immediately before a special pattern character to indicate that the special pattern character is to be treated like a normal character.
Examples: using a backslash (\) as the escape character
① like “ab\%cd%” escape “\” matches all string beginning with “ab%cd”
② like “ab\\cd%” escape “\”matches all strings beginning with “ab\cd”.
4.1.7 Ordering the Display of Tuples
Order by clause: desc asc
Examples:
① To list in alphabetic order all customers who have a loan at the Perryridge branch.
Ordering can be performed on multiple attributes② List the entire loan relation in descending order of amount. If several loans have the same amount, we order than in ascending order by loan number.
select ﹡ from loanorder by amount desc, loan-number asc
Loan-schema=( branch-name, loan-number ,amount)
loan-number branch-name amount
L-11
L-15
L-14
Perryridge
Perryridge
Mianus
900
1500
1500
loan-number branch-name amount
L-14
L-15
L-11
Mianus
Perryridge
Perryridge
1500
1500
900
asc
desc
4.2 Set Operations
union, intersect, except : ∪ , ∩, - 1. The Union Operation
Example: ① Find all customers having a loan, an account or both at the bank.
(select customer-name from depositor)
union
(select customer-name from borrower)
Union operation automatically eliminates duplicates. If we want to retain all duplicates, we must write union all in place of union.
customers who have a loan
customers who have an account
4.2 Set Operations
2. The Intersect OperationExample:① Find all customers who have both a loan and an account at the bank
(select customer-name from depositor)
intersect
(select customer-name from borrower)
If we want to retain all duplicates, we must write intersect all in place of intersect.
4.2 Set Operations
3. The Except Operation
Example:① Find all customers who have an account but no loan at the bank
(select customer-name from depositor)except
(select customer-name from borrower)
If we want to retain all duplicates, we must write except all in place of except.
4.3 Aggregate Functions
1. Aggregate functions are functions that take a collection of values as input and return a single value. SQL offers five built-in aggregate functions.
Average: avg Minimum: min Maximum: max Total: sum Count: count Example: ① Find the average account balance at the Perr
yridge branch.
select avg(balance) from accountwhere branch-name=“Perryridge”
account-number branch-name balance
L-16
L-93
L-98
Perryridge
Redwood
Perryridge
500
700
900
700
4.3 Aggregate Functions
2. group by clause. Examples:①Find the average account balance at each branch.
select branch-name, avg(balance) as ebalance from account
group by branch-name
branch-name ebalance
Perryridge
Redwood
700
800
account-number branch-name balanceL-16
L-18
L-21
L-25
L-33
Perryridge
Redwood
Perryridge
Redwood
Perryridge
600
700
800
900
700
4.3 Aggregate Functions
② Find the number of depositor for each branch.
select branch-name, count(distinct customer-name) from depositor,account
where depositor.account-number=account.account-number
4. Test for the Absence of Duplicate Tuplesunique construct
Examples:
① Find all customers who have only one account at the perryridge branch.select T.customer-namefrom depositor as Twhere unique(select R.customer-name
from account,depositor as R
where T.customer-name=R.customer-name andR.account-number=account.account-number andaccount.branch-name=“perryridge”)
4.5 Nested Subqueries
② Find all customers who have at least two accounts at the perryridge branch.
unique not unique
4.6 views
The form of the create view command is:create view v as <query expression>
Examples:
① consider the view consisting of branch names and the names of customers who have either an account or a loan at that branch. Assume that we want this view to be called all-customer.
create view all-customer as (select branch-name, customer-namefrom depositor, accountwhere depositor.account-number=account.account-number)
1. Deletiondelete from r (relation) where p (predicate)
Notice: We can delete only whole tuples; we can’t delete valueson only particular attributes.
① delete all smith’s account recordsExamples:
delete from depositorwhere customer-name=“smith”
② delete all loans with loan amounts between $1300 and $1500
delete from loanwhere amount between 1300 and 1500
4.9 Modification of the Database
③ delete all accounts at every branch located in perryridge.delete from account
where branch-name in (select branch-name
from branchwhere branch-city=“perryridge”)
④ delete the records of all accounts with balances below the average at the bank.
delete from accountwhere balance (select avg(balance)
from account)
4.9 Modification of the Database
2. Insertion To insert data into a relation, we either specify a tuple to be inserted or write a query whose result is a set of tuples to be inserted.
Examples:
① Suppose that we wish to insert the fact that there is an accountA-9732 at the perryridge branch and that is has a balance of $1200
insert into account values(“perryridge”, “A-9732”,1200)
insert into account(account-number,branch-name,balance) values(“A-9732”, “perryridge”,1200)
or
4.9 Modification of the Database
② suppose that we want to provide as a gift for all loan customers of the perryride branch, a new $200 savings account for each loan account they have.let the loan number serve as the account number for the savings account.
insert into account
select branch-name, loan-number, 200
from loan
where branch-name=“perryridge”
We also need to add tuples to the depositor relation.
4.9 Modification of the Database
Insert into depositorselect customer-name, loan-numberfrom borrower, loanwhere borrower.loan-number=loan.number and
branch-name=“perryridge”
Problems:
① insert into account
select *
from accont
② insert into account
values(null, “A-401”, 1200)
select account-number
from account
where branch-name=“perryridge”
4.9 Modification of the Database
3. Updates
In certain situations, we may wish to change a value in a tuple without changing all values in the tuple.
Examples: ① account with balance over $10,000 receive 6 percent interest,
whereas all other receive a percent.update accountset balance=balance*1.06where balance>10000update accountset balance=balance*1.05
where balance<=10000
4.9 Modification of the Database
update account
set balance=balance*1.05
where balance>
(select avg(balance)
form account)
② pay 5 percent interest on accounts whose balance is greater than average.
Case construct:
update account
set balance= casewhen balance<=10000 then balance*1.05esle balance*1.06
end
4.9 Modification of the Database
4. Update of a view
Example:create view branch-loan as
select branch-name, loan-number from loan insert into branch-loanvalues(“perryridge”, “L-307”)
* A modification is permitted through a view only if the view in
question is defined in terms of one relation of the actual relational
database---that is, of the logical-level database.
4.10 Joined Relations
SQL provide various mechanisms for joining relations, including condition joins, natural joins and various forms of outer joins. (from clause)
1. Inner join
branch-name
loan-number
amount
Downtown L-170 3000
Redwood L-230 4000
Perryridge L-260 1700loan
customer-name
Loan-number
Jones L-170
Smith L-230
Hayes L-155borrower
Example:
4.10 Joined Relations
1. Inner joinloan inner join borrower on loan.loan-number=borrower.loan-number
branch-name
loan-number
amount
Downtown L-170 3000
Redwood L-230 4000
customer-name
Loan-number
Jones L-170
Smith L-230
the attributes of the result consist of the attributes of the left-hand-side relation followed by the attributes of the right-hand-side relation.Using as
loan left outer join borrower on loan.loan-number=borrower.loan-number2. Left outer join
Steps: ① compute the inner join ② for every tuple t in the left-hand-side relation loan that did not ma
tch any tuple in the right-hand-side relation borrower in the inner join, a tuple r is added to the result of the join as follows. The attributes of tuple r that are derived from the left-hand-side relation are filled with null values.
branch-name
loan-number
amount
Downtown L-170 3000
Redwood L-230 4000
Perryridge L-260 1700
customer-name
Loan-number
Jones L-170
Smith L-230
null null
4.10 Joined Relations
3. Natural join
branch-name
loan-number
amount
Downtown L-170 3000
Redwood L-230 4000
customer-name
Jones
Smith
loan natural inner join borrower
The attributes loan-number appears only once in the result of the natural join.
Notices:
4.10 Joined Relations
Join type and conditions ① join condition: define which tuples in the two relations match, an
d what attributes are present in the result of the join. (natural; on<predicate>; using<A1,A2……An>)
② join type: define how tuples in each relation that do not match any tuple in the other relation are treated. (inner join, left outer join, right outer join, full outer join)Examples:
① loan natural right outer join borrower
branch-name loan-number
amount
Downtown L-170 3000
Redwood L-230 4000
null L-155 null
customer-name
Jones
Smith
Hayes
4.10 Joined Relations
② loan full outer join borrower using (loan-number)
branch-name
loan-number
amount
Downtown L-170 3000
Redwood L-230 4000
Perryridge L-260 1700
null L-155 null
customer-name
Jones
Smith
null
Hayes
4.10 Joined Relations
③ find all customers who have an account but no loan at the bank.
select d-CNfrom (depositor left join borrower
on depositor.customer-name=borrower.customer-nameas db1(d-CN, account-number, b-CN, loan-number)
where b-CN is null
④ find all customers who have either an account or a loan at the bank.
select customer-namefrom (depositor natural full outer join borrower)
where account-number is null or loan-number is null
4.11 Data-Definition Language
The set of relations in a database must be specified to the system by means of a data definition language(DDL)
① the schema for each relation
② the domain of values associated with each attribute
Specification function:
③ the integrity constraints
④ the set of indices to be maintained for each relation
⑤ the security and authorization information for each relation
⑥ the physical storage structure of each relation on disk
4.11 Data-Definition Language
Domain Types in SQLDomain types:
char(n) varchar(n) int smallint numeric(p,d)real float(n) date time interval
a particular case where it is essential to prohibit null values is in the primary key of a relation schema.
create domain clause
create domain person-name char(20)
4.11 Data-Definition Language
Schema Definition in SQLcreate table command:
create table r (A1D1, A2D2……AnDn,
<integrity-constrant1>,
……
<integrity-constrantk>)
The allowed integrity constraints include:primary key (Aj1,Aj2……Ajm)
check(p)and
nonnull unique
4.11 Data-Definition Language
check clause:Examples:
① create table student
check(degree-level in(“bachelors”, “masters”, primary key (student-id)degree-level char(15) not nullstudent-id char(10) not null(name char(15) not null
① check (branch-name in (select branch-name
“doctorate”)))
from branch))
4.11 Data-Definition Language
Remove a relation:
drop table r
Add attributes to an existing relation:
alter table r add AD
Drop attributes from a relation:
alter table r drop A
( delete from r )
4.12 Embedded SQL
Most SQL products allow SQL statements to be executed both directly and as part of an application program can typically be written in a variety of host language.
The fundamental principle underlying embedded SQL, which we refer to as the dual mode principle, is that any SQL statement that can be used interactively can also be used in an application program.
Points: ① Embedded SQL statement are prefixed by EXEC SQL, to disti
nguish them from statement of the host language, and are terminated by a special terminator symbol.
4.12 Embedded SQL
② An executable SQL statement can appear wherever an executable host statement can appear. Embedded SQL includes some statements that are purely declarative, not executable.
③ SQL statements can include references to host variable; such references must include a colon prefix to distinguish them from SQL column names.
④ All host variable referenced in SQL statements must be declared . Within an embedded SQL declare section, which is delimited by the BEGIN and EDN DECLARE SECTION statement.
4.12 Embedded SQL
⑤ Every program containing embedded SQL statements must
include a host variable called SQLSTATE.
⑥ Host variable must have a data type appropriate to the uses to which they are put.
Host variables and SQL columns can have the same name.
Problem: Operations retrieve may rows, not just one, and host languages are generally not equipped to handle the retrieved of more than one row at a time.
4.12 Embedded SQL
Solution: cursors
A cursor is a special kind of SQL object that applies to embedded SQL only. It consists essentially of a kind of pointer that can be used to run through a collection of rows, pointing to each of the rows in turn and thereby providing address ability to those rows one at a time.
4.12 Embedded SQL
Example:
EXEC SQL DECLARE C SURSOR FOR
select customer-name, customer-city
from depositor, customer, account
where depositor.customer-name=customer.customer-name
and account.account-number=depositor.account-number
and account.balance>:amount
END-EXEC
4.12 Embedded SQL
EXEC SQL OPEN C
Do for all depositor rows accessible via c
END-EXEC
EXEC SQL FETCH C INTO :cn, :cc
END-EXEC
EXEC SQL CLOSE C END-EXEC
4.12 Embedded SQL
① The variable c in the preceding expression is called a cursor for the query. The statement “DECLARE C CURSOR…” defines a cursor called c.
② DECLARECURSOR is a purely declarative statement, not executable. The expression is evaluated when the cursor is opened.
③ The statement “FETCH C INTO…” is then used to retrieve rows one at a time from the resulting set, assigning retrieved values to host variables in accordance with the specifications of the INTO clause in that statement.
4.12 Embedded SQL
④ Since there will be many rows in the result set, the FEICH with normally appear within a loop; the loop will be repeated so long as there are more rows still to come in that result set. On exit from the loop, cursor c is closed.
⑤ There executable statement are provided to operate on cursors: OPEN, FETCH and CLOSE
using the suppliers-parts-projects database, write a program with embedded SQL statement to list all supplier rows, in supplier number order. Each supplier row should be immediately followed in the listing by all project rows for projects supplied by that supplier, in project number order.
Note that there might be some suppliers who supply no projects at all.
EXEC SQL DECLARE CJ SURSOR FORSELECT JNO, JNAME, CITYFROM JWHERE JNO IN
(SELECT SPJ.JNOFROM SPJWHERE SPJ.SNO=:SNO)
ORDER BY JNO;EXEC SQL OPEN CS;
while(1){
EXEC SQL TETCH CS INTO :SNO, :SNAME, :STATUS,:SCITY;
if(no_more_tuples) break;
printf(“%d,%c,%c,%c”,SNO,SNAME,STATUS,SCITY);
4.12 Embedded SQL
EXEC SQL OPEN CJ;
while(1){
EXEC SQL TETCH CJ INTO JNO,JNAME,JCITY;
if(no_more_tuples) break;
printf(“%d,%c,%c”,JNO,JNAME,JCITY);
EXEC SQL CLOSE CJ;}
EXEC SQL CLOSE CS;
}
}
4.12 Embedded SQL
Dynamic SQL The two principal dynamic statements are PREPARE
and EXECUTE.Example:DCL SQLSOURCE CHAR VARYING(65000)
SQLSOURE=“DELETE FROM loan
WHERE amount<300”;
EXEC SQL PREPARE SQLPREPPED FROM: SQLSOURCE;
EXEC SQL ECECUTE SQLPREPPED;
Exercises:
① Relational Model:
Relation S(S#, SNAME, CITY) KEY=S#
Relation P(P#, PNAME, COLOR, WEIGHT) KEY=P#
Relation J(J#, JNAME, CITY) KEY=J#
Relation SPJ(S#, P#, J#, QTY) KEY=S# P# J#
Consider the relational model, for each of the following queries, given an expression in the relational algebra, the tuple-relational-calculus, the domain-relational-calculus and the SQL:
a. Find the suppliers’ numbers s# of all suppliers who offer the parts to both projects J1 and J2.
b. Find the suppliers’ numbers s# of all suppliers who offer the parts to any projects of either ShangHai or BeiJing.
Exercises:
c. Find the suppliers’ numbers s# of all suppliers of ShangHai who offer the parts to the projects of their own places.
d. Find the suppliers’ numbers s# of all suppliers of BeiJing who never offer the red parts.
e. Find the project numbers J# which use the parts offered by S1 at least.
f. Find the names of all suppliers who offer all parts.
g. Find the suppliers’ numbers s# of all suppliers who offer the all parts that S1 offered.