Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS Lab Quetsions With Answer

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

1

CS2258 Database Management Systems Lab (SET I)


1. Create Tables as follows by choosing appropriate data type and
set the necessary primary and foreign key constraints:
Product (Prodid, Prodesc, Price, Stock)
Purchase (Purid, Proid, qty, supname)
Sales (Saleid, Proid, qty, custname)
a. Update the Stock of all products by 10.
Query: update product set stock=stock+10;
b. Display products whose stock is <15 and order it by stock.
Query: select prodesc from product where stock<15 order by stock;
c. Display list of product details (Prodid, Prodesc, Price) supplied by a
particular supplier(ABC).
Query: select prodid,prodesc,price from product where prodid in(select proid from
purchase where supname=ABC);
d. Product ids and Sum of quantity purchased
Query: select sum(qty),proid from purchase group by proid;
e. Product details (Prodid, Prodesc, Price) of products which are purchased as
well as sold.
Query: select prodid,prodesc,price from product where prodid in (select proid from
purchase intersect select proid from sales);
f. Create a procedure which accepts a prodid and displays all the sales and
purchase records of it.
Query: CREATE OR REPLACE PROCEDURE disp(pID IN sales.Proid%type)
2 IS
3 CURSOR emp_cur is
4 Select * from sales where Proid = pID;
5 emp_rec emp_cur%rowtype;
6 BEGIN
7 FOR emp_rec in emp_cur
8 LOOP
2

9 dbms_output.put_line(emp_rec.Saleid ||' '|| emp_rec.Proid || || emp_rec.qty || ||


emp_rec.custname);
10 END LOOP;
11 END;
12 /
Query: set serveroutput on
Query: EXECUTE disp(2);

2. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Product (Prodid, Prodesc, Price, Stock)
Purchase (Purid, Proid, qty)
a. Add a column customer name in Purchase table.
Query: alter table purchase add column(custname varchar(15);
b. Productids which are not at all purchased. List of product details (Prodid,
Prodesc, Price) purchased by a particular customer. (MATHEW).
Query: select prodid, prodesc, price from product where prodid not in (select proid from
purchase where custname=MATHEW);
c. The number of customers who made the purchases
Query: select distinct(custname) from purchase;
d. List of the productids and total quantity purchased
Query: select sum(qty) ,proid from purchase group by proid;
e. Product ids of the products which are purchased more than 5 times
Query: select count(proid),proid from purchase group by proid having count(proid)>2;
f. Create a procedure to print the Prodid and qty when the Purid is given as the
parameter.
Query: CREATE OR REPLACE PROCEDURE disp(pID IN purchase.Proid%type)
2 IS
3 CURSOR emp_cur is
4 Select Proid, qty from purchase where Proid = pID;
5 emp_rec emp_cur%rowtype;
3

6 BEGIN
7 FOR emp_rec in emp_cur
8 LOOP
9 dbms_output.put_line(emp_rec.proid ||' '|| emp_rec.qty);
10 END LOOP;
11 END;
12 /
Query: set serveroutput on
Query: EXECUTE disp(2);

3. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Customer (Custid, Custname, Age, phno)
Loan (Loanid, Amount, Custid, Emi)
a. Customerids who have not taken Loan
Query: select custid from customer where custid not in (select custid from loan);
b. Loan details (Loanid, Amount, Custid ) of Loan taken by a particular
customer(JOHN)
Query: select loanid, amount, custid from loan where custid in (select custid from
customer where custname=JOHN);
c. The Total number of Loans aviailed
Query: select count(*) from loan;
d. List of the customerids and total Loan amount taken
Query: select sum(amount),custid from loan group by custid;
e. Customer ids who have taken less than 2 Loans
Query: select custid, count(custid) from loan group by custid havning count(custid)>2;
f. Create a procedure to print the Amount and Custid when the Loanid is given
as the parameter.
Query: CREATE OR REPLACE PROCEDURE disp(pID IN Loan.Loanid%type)
2 IS
3 CURSOR emp_cur is
4

4 Select custid, amount from Loan where Loanid = pID;


5 emp_rec emp_cur%rowtype;
6 BEGIN
7 FOR emp_rec in emp_cur
8 LOOP
9 dbms_output.put_line(emp_rec.custid ||' '|| emp_rec.amount);
10 END LOOP;
11 END;
12 /
Query: set serveroutput on
Query: EXECUTE disp(2);

4. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Customer (Custid, Custname, Age, phno)
HLoan (HLoanid, Amount, Custid)
VLoan (VLoanid, Amount, Custid)
a. Update the HLoan amount by reducing 5000 rupees which is a special
discount offer.
Query: update HLoan set amount=amount-5000;
b. Number of VLoan taken by a particular customer id.
Query: select count(*) from VLoan group by custid having custid=2;
c. Customer details (Custid, Custname, Age, phno) who have taken HLoan
Query: select Custid, Custname, Age, phno from customer where custid in (select custid
from HLoan);
d. List of the customerids and total VLoan amount taken
Query: select sum(amount), custid from VLoan group by custid;
e. Customer details (Custid, Custname, Age, phno) who have taken both Hloan
and VLoan
5

Query: select Custid, Custname, Age, phno from customer where custid in (select custid
from HLoan intersect select custid from VLoan);

f. Create a procedure to print the Amount and HLoanid when the Custid is given
as the parameter.
Query: CREATE OR REPLACE PROCEDURE disp(pID IN HLoan.custid%type)
2 IS
3 CURSOR emp_cur is
4 Select HLoanid, amount from HLoan where custid = pID;
5 emp_rec emp_cur%rowtype;
6 BEGIN
7 FOR emp_rec in emp_cur
8 LOOP
9 dbms_output.put_line(emp_rec.HLoanid ||' '|| emp_rec.amount);
10 END LOOP;
11 END;
12 /
Query: set serveroutput on
Query: EXECUTE disp(2);

5. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Customer (Custid, Custname, Addr, phno,panno)
Loan (Loanid, Amount, Interest,Custid)
Account (Accd, Accbal, Custid)
a. Add a column CUSDOB in customer table.
Query: alter table customer add column(custdob date);
b. Select the customers whose name endswith SINGH and order by custid
Query: select custname, custid from customer where custname like %SINGH order by
custid;
6

c. Display the customerids and sum of his account balances


Query: select custid, sum(accbal) from account group by custid;

d. Display the accounts of customerids C01,C02,C03


Query: select * from account where custid in(C01,C02,C03);
e. Select the customername,id of customers whose number of accounts is
greater than
Query: select custid, custname from customer where custid in (select custid from account
group by account having count(custid)>5);
f. Create a procedure to print the Loan details when Customer name is given as
the parameter.
Query: CREATE OR REPLACE PROCEDURE disp(pID IN customer.custname%type)
2 IS
3 CURSOR emp_cur is
4 Select * from Loan where custid in (select custid from customer where custname=
pID);
5 emp_rec emp_cur%rowtype;
6 BEGIN
7 FOR emp_rec in emp_cur
8 LOOP
9 dbms_output.put_line(emp_rec.Loanid ||' '|| emp_rec.amount || || emp_rec.interest);
10 END LOOP;
11 END;
12 /
Query: set serveroutput on
Query: EXECUTE disp(2);

6. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Product (Prodid, Prodesc, Price, Stock)
Purchase (Purid, Proid, qty, supname)
7

Sales (Saleid, Proid, qty, custname)


a. Display supname when a particularproduct id is given (DAL23).
Query: select supname from purchase where proid=DAL23;

b. List of names who are both supplier as well as customer.


Query: select supname from purchase intersect select custname from sales;
c. Create a Trigger which reduces the stock of Product that is been inserted in
sales and update the stock when purchase is made
Query:
Create trigger t after insert on sales
for each row
begin
update product set stock=stock+1;
end;
/
d. Create a view which displays Product ids and sum of quantity in sales.
Query: Create view v as select sum(qty), proid from sales group by proid;

7. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Product (Prodid, Prodesc, Price, Stock,Reord)
Sales (Salesid, Proid, qty)
a. Display Prodesc of products ofparticular Sales(SA234).
Query: select prodesc from product where prodid in (select proid from sales where
salesid=SA234);
b. Display the amount (price * qty)of Products in each Sales.
Query: select amount as p.price*s.qty from product p, sales s where p.prodid=s.proid;
c. Create a Trigger which reduces the stock of Product that is been inserted in
sales and print if it is out of stock (stock <Reord)
Query:
create trigger t before insert on sales
8

Begin
Update product set stock=stock 10;
End; /
d. Create a view which displays Proid, Prodesc and sum of quantity in sales.
Query: create view v as select sum(qty),proid from sales group by proid;

8. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Customer (Custid, Custname, Age, phno)
Loan (Loanid, Amount, Custid, Emi)
a. Display the total Emi that need to bepaid by customer with a particular
Custname(LEENA)
Query: (select custid, custname from customer where custname=LEENA and custid in
(select sum(emi),custid from loan group by custid);
b. Add a column nol and update the columnvalues with sum of Loans taken by
him
Query: alter table customer add column(nol number(5));
c. Create a Trigger which updates the nol value on insertion and deletion of
Loan for a customer.
Query:
Create trigger u after insert on loan
For each row
Begin
Update customer set nol=nol+500;
End;
/
d. Create a View with Custname and sum of Loan Amount
Query: create view w as select sum(amount),custid from loan group by custid;

9. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
9

Customer (Custid, Custname, Age, phno)


HLoan (HLoanid, Amount, Custid)
VLoan (VLoanid, Amount, Custid)

a. Display Custname name with both HLoan and VLoan


Query: select custname from customer where custid in (select custid from HLoan intersect
select custid from VLoan);
b. List of the customerids and total VLoan amount taken
Query: select sum(amount) from VLoan group by custid;
c. Create a Trigger which displays the HLoan details when values in HLoan is
inserted, do the same for VLoan also.
Query:
Create trigger t after insert on HLoan referencing new row as nrow
For each row
Begin
dbms_output.put_line(nrow.HLoanid ||' '|| nrow.amount || || nrow.custid);
end;
/
d. Create a view with Custid and Custname with sum of Loan Amount(both
HLoan and VLoan)
Query: create view s as select sum(amount) from HLoan group by custid having
custid=(select custid from HLoan intersect select custid from VLoan);

10. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Customer (Custid, Custname, Addr, phno,panno)
Loan (Loanid, Amount, Interest, Custid)
Account (Accd, Accbal, Custid)
a. Display the sum of Accbal of a Particular Custname(LEENA)
Query: select custid, custname from customer where custname=LEENA and custid in
(select sum(accbal) from account group by custid);
10

b. Update the interest with 1% when Accbal of the Custid >50% of Loan
Amount
Query: update account set accbal=accbal+(accbal * 0.01) where accbal > (select (amount
* 0.5) from loan);

c. Create a Trigger which checks whether the Accbal is 25% of amount to be


inserted in Loan
Query:
Create trigger f after insert on loan
Begin
dbms_output.put_line(account.accbal);
end;
/
d. Create a View with Accbal and Loan Amount of all Cutomers
Query: create view d as select l.amount, a.accbal from customer c, account a, loan l where
c.custid=a.custid and c.custid=l.custid;

11. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Customer (Custid, Custname, Addr, phno,panno)
Loan (Loanid, Amount, Interest, Custid)
Account (Accd, Accbal, Custid)
a. Display the Custname having both Loan and Account
Query: select custname from customer where custid in (select custid from loan intersect
select custid from account);
b. Create an user defined function getBal which returns the Sum of Accbal of
a particular Custid passed as parameter
SQL>CREATE OR REPLACE FUNCTION em_dtl_func
RETURN account.accbal%type
IS
acc_bal account.accbal%type;
11

BEGIN
SELECT sum(accbal) INTO acc_bal FROM account WHERE custid = 2;
RETURN acc_bal;
END; /
SQL> select em_dtl_func from dual;
c. Create a procedure which prints the Customer details whose sum of Accbal
>10000 using getBal function created by user above (in b)
Query: CREATE OR REPLACE PROCEDURE disp
IS
CURSOR emp_cur is
Select sum(accbal), custid from account group by custid having sum(accbal)>10000;
emp_rec emp_cur%rowtype;
BEGIN
FOR emp_rec in emp_cur
LOOP
dbms_output.put_line(emp_rec.custid);
END LOOP;
END;
/
Query: set serveroutput on
Query: EXECUTE disp(2);

12. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Customer (Custid, Custname, Addr, phno,panno)
Loan (Loanid, Amount, Interest, Custid, PrincipalPaid)
Account (Accd, Accbal, Custid)
a. Display the Custname doesnt holdany Account nor taken any Loan
Query: select custname from customer where custid not in (select custid from loan union
custid from account);
12

b. Create an user defined function getBalance which returns the Balance


Principal Loan Amount Pending for a Particular Custid passed as parameter
Query: Answer 11(b)
c. Create a procedure which prints the Customer details whose have Balance
Principal Loan Amount Pending >0 using getBalance function created by user
above (in b)
Query: Answer 11(c)

13. Create Tables as follows by choosing appropriate data type and


set the necessary primary and foreign key constraints:
Customer (Custid, Custname, Addr, phno,panno)
Loan (Loanid, Amount, Interest, Custid, LoanDate)
a. Display number of Loans, the sum of Loan Amount of a Particular
Custname(LEENA)
Query: select custid, custname from customer where custname=LEENA and custid in
(select sum(amount) loan group by custid);
b. Create a Report (using Crystal Reports/Any report generation tool) which
displays Loan Taken between From and To Dates

14. Create Tables as follows by choosing appropriate data type and set the
necessary primary and foreign key constraints:
Product (Prodid, Prodesc, Price, Stock, Reord)
Sales (Salesid, Proid, qty)
a. Display the total amount (price * qty) of Sales made so far
Query: select totalamount as p.price*s.qty from product p, sales s where p.prodid=s.proid;
b. Create a Report (using Crystal Reports/ Any report generation tool) when
Given with a Proid generates the list of sales made with sum of quantity sold

15. Create Tables as follows by choosing appropriate data type and set the
necessary primary and foreign key constraints:
Customer (Custid, Custname, Addr, phno,panno)
13

Loan (Loanid, Amount, Interest, Custid, LoanDate)


a. Display number of Total Loan Amount taken on LoanDates and order by Loan
Date then by Loan Amount
Query: select sum(amount) from loan group by LoanDate order by LoanDate,amount;
b. Create a Form to make a Loan entry (using VB/ VC++/Java).
c. Loan can be entered only for available customer so display available Custid
in the Form to Choose one among them for Loan Entry
Project for b & c

16. Create Tables as follows by choosing appropriate data type and set the
necessary primary and foreign key constraints:
Product (Prodid, Prodesc, Price, Stock, Reord)
Sales (Salesid, Proid, qty)
a. Double the Reord value of Products with Price >500 and Reord <50
Query: update product set reord=reord*2 where price>500 and reord<50;
b. Create a Form to make a Sale entry (using VB/ VC++/Java).
c. Sale can be entered only for available products so display available Prodid in
the Form to Choose one among them for Sale Entry
Project for b & c

You might also like