0% found this document useful (0 votes)
185 views

Database Lab Questions On Views

The document describes creating database tables with primary and foreign keys, inserting sample data, creating views on the tables, and performing operations like updating, deleting from views and base tables. It demonstrates how on delete cascade and on delete set null constraints work. Various views are created on single and joined tables with different conditions and operations are performed on the views.

Uploaded by

Hrisav Bhowmick
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
185 views

Database Lab Questions On Views

The document describes creating database tables with primary and foreign keys, inserting sample data, creating views on the tables, and performing operations like updating, deleting from views and base tables. It demonstrates how on delete cascade and on delete set null constraints work. Various views are created on single and joined tables with different conditions and operations are performed on the views.

Uploaded by

Hrisav Bhowmick
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Create the following tables with the primary key and foreign key constraint.

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

Borrower (customer_name, loan_number)


create table borrower2(customer_name varchar(50), loan_no varchar(10));
alter table borrower2 add constraint fk_ln foreign key(Loan_no) references Loan2(Loan_no) on delete
cascade;

Depositor (customer_name, account_number)


create table depositor3(customer_name varchar(50), account_no varchar(10));
alter table depositor3 add constraint fk_an foreign key(account_no) references account2(account_no) on
delete set null;

Account (accno, bname, balance)


create table account2(account_no varchar(10) constraint pk_ano primary key, bname varchar(50), balance
number(8,2))

UR11CS010
ASHISH NELSON

Customer (customer_name, street, city)


create table customer2(customer_name varchar(50),street varchar(50),city varchar(50))

2. Insert the following values into the above table


Loan

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

insert into customer2 values('king','12,2nd','downtown');


insert into customer2 values('mary','4th street','perryridge');
insert into customer2 values('sharon','r.k','perryridge');
insert into customer2 values('ruth','harry','redwood');
insert into customer2 values('christos','downstreet','perryridge');
insert into customer2 values('mercy','blueway','redwood');
3. Delete a row from the customer table and see how the on delete cascade and on delete
set null works.

delete from loan2 where loan_no='L-16';


loan relation
borrower relation

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

insert into cnames values('aleena');


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

insert into cnames2 values('A-12','downtown',900)

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

14. Create a view which stores number of depositors at each branch.


create view depo_no(branch_name,No_of_depositors) as (select bname,count(account_no) from
account2 join depositor3 using(account_no) group by bname);

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

18. Drop the last created view.


drop view depo_no;

UR11CS010
ASHISH NELSON

You might also like