3cs518ic24 Dbms Minor Unit 3
3cs518ic24 Dbms Minor Unit 3
Basic Structure
Set Operations
Aggregate Functions
Null Values
Nested Subqueries
Derived Relations
Views
Modification of the Database
Joined Relations
Data Definition Language
Embedded SQL, ODBC and JDBC
branch-name(loan)
NOTE: SQL does not permit the ‘-’ character in names,
Use, e.g., branch_name instead of branch-name in a real
implementation.
We use ‘-’ since it looks nicer!
NOTE: SQL names are case insensitive, i.e. you can use capital
or small letters.
You may wish to use upper case where-ever we use bold font.
select *
from loan
The select clause can contain arithmetic expressions involving
the operation, +, –, , and /, and operating on constants or
attributes of tuples.
The query:
Find the name, loan number and loan amount of all customers
having a loan at the Perryridge branch.
Find the name, loan number and loan amount of all customers;
rename the column name loan-number as loan-id.
Find all customers who have a loan at the bank but do not have
select branch-name
from branch
where assets > some
(select assets
from branch
where branch-city = ‘Brooklyn’)
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
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
select branch-name
from branch
where assets > all
(select assets
from branch
where branch-city = ‘Brooklyn’)
where:
<query expression> is any legal expression
The view name is represented by v
select customer-name
from all-customer
where branch-name = ‘Perryridge’
with max-balance(value) as
select max (balance)
from account
select account-number
from account, max-balance
where account.balance = max-balance.value
update account
set balance = balance 1.05
where balance 10000
The order is important
Can be done better using the case statement (next slide)
update account
set balance = case
when balance <= 10000 then balance *1.05
else balance * 1.06
end
Motivating example
Transfer of money from one account to another involves two steps:
deduct from one account and credit to another
If one steps succeeds and the other fails, database is in an inconsistent state
Therefore, either both steps should succeed or neither should
If any step of a transaction fails, all work done by the transaction can be
undone by rollback work.
Rollback of incomplete transactions is done automatically, in case of
system failures
customer-name loan-number
Jones L-170
Smith L-230
Hayes L-155
Note: borrower information missing for L-260 and loan information missing for L-155
select customer-name
from (depositor natural full outer join borrower)
where account-number is null or loan-number is null