Database Lab Questions On Views
Database Lab Questions On Views
For the
borrower table foreign key add an on delete cascade constraint. For Depositor table add an
on delete set null constraint.
Loan (loan_no, bname, amount)
create table Loan2(Loan_no varchar(10) constraint pk_lno primary key, bname varchar(50),amount
number(8,2));
UR11CS010
ASHISH NELSON
Account
Loan_no
L-11
L-14
L-15
L-16
bname
Bname
Downtown
Perryridge
Perryridge
Redwood
amount
900
1500
1500
1300
Accno
A-11
A-14
A-15
A-16
Downtown
Perryridge
Perryridge
Redwood
900
1500
1500
1300
Borrower
Depositor
Cname
King
Sharon
Ruth
Christos
Accno
Loan_no
L-11
L-14
L-15
L-16
Cname
King
Mary
Mercy
Christos
A-11
A-14
A-15
A-16
King
A-2
UR11CS010
ASHISH NELSON
King
A-89
Customer
cname
King
Mary
Sharon
Ruth
Street
12, 2nd
4th street
R .k
Harry
city
Downtown
Perryridge
Perryridge
Redwood
christos
Downstreet
Perryridge
Mercy
Blueway
Redwood
4. Create a view which contains names of all the depositors. Insert a new row to this view.
Is the view updatable? If Yes. Is the updation reflects in the base table?
create view cnames as(select customer_name from depositor3);
View-cnames
Base table-depositor3
UR11CS010
ASHISH NELSON
5. Delete a row from the above view. What happened to the view and the base table?
delete from cnames where customer_name ='aleena'
View-cnames
Base table-depositor3
6. Create a view which contains the accno, bname and balance of all accounts in
perryridge branch. Insert a new row to the view with branch name DownTown. Is the
view Updatable? Is the updation reflects in the base table?
create view cnames2 as (select account_no,bname,balance from account2 where
bname='perryridge');
7. Create a view with check option which contains the accno, bname and balance of all
accounts in perryridge branch. Insert a new row to the view with branch name
DownTown. Is the view Updatable? If not, why?
create view chk as select account_no,bname,balance from account2 where bname='perryridge'
WITH CHECK OPTION CONSTRAINT chk
UR11CS010
ASHISH NELSON
8. Create view which contains all the details of customer who have account. Is the view
updatable? If not, Why?
create view custview as(select * from account2 join depositor3 using (account_no));
9. From the above create view, create a new view which includes only details of customer
having balance > 2000.
create view viewy as (select * from custview where balance >2000)
10. Create view which includes all customers who have at most one account at the
perryridge branch.
create view inc as(select customer_name,bname from depositor3 join account2 using(account_no)
where bname='perryridge');
11. Create view which includes all customer who have both an account and a loan at the
perryridge branch.
create view all_cust as((select customer_name,bname from depositor3 join account2
using(account_no) where bname='perryridge') union
(select customer_name,bname from borrower2 join loan2 using(loan_no) where
bname='perryridge'));
12. Create a view which includes the branch name and the average account balance of those
branches where the average account balance is greater than 1200.
create view branch_view1(branch_name,average_balance) as (select bname, avg(balance) from
account2 group by bname having avg(balance) > 1200);
13. Create a view which includes the average balance for each customer who lives in
Downtown and has at least three accounts.
create view bal_view1 (customer_name,average_balance) as( select customer_name,avg(balance)
from account2 join depositor3 using(account_no) where bname='Downtown' group by
customer_name having count(account_no)>=3);
UR11CS010
ASHISH NELSON
15. Find the account number which has the maximum balance. [Use with clause]
with max_bal as (select max(balance) as mb from account2)
select account_no from account2,max_bal
where account2.balance=max_bal.mb
16. Find all the branches where the total account deposit is greater than the average of the
total account deposit at all branches. [use with clause]
with branc as (select bname,sum(balance)as mb from account2 group by bname),branc_avg as
(select bname,avg(balance)as av from account2 group by bname)
select branc.bname from branc,branc_avg where branc.mb>branc_avg.av;
17. Find the maximum across all branches of the total balance at each branch.
with tot_bal as (select bname as branch,sum(balance) as amount from account2 group by bname )
select branch from tot_bal where amount=(select max(amount) from tot_bal);
UR11CS010
ASHISH NELSON