98
Chapter 4 SQL 4.1 Basic Structure 4.2 Set Operations 4.3 Aggregate Functions 4.4 Null Values 4.5 Nested Subqueries 4.6 Views 4.7 Complex Queries 4.8 Recursion in SQL 4.9 Modification of the Database 4.10 Joined Relations 4.11 Data-Definition Language 4.12 Embedded SQL 4.13 Exercises

Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

Embed Size (px)

Citation preview

Page 1: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 2: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 3: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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))

Page 4: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 5: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 6: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 7: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 8: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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)

Page 9: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 10: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

select distinct customer-name, borrower.loan-number,amount

from borrower, loan

where borrower.loan-number=loan.loan-number

Loan-schema=( branch-name, loan-number ,amount)

Borrower-schema=(customer-name, loan-number)

Page 11: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.1.3 The from Clause

loan-number branch-name amount

L-11

L-14

L-15

Perryridge

Perryridge

Mianus

900

1500

2000

loanborrower

loan-numbercustomer-name

Hayes

Johnson

Smith

L-10

L-14

L-15

loan.loan-number branch-name amount

L-14

L-15

Perryridge

Mianus

1500

2000

borrower.loan-numbercustomer-name

Johnson

Smith

L-14

L-15

where borrower.loan-number=loan.loan-number

select distinct customer-name, borrower.loan-number,amount

Page 12: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.1.3 The from Clause

② Find the names and loan numbers of all customers who have a loan at the Perryridge branch.

select distinct customer-name, borrower.loan-number

from borrower, loan

where borrower.loan-number=loan.loan-number and

branch-name=“Perryridge”

Loan-schema=( branch-name, loan-number ,amount)

Borrower-schema=(customer-name, loan-number)

Page 13: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.1.4 The Rename Operation

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

Page 14: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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”

Page 15: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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 >

Page 16: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

>

Page 17: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 18: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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%”

Customer-schema=(customer-name,customer-street,customer-city)

customer-name customer-street customer-city

Adams

Brooks

Curry

Hayes

Spmain

main

Nmainh

Park

Pittsfield

Brooklyn

Rye

Stamford

customer-name

Adams

Brooks

Curry

Page 19: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.1.6 String Operations

⑶ 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”.

Page 20: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

select distinct customer-name

from borrower, loan

where borrower.loan-number=loan.loan-number and

branch-name=“Perryridge”

order by customer-name (asc)

Loan-schema=( branch-name, loan-number ,amount) Borrower-schema=(customer-name, loan-number)

Page 21: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.1.7 Ordering the Display of Tuples

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

Page 22: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 23: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 24: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 25: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 26: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 27: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

group by branch-name

Depositor-schema=(customer-name, account-number) Account-schema=(branch-name, account-number, balance)

Page 28: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.3 Aggregate Functions

3. having clause Example: ① we might be interested in only those bran

ches where the average account balances is more than $750.

branch-name ebalance

Redwood 800

ebalance> $750

account-number branch-name balanceL-16

L-18

L-21

L-25

L-33

Perryridge

Redwood

Perryridge

Redwood

Perryridge

600

700

800

900

700

branch-name ebalance

Perryridge

Redwood

700

800

Page 29: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

select branch-name, avg(balance)

from account

group by branch-name

having avg(balance)>750

4.3 Aggregate Functions

ebalance> $750 branch-name ebalance

Redwood 800

branch-name ebalance

Perryridge

Redwood

700

800

Page 30: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

customer-name customer-street customer-city

Adams

Brooks

Curry

Hayes

Spmain

main

Nmainh

Park

Pittsfield

Brooklyn

Rye

Stamford

4.3 Aggregate Functions

4. At times, we wish to treat the entire relation as a single group, in such cases, we do not use a group by clause.

Example: ① Find the average balance for all account

select avg(balance)

from account

select count( ) ﹡ from customer

5. count ( ) ﹡Example:① Find the

number of tuples in the customer relation

4

Page 31: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.3 Aggregate Functions

6. A where clause and a having clause appear in the same query

Example:① Find the average balance for each customer who lives in Harrison and has at least three accounts.

Depositor-schema=(customer-name, account-number)

Account-schema=(branch-name, account-number, balance)

Customer -schema=(customer-name, customer-street,customer-city) ① innerjion depositor, account, customer

② customer-city=“Harrison”

③ group by depositor.customer-name

(where)

(having) ④ count(distinct depositor.account-number)>=3

Page 32: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

select depositor.customer-name, avg(balance)

from depositor, account, customer

where depositor.account-number=account.account-number and

depositor.customer-name=customer.customer-name and customer-city=“Harrison” group by depositor.customer-name

having count(distinct depositor.account-number)>=3

4.3 Aggregate Functions

① Find the average balance for each customer who lives in Harrison. (where)

② Find the average balance for each customer who has at least three accounts. (having)

Page 33: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.4 Null Valus

SQL allows the use of null values to indicate absence of information about the value of an attribute.

We use the special keyword null in a predicate to test for a null value.

Example:

① Find all loan numbers that appear in the loan relation with null value for amount.

select loan-number

from loan

where amount is null

select sum(amount)

from loan

Page 34: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

A subquery is a select-from-where expression that is nested within another query.

1. Set Membership in and not in Examples:

① Find all customers who have both a loan and an account at the bank.

select distinct customer-name

from borrower

where customer-name in (select customer-name

from depositor)

Borrower-schema=(customer-name, loan-number)

Depositor-schema=(customer-name, account-number)

Page 35: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

borrower

loan-number

Hayes

Johnson

A-102

A-101

customer-name

depositor

customer-name account-number

Hayes

Curry

L-16

L-93

4.5 Nested Subqueries

① select customer-name

from depositor

customer-name

Hayes

Curry

Hayes

Johnson

② select distinct customer-name

from borrower

where customer-name in

in

not in

③ select customer-name Hayes

Page 36: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

② 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, accountwhere depositor.account-number=account.account-number)

(“perryridge” ,customer-name)

Page 37: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

account

account-number branch-name balance

L-16

L-93

L-98

Perryridge

Redwood

Mianus

500

700

900depositor

customer-name account-number

Hayes

Curry

Smith

L-16

L-93

L-95

(select branch-name,customer-name

from depositor, accountwhere depositor.account-number=account.account-number)

branch-name customer-name

Perryridge

Redwood

Hayes

Curry

Page 38: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

loan-number branch-name amount

L-11

L-14

L-15

Perryridge

Redwood

Mianus

900

1500

800loan

customer-name loan-number

Hayes

Smith

Curry

L-11

L-14

L-16borrower

(branch-name,customer-name) (Perryridge, Hayes)

4.5 Nested Subqueries

branch-name customer-name

Perryridge

Redwood

Hayes

Curry

Hayes have a loan at the Perryridge branchin

Hayes and Curry have a account

Hayes have both an account and a loan at the perryridge branch

Page 39: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

③ Find all customer who do have a loan at the bank but do not have an account at the bank.

select distinct customer-name

from borrower

where customer-name not in

(select customer-name

from depositor)

Page 40: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

④ Select the names of customers who have a loan at the bank, and whose names are neither “smith” nor “jones”

select distinct customer-name

from borrower

where customer-name not in (“smith”, “jones”)

Page 41: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

2. Test for Empty Relations exist and not exist

Examples:

① Find all customers who have both an account and a loan at the bank

select distinct customer-name from borrowerwhere exists (select *

from depositorWhere depositor.customer-name=borrower.customer-name)

Borrower-schema=(customer-name, loan-number)

Depositor-schema=(customer-name, account-number)

Page 42: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

borrower

loan-number

Hayes

Johnson

A-102

A-101

customer-name

④ select customer-name form (Hayes,A-102)

① borrower(Hayes,A-102) (Johnson,A-101)

③ where exists(‘Hayes’, ‘L-16’) True

② (select *from depositorWhere depositor.customer-name=‘Hayes’)

customer-name

Hayes

depositor

customer-name account-number

Hayes

Curry

L-16

L-93

Page 43: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

② Find all customers who have an account at all the branches located in brooklyn.

all the branches in brooklyn

Brighton

Downtown

Smith brighton

downtown

curry brighton

Redwood

relation A contains relation B

not exists(B except A)

smith

brighton downtown

A

contain

Brighton

DowntownB

except

Null not exists true

smith

Page 44: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

(select branch-name

from branch

where branch-city=“brooklyn”)

B

(select customer-name ,branch-name

from depositor as T, account as R

where T.account-number=R.account-number)

Depositor-schema=(customer-name, account-number) Account-schema=(branch-name, account-number, balance)

A

not exists

(

)

except

Brighton

DowntownB

smith

Brighton Downtown

A

Branch-schema=(branch-name, branch-city,assets)

Page 45: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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))

4.5 Nested Subqueries

Depositor-schema=(customer-name, account-number) Account-schema=(branch-name, account-number, balance)

Page 46: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

③ find the names of all students who have not chosen all courses.(who choose all courses?)

S-schema=(sno,sname,ssex,sage) PK =sno

all the courses number

C-10

C-11

Smith c-11

curry c-11

c-10

not contain

C-schema=(cno,cname,teacher) PK=cnoSC-schema=(sno,cno,score) PK =(sno,cno)

Smith

Page 47: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

select sname

from swhere exists

(select *from cwhere not exists

(select *

from sc

where sc.cno=c.cno and sc.sno=s.sno))

S-schema=(sno,sname,ssex,sage) PK =sno

C-schema=(cno,cname,teacher) PK=cnoSC-schema=(sno,cno,score) PK =(sno,cno)

4.5 Nested Subqueries

falsetrue

falsetrue

Page 48: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

④ find the names of all students who have chosen the courses including one course which the NO.1 student has chosen at lease.

S-schema=(sno,sname,ssex,sage) PK =sno

NO.1

C-10

C-11

Smith c-11

Curry c-10

c-11

contain

C-schema=(cno,cname,teacher) PK=cnoSC-schema=(sno,cno,score) PK =(sno,cno)

Curry

Smith

Page 49: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

select snamefrom swhere exists

(select *

from sc as sx

where sno=“NO.1”

and exists (select * from sc as sy where sy.sno=s.sno and sx.cno=sy.cno))

4.5 Nested Subqueries

S-schema=(sno,sname,ssex,sage) PK =sno

C-schema=(cno,cname,teacher) PK=cnoSC-schema=(sno,cno,score) PK =(sno,cno)

falsetrue

falsetrue

sno cno scoresx

No.1

No.1

No.2

No.2

No.3

C-10

C-11

C-10

C-11

C-11

90

89

78

86

89

sno cno score

No.1

No.1

No.2

No.2

No.3

C-10

C-11

C-10

C-11

C-11

90

89

78

86

89

sy

Page 50: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

select snamefrom swhere not exists

(select *

from sc as sx

where sno=“NO.1”

and not exists (select * from sc as sy where sy.sno=s.sno and sx.cno=sy.cno))

4.5 Nested Subqueries

⑤ Find the names of all students who choose the courses include all courses which NO.1 students has chosen at least.

false

true

true

false

sno cno scoresx

No.1

No.1

No.2

No.2

No.3

C-10

C-11

C-10

C-11

C-11

90

89

78

86

89

sno cno score

No.1

No.1

No.2

No.2

No.3

C-10

C-11

C-10

C-11

C-11

90

89

78

86

89

sy

Page 51: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

⑥ Find the names of all students who have chosen the courses not including all courses which NO.1 students has chosen.

exists not exists

Page 52: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

3. Set comparison > some ==“greater than at least one” ( any ) Examples: ① Find the names of all branches that have assets great

er than those of at least one branch located in brooklyn.

select distinct T.branch-namefrom branch as T, branch as S

where T.assets > S.assets and S.branch-city=“Brooklyn”

Branch-schema=(branch-name, branch-city,assets)

Page 53: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

select branch-name

from branch

where assets > some(select assets

from branch

where branch-city=“brooklyn”)

② Find the names of all branches that have assets greater than that of each branch in brooklyn.

>some >all

=some in

<>all not in

>1500

1600

1900

assets(brooklyn)

1550 (Mianus)

1350 (Bighton)

……. (……….)

?

>some >min()

Page 54: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

③ Find the branch that has the highest average balance

select branch-name

from accountgroup by branch-name

having avg(balance) > =all (select avg(balance) from account

group by branch-name)

1900 (Mianus)

1350 (Bighton)

……. (……….)

1500

1600

1900

avg(balance)all branchs

?>= >=all max()avg(balance)

Account-schema=(account-number,branch-name,balance)

Page 55: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

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”)

Page 56: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.5 Nested Subqueries

② Find all customers who have at least two accounts at the perryridge branch.

unique not unique

Page 57: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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)

union(select branch-name, customer-namefrom borrower, loanwhere borrower.loan-number=loan.loan-number)

Page 58: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.6 views

② The attribute names of a view can be specified

create view branch-total-loan(branch-name,total-loan) as select branch-name, sum(amount)from loangroup by branch-name

③ Using the view all-customer, we can find all customersof the perryridge branch.

explicitly as follows:

select customer-namefrom all-customerwhere branch-name=“perryridge”

Page 59: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.7 Complex Queries

Derived Relations: Result relation should be given a name, and the attributes can be renames.

Examples: ① Find the average account balance of those branches

where the average account balance is greater than$1200

select branch-name, avg-balancefrom (select branch-name, avg(balance)

group by branch-name)

as result(branch-name, avg-balance)

from account

where avg-balance > 1200

Account-schema=(branch-name, account-number, balance)

Page 60: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.7 Complex Queries

② Find the branch name which has the largest sum of account balance in the bank.

select max(tot-balance)

from (select branch-name, sum(balance)

group by branch-name) as branch-total(branch-name, tot-balance)

from account

Page 61: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.7 Complex Queries

The with clause Examples: ① Find the account number which has the

largest account balance in the bank.

with max-balance(value) as select max(balance)

where account.balance=max-balance.value

from account select account-number

from account,max-balance

② Find all branches where the total account deposits less than the average of the total account deposits at all branches.

Page 62: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.7 Complex Queries

with branch-total(branch-name,value) as

select branch-name,sum(balance)

where branch-total.value>=branch-total-avg.value

from account

select branch-name

from branch-total,branch-total-avg

group by branch-name

with branch-total-avg(value) as

select avg(value)

from branch-total

Page 63: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.8 Recursion in SQL

With recursive clause:

Example: ① suppose the relation manager has a attributes emp and mgr. We can find every pair(X,Y)such that X is directly or indirectly managed by Y.

mgr emp

Alon Jak

Jak Kat

Met Fin

Fin Lat

Lat Gut

with recursive empl(emp,mgr) as ( select emp, mgr

from manager

select *from empl

unionselect emp,empl.mgrfrom manager,emplwhere manager.mgr=empl.emp)

Page 64: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.9 Modification of the Database

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

Page 65: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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)

Page 66: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 67: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 68: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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”

Page 69: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 70: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 71: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 72: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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:

Page 73: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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 inner join borrower on loan.loan-number=borrower.loan-numberas lb ( branch, loan-number, amount, cust, cust-loan-num)

Page 74: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.10 Joined Relations

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

Page 75: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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:

Page 76: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 77: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 78: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 79: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 80: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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)

Page 81: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 82: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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))

Page 83: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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 )

Page 84: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 85: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 86: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 87: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 88: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 89: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

Page 90: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 91: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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

ⅰEXEC SQL OPEN<cursor name>

ⅱEXEC SQL FETCH<cursor name>INTO <host variable reference commalist>

ⅲEXEC SQL CLOSE<cursor name>

Page 92: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.12 Embedded SQL

Example:

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.

S={SNO, SNAME, STATUS, CITY}

J={JNO, JNAME, CITY} SPJ={JNO,SNO,PNO,QUENTITY}

Page 93: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.12 Embedded SQL

First we define two cursors, CS and CJ.

EXEC SQL DECLARE CS SURSOR FORSELECT SNO, SNAME, STATUS, CITYFROM SORDER BY SNO;

# define no_more_tuples!(strcmp(SQLSTATE,”02000”))void pinfo(){

EXEC SQL BEGIN DECLARE SECTION;int SNO, JNO;

char SNAME[20], SCITY[50], STATUS[12], JNAME[20];

char JCITY[50], SQLSTATE[6];

EXEC SQL END DECLARE SECTION;

Page 94: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

4.12 Embedded SQL

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);

Page 95: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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;

}

}

Page 96: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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;

Page 97: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.

Page 98: Chapter 4 SQL 4.1 Basic StructureBasic Structure 4.2 Set OperationsSet Operations 4.3 Aggregate FunctionsAggregate Functions 4.4 Null ValuesNull Values

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.