Solution Set 1
Solution Set 1
Solution Set 1
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
Page 1 of 28
b. Create the above mentioned tables and populate the tables [15]
INSERT INTO BUS_T VALUES ( 101,'PUNE','CHENNAI' );
INSERT INTO BUS_T VALUES ( 102,'PUNE','BANGLORE' );
INSERT INTO BUS_T VALUES ( 103,'CHENNAI','PUNE' );
INSERT INTO BUS_T VALUES ( 104,'CHENNAI','BANGLORE' );
INSERT INTO BUS_T VALUES ( 105,'BANGLORE','PUNE' );
INSERT INTO BUS_T VALUES ( 106,'BANGLORE','PUNE' );
ALTER TABLE PASSENGER_T ADD CONSTRAINT CHECK_PASS_DOB check (EXTRACT(YEAR FROM DOB)
> 2010)
c. Display the passengers who had booked the journey from Bangalore to Chennai on 03-NOV-
2014. [10]
Select P.* from passenger_t P,book_ticket_t t,bus_t b where t.pid = p.pid and t.journey_date =
'11/3/2014' and t.routeno = b.routeno and b.source = 'BANGLORE' and b.destination='CHENNAI'
d. List the details of passengers who have traveled more than three times on the same route. [10]
Page 2 of 28
select pid, count(routeno) from book_ticket_t group by pid having count (routeno) >= 3;
e. Create a view that displays the RouteNo, source, destination and journey_date which moves
from Chennai to Pune. [10]
h. Create a PL / SQL stored procedure that accepts journey_date and displays list of passengers
booked ticket on that date. [20]
i. In the above created procedure, include exceptions to display "No ticket booked on specified date"
for a given journey_date
Exercise – 2
Page 3 of 28
Consider the following relations for a boat management application for a beach resort:
SAILOR (SID, NAME, DOB, GENDER)
CREATE TABLE SAILOR( SID INT NOT NULL, NAME VARCHAR(50), DOB DATE, GENDER VARCHAR
(1),PRIMARY KEY(SID));
BOAT (BID, BTYPE, BNAME, COLOR)
BTYPE can take two values (D, S)
D – Deluxe
S –Super Deluxe
CREATE TABLE BOAT (BID INT NOT NULL, BTYPE VARCHAR (1), BNAME VARCHAR (50), COLOR
VARCHAR (20), PRIMARY KEY(BID),
CONSTRAINT CHECK_BTYPE check (BTYPE in ('B','S')))
a. The primary keys are underlined. Identify the foreign keys and draw schema
diagram [5]
b. Create the above mentioned tables and populate the tables [15]
Note: Read all questions and populate values accordingly.
INSERT INTO SAILOR VALUES (&SID,'&NAME','&DOB',’&GENDER’ );
INSERT INTO BOAT VALUES (&BID,'&BTYPE’,’&BNAME','&COLOR');
Page 4 of 28
INSERT INTO SAILS VALUES (&SID,&BID,&DOT,’&SHIFT');
Exercise – 3
Consider the following relations for an order processing application:
CUSTOMER (CID, NAME)
PRODUCT (PCODE, PNAME, UNIT_PRICE)
CUST_ORDER (OCODE, ODATE, CID)
ORDER_PRODUCT (OCODE, PCODE, NOU)
NOU – Number of Units. An order can contain many products.
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
Page 5 of 28
b. Create the above mentioned tables and populate the tables [20]
Note: Read all questions and populate values accordingly.
Create table Customer (Cid int not null, name varchar(50), primary key(cid));
Create table Product (pid int not null, pname varchar(50), unit_price int, primary key(pid));
Create table customer_order (ocode int not null, odate date, cid int not null, primary key(ocode));
Create table order_product (ocode int not null, pcode int not null, nou varchar, primary key(ocode,
pcode));
c. Ensure that product names should be within Laptop, Mouse, Server, Air conditioner [5]
alter table product add constraint check pnname in (‘Laptop’, ‘Mouse’, ‘Server’, ‘Air conditioner’)
d. Develop a SQL query to list the details of products whose unit price is greater than the
average price of all products [10]
select * from product where unit_price > avg(unit_price);
e. List the customer names who have orders more number of products [10]
select t1.name from customer t1, cust_order t2, order_product t3 where t1.cid = t2.cid and t2.ocode
= t3.ocode.
f. Create a view that displays the PCODE, PNAME and NOU of the product ordered [10]
create view product_view as
select t1.pcode, t1.pname, t2.now from product t1, Order_Product t2 where t1.pcode = t2.pcode
g. Create a function that accepts PCODE, Unit_Price and NOU. Calculate the total_cost
of the ordered product. Return the total_cost. [20]
Page 6 of 28
create procedure get_total_cost_sp(pcode in int, unit_price in int, nou in int, total_cost out varchar)
as
begin
Select unit_price * nou into total_cost;
end find_sname_sp;
h. Create a sequence named Product_Sequence that will get incremented by 1. Use the created
sequence while inserting PCODE into Product table. [20]
Create sequence product_sequence minvalue 1 start with 1 increment by 1 cache 20;
Exercise - 4
Consider the following relations for a transport management system application:
BUS (ROUTENO, SOURCE, DESTINATION)
DRIVER (DID, DNAME, DOB, GENDER)
ASSIGN_ROUTE (DID, ROUTENO, JOURNEY_DATE)
Create table bus (routeno int not null, source varchar(50), destination varchar(50), primary
key(routeno));
Create table driver (did int not nll, dname varchar(50), dob date, gender varchar(1), primary
key(did));
Create table assign_route(did int not null, routeno int not all, journey_date date, primary key(did,
routeno));
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [15]
Note: Read all questions and populate values accordingly.
c. Include constraints that the routeNo starts with letter 'R' and gender of driver is always
'Male' [10]
Alter table bus add constraint check (routeno like ‘R%’);
Alter table driver add constraint check (gender = ‘M’);
d. Develop a SQL query to list the details of drivers who have traveled more than three
times on the same route [10]
select t1.did, dname,dod, gener from driver t1, assign_route t2 where t1.did = t2.did group by t2.did
having count (routeno) >= 3;
e. Create a sequence named Driver_Sequence that will get incremented by 1. Use the created
sequence while inserting DID into Driver table. [20]
Create sequence driver_sequence minvalue 1 start with 1 increment by 1 cache 20
f. Create a view that displays the DID, DNAME assigned for RouteNo 'R5' on 02-NOV-2014 [20]
create view driver__view as
select t1.did, t1.dname, t2.routeno from driver t1, assign_route t2 where t1.did = t2.did and
t2.routeno = ‘R5’ and t2. Journeydate =’02-nov-2014’;
Page 7 of 28
g. Create a procedure that displays the details of all drivers. [20]
Exercise – 5
5. Consider the following relations for a transport management system application:
DRIVER (DCODE, DNAME, DOB, GENDER) CITY (CCODE, CNAME)
TRUCK (TRUCKCODE, TTYPE)
TTYPE can take two values (‘L’,’H’)
L-Light
H- Heavy
Each truck is assigned a unique truck code. There can be many trucks belonging to the same truck
type.
DRIVE_TRUCK (TRUCKCODE, DCODE, DOT, CCODE)
DOT – Date of Trip
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram
[5]
b. Create the above mentioned tables and populate the tables [20]
Create table driver (dcode int not null, dname varchar (50), dob date, gender varchar(10), primary
key(dcode));
Create table city (ccode int not null, cname varchar(50), primary key(ccode));
Create table truck (truckcode int not null, ttype varchar(50), primary key(truckcode));
Create table drive_truck (truckcode int not null, dcode int not null, dot date, ccode int not null,
primary key (truckcode,dcode, dot));
c. Include the constraint as mentioned above and the gender of driver is always 'male'. [10]
Alter table driver add constraint check (gender = ‘male’)
d. Develop a SQL query to list the details of each driver and the number of trips traveled. [10]
select dname, count(dot) from driver t1, drivetruck t2 where t1.dcode = t2.dcode group by
t2.dcode
e. Create an index on truck_code in Drive_truck table [5]
Create unique index trk_cde on Driver_Truck(truck_code);
f. Create a view that displays the Driver details and also the city in which he drives a truck
[20]
Create view driverDetailsView as
Select t1.dname, t2.city from driver t1, city t2, driver_truck t3 where t1.dcode=t3.dcode and
t3.code = t3.ccode;
Page 8 of 28
g. Create a procedure that displays the details of all drivers, the truck_code and DOT. Use cursors
appropriately. [30]
l_name employee_cur%ROWTYPE;
l_dob employee_cur%ROWTYPE;
l_gender employee_cur%ROWTYPE;
l_truckcode employee_cur%ROWTYPE;
l_dot employee_cur%ROWTYPE;
BEGIN
OPEN employee_cur;
LOOP
FETCH employee_cur INTO l_name, l_dob,l_gender,l_truckcode, l_dot;
EXIT WHEN employee_cur%NOTFOUND;
dbms_output.put_line( l_name, l_dob,l_gender,l_truckcode, l_dot);
END LOOP;
CLOSE employee_cur;
END;
End;
Exercise – 6
Consider the following relations for an order-processing database application in a company:
CUSTOMER (CUSTOMERNO VARCHAR2 (5), CNAME VARCHAR2 (30), CITY VARCHAR2 (30))
Implement a check constraint to check CUSTOMERNO starts with ‘C’
CUST_ORDER (ORDERNO VARCHAR2 (5), ODATE DATE, CUSTOMERNO REFERENCES CUSTOMER,
ORD_AMT NUMBER (8))
Implement a check constraint to check ORDERNO starts with ‘O’
ITEM (ITEMNO VARCHAR2 (5), ITEM_NAME VARCHAR2 (30), UNIT_PRICE NUMBER (5))
Implement a check constraint to check ITEMNO starts with ‘I’
ORDER_ITEM (ORDERNO REFERENCES CUST_ORDER, ITEMNO REFERENCES ITEM, QTY
NUMBER (3))
SHIPMENT (ORDERNO REFERENCES CUST_ORDER, ITEMNO REFERENCES ITEM, SHIP_DATE DATE)
Here, ORD_AMT refers to total amount of an order (ORD_AMT is a derived attribute);
ODATE is the date the order was placed; SHIP_DATE is the date an order is shipped.
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram
[5]
b. Create the above mentioned tables and populate the tables [30]
Page 9 of 28
Create Table Customer (Customerno Varchar2 (5), Cname Varchar2 (30), City Varchar2 (30), Primary
Key(Customerno));
Create Table Cust_Order (Orderno Varchar2 (5), Odate Date, Customerno Varchar2 (5), Ord_Amt
Number (8))
Create Table Item (Itemno Varchar2 (5), Item_Name Varchar2 (30), Unit_Price Number (5));
Create Table Order_Item (Orderno Varchar2 (5), Itemno Varchar2 (5), Qty Number (3))
Create Table Shipment (Orderno Varchar2(5), Itemno Varhar2(5), Ship_Date Date)
d. Develop a SQL query to list the order number and number of items in each order [10]
Select t1.OrderNo, t2.Qty from Cust_Order t1, Order_Item t2 where t1. Orderno = t2.orderno;
f. Create a view that will keep track of the details of each customer and the number of orders
placed by each customer [20]
Create view customerOrderView as
Select t1.customerNo, t1.cname, t2.orderno from customer t1, cust_order t2 where t1.customerNo =
t2.CustomerNo;
g. Develop a database trigger that will not permit to insert more than six records in the
CUST_ORDER relation for a particular order. (An order can contain a maximum of six items).
[20]
Exercise – 7
7. Consider the following relational schema for a banking database application:
Page 10 of 28
CUSTOMER (CID, CNAME)
An account can be a savings account or a current account. Check ATYPE in ‘S’ or ‘C’. A customer can
have both types of accounts.
D- Deposit; W – Withdrawal
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [15]
Create table Account (Ano Number, Atype Varchar2(20), Balance Number, CID Number, Primary
key(Ano));
Create table Transaction (TID Number, Ano Number, Ttype varchar2(20), Tdate date, Tamount
number, primary key(tid));
d. Write a query that lists the customer details and the number of accounts each customer
has. [10]
Select t1.cid,t1.cname, count(t2.Ano) from Customer t1, Account t2 where t1.Cid = T2 .Cid group by
t2.cid
e. Create a sequence named Customer_Sequence which gets incremented by 10 and use this
f. Create a view that will keep track of the details of each customer and account details who have
both savings and current account. [10]
Select t1.*, t2.* from Customer t1, Account t2 where t1.cid = t2.cid and t2.Atype in (‘S’,’C’);
Page 11 of 28
g. Develop a database procedure that will accept transaction id, account number, transaction type,
transaction date and transaction amount as input and insert a record into
i. If TTYPE =’D’ the value of BALANCE in the ACCOUNT table must be incremented by the value of
TAMOUNT
ii. If TTYPE =’W’ the value of BALANCE in the ACCOUNT table must be decremented by the value of
TAMOUNT.
If a minimum balance of Rs. 2000/- will be maintained for a savings account and a minimum balance
of Rs. 5000/- will be maintained for a current account else appropriate messages must be displayed
[30]
i. In the above created procedure, if TTYPE = ‘W’, and transaction amount is > available balance,
raise exceptions to display “Amount > available Balance” [10]
Exercise – 8
Consider the following relational schema for a banking database application:
An account can be a savings account or a current account. Check ATYPE in ‘S’ or ‘C’. A customer can
have both types of accounts.
D- Deposit; W – Withdrawal
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [20]
Create Table Account (Ano Number, Atype Varchar2(20), Balance Number, CID Number, BCode
Varchar2(20), Primary key(Ano));
Create Table Transaction (Tid Number, Ano Number, Ttype Varchar2(20), Tdate date, Tamount
Number, Primary Key(Tid));
Page 12 of 28
Insert into Account Values (&Ano, ‘&Atype’ , &Balance, &Cid, ‘&Bcode’);
d. Develop a SQL query to list the details of branches and the number of accounts in each
branch. [10]
Select t1.Bcoe, t1.Bname, count(t2.Ano) from Branch t1, Account T2 where t1.Bcode = t2. Bcode
group by t2.Ano;
e. Develop a SQL query to list the details of customers who have performed three
Select t1.Cname, count(t3.Ano) from Customer t1, Account t2, Transaction t3 where t1.cid = t2. cid
and t2.ano = t3. Ano group by t3. Ano, t3.tdate having count(t3.Ano) > 3;
f. Create a view that will keep track of the details of each customer and account details who have
both savings and current account. [10]
Select t1.Cid, t1.Cname from Customer t1, Account t2 where t1.cid = t2.cid and t1.cid in (select cid
from Account where Atype = ‘S’ and Cid in (Select Cid from Account where Atype = ‘C’));
g. Develop a database trigger that will update the value of BALANCE in ACCOUNT table
when a record is inserted in the transaction table. Consider the following cases:
i. If TTYPE =’D’ the value of BALANCE in the ACCOUNT table must be incremented by the value of
TAMOUNT
ii. If TTYPE =’W’ the value of BALANCE in the ACCOUNT table must be decremented by the value of
TAMOUNT.
If a minimum balance of Rs. 2000/- will be maintained for a savings account and a minimum balance
of Rs. 5000/- will be maintained for a current account else appropriate messages must be displayed
[30]
Page 13 of 28
IF (Atype = 'S' and Balance < 2000) Then
dbms_output.put_line('Savings Account Balance is less the required minium account ')
ELSIF (Atype = 'C' and Balance < 5000) Then
dbms_output.put_line('Current Account Balance is less the required minium account ')
END IF;
end;
Exercise -9
Consider the following relational schema for a library management system:
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [25]
Create Table Book (Bookid Number, Title Varchar2(20), publisherCode Number, No_of_Copies
Number, Primary Key(Bookid));
Create Table Book_Author (Bookid Number, AuthorId Number, Primary key (Bookid));
Create Table Book_Loan (Book_ID Number, CardNo Number, DateOut Date, DueDate Date, Status
Varchar2(20), Primary Key(Book_id, Cardno, Dateout));
d. Develop a SQL query to list the details of borrowers who do not have any books checked
out. [5]
Page 14 of 28
Select t1. Name from Borrower t1, Book_Loan t2 where t1.cardNo = t2.CardNo and t2.Status =’R’;
e. Develop a SQL query to list the details of borrowers who have more than five books
Select t1. Name, count(t2.status) from Borrower t1, Book_Loan t2 where t1.cardNo = t2.CardNo and
t2.Status =’T’ group by t2.cardNo having count(cardNo) > 5;
Select t1. Name, count(t2.status) from Borrower t1, Book_Loan t2 where t1.cardNo = t2.CardNo and
t2.Status =’T’ group by t2.cardNo ;
h. Create a procedure named Author_Details that accepts the BookID and displays the author
ID, author name and also the status of the book. [30]
create or replace procedure Author_details_sp ( bookid in Number, Authorid out varchar2(20), Name
out varchar2(20),Status out Varchar2(20) )
as Begin
Select t1. Authorid, t2.Name, t3.status from Author t1, Author_book t2, Book_Loan t3 where
t1.authorid = t2.Authorid and t1.bookid = Bookid and t3.Bookid = t2.Bookid;
end find_passenger_sp;
Exercise – 10
Consider the following Staff relational schema:
BASIC_PAY, DEPTNO)
OUT_TIME)
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
Page 15 of 28
b. Create the above mentioned tables and populate the tables [30]
Create table Staff (StaffNo Number, Name Varchar2(20), DOB date, Gender Varchar2(20), DOJ Date,
Designation Varchar2(20), Primary Key(StaffNo));
Create Table Staff_Skill (StaffNo Number, Skill_code Number, Primary Key(StaffNo, Skill_Code));
d. Develop a SQL query to list the details of staff who earn less than the basic pay of all
staff. [10]
e. Create a view that keeps track of DeptNo, DeptName and number of staff in each
department. [10]
Select t1.DeptNo, t1.DeptName, Count(t2.StaffNo) from Dept t1, Staff t2 where t1.staffno = t2. Staff
no and t1.DeptNo = t2.DeptNo group by t1.deptNo;
f. Develop a SQL query to list the details of staff who have more than three skills. [5]
Select t1.StaffNo, t1.Name, t1.Designation from Staff t1, StaffSkill t2, Skill t3 where t1.Staffno=
t2.Staffno and t2.Skillcode =t3.Skillcode and count (t3.skillcode ) > 3 group by t3.skillcode;
h. Develop a procedure Staff_Increment that will accept staff number and increment amount
as input and update the basic pay of the staff in the staff table. [20]
i. In the above procedure include exception in the procedure that will display a message
“Staff has basic pay null” if the basic pay of the staff is null and display a message “No
such staff number” if the staff number does not exist in the staff table. [10]
Page 16 of 28
IF (BasicPay = Null) Then
dbms_output.put_line('Baic pay of staff is Null. ')
ELSIF (if not exist(Select StaffNo from Staff where StaffNo = StaffNo) ) Then
dbms_output.put_line('No Such Staff Number Exist ')
END IF;
End ;
Exercise – 11
Consider the following relational schema for a company database application:
EMPLOYEE (ENO, NAME, GENDER, DOB, DOJ, DESIGNATION, BASIC,
DEPT_NO, PAN, SENO)
Implement a Check Constraint for GENDER
PAN – Permanent account Number
SENO – Supervisor Employee Number
DEPARTMENT (DEPT_NO, NAME, MENO)
MENO - Manager Employee Number
PROJECT (PROJ_NO, NAME, DEPT_NO)
WORKSFOR (ENO, PROJ_NO, DATE_WORKED, HOURS)
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [20]
Note: Read all questions and populate values accordingly.
Create Table Employee (Eno Number, Gender Varchar2(10), DOB Date, DOJ Date, Designation
Varchar2(20), Basic Number, Dept_no Number, Primary Key(Eno));
Create Table Department (DeptNo Number, Name Varchar2(20), Memo Varchar2(20), Primary
Key(DeptNo));
Create Table Project (ProjectNo Number, Name Varchar2(20), DeptNo Number, Primary
Key(ProjNo));
d. Develop a SQL query to list the details of department which has more than 3 employees
working for it. [10]
Select t1.DeptNo, T1.DeptName where Department T1, Employee T2 where t1.DeptNo =
T2.DeptNo and group by t.1DeptNo having count(t1.deptno) >3;
e. Create a view that keeps track of DeptNo, DeptName and number of employees in each
department. [10]
Create View deptView as
Select t1.DeptNo, T1.DeptName, count(t2.Eno) from Department T1, Employee T2 where
t1.DeptNo = T2.DeptNol
f. Develop an SQL query to list the departments and the details of manager in each
department. [5]
Select t1.Name, t2.Designation from Departmnet t1, Employee t2 where t1.DeptNo = T2.DeptNo
and T2.Designation = ‘Employee’;
Page 17 of 28
h. Develop a procedure Employee_Increment that will accept Employee number and increment
amount as input and update the basic pay of the employee in the employee table. [20]
i. In the above procedure include exception in the procedure that will display a message
“Employee has basic pay null” if the basic pay of the employee is null and display a message “No
such Employee number” if the employee number does not exist in the employee table. [10]
j. Create a database trigger that will not permit to insert values into Employee table if DOJ is less
than DOB. [10]
Exercise – 12
Consider the following relational schema for a Product Sales database application:
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [20]
Create Table Product (ProductID Number. Prodesc Varchar2(20), Price Number, Stock Number,
Primary Key(Prodid));
Create Table Purchase (Purid Number, Proid Number, Qty Number, SupplierName Varchar2(20),
Primary Key(Purid));
Create Table Sales (Salesid Number, Proid Number, Qty Number, CustName Varchar2(20));
c. Include the constraint on Saleid that it starts with letter ‘S’. [5]
Alter Table Sales add Constraint Saleidx Check (Saleid like ‘S%’);
d. Display the ProdID and the sum of quantity purchased for each product. [10]
Page 18 of 28
Select t1.Prodid, prodesc SUM(Qty) product t1, Purchase t2 where t1.proid =p2.proid;
e. Create a view that keeps track of Prodid, price, Purid, qty and customerName who made
Select t1.Proid, t1.price, t2.purid, t2.qty, t2.custName from Product t1, sales t2 where t1.prodid =
t2.prodid;
f. Create a sequence named Product_Sequence that gets incremented by 10 and use it for
g. Develop a procedure named Product_Sales that accepts a prodid and displays all the sales
h. In the above procedure include exception in the procedure that will display a message
“No such Product ID” if the given product id does not exist in the product table. [10]
IF (if not exist(Select Prodid from Product where Prodid = Pid) ) Then
dbms_output.put_line('No Such Product Exist ')
END IF;
End ;
Exercise – 13
Consider the following relational schema for a Product Sales database application:
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [20]
Create Table Product (ProductID Number. Prodesc Varchar2(20), Price Number, Stock Number,
Primary Key(Prodid));
Create Table Purchase (Purid Number, Proid Number, Qty Number, SupplierName Varchar2(20),
Primary Key(Purid));
Create Table Sales (Salesid Number, Proid Number, Qty Number, CustName Varchar2(20));
Page 19 of 28
Insert into Product Values(&Prodid, ‘&Prodesc’,&Price,&Stock);
c. Include the constraint on Saleid that it starts with letter ‘S’. [5]
Alter Table Sales add Constraint Saleidx Check (Saleid like ‘S%’);
d. Display the ProdID and the sum of quantity purchased for each product. [10]
Select t1.Prodid, prodesc SUM(Qty) product t1, Purchase t2 where t1.proid =p2.proid;
e. Create a view that keeps track of Prodid, price, Purid, qty and customerName who made
Select t1.Proid, t1.price, t2.purid, t2.qty, t2.custName from Product t1, sales t2 where t1.prodid =
t2.prodid;
f. Create a sequence named Product_Sequence that gets incremented by 10 and use it for
g. Develop a procedure named Product_Sales that accepts a prodid and displays all the sales
“No such Product ID” if the given product id does not exist in the product table. [10]
IF (if not exist(Select Prodid from Product where Prodid = Pid) ) Then
dbms_output.put_line('No Such Product Exist ')
END IF;
End;
Exercise - 14
Consider the following relational schema for a Loan database application:
Customer (Custid, Custname, Age, phno)
Loan (Loanid, Amount, Custid)
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [10]
Note: Read all questions and populate values accordingly.
Create Table Customer (Custid Number, CustName Varchar2(20), Age Number, Phno Number,
Primary Key(Custid));
Create Table Loan(Loanid Number, Amount Number, Custid Number, Primary Key (Loanid));
Page 20 of 28
c. Include the constraint on Loanid that it starts with letter ‘L’. [5]
Alter Table Loan add constraint loanidx Check (Loanid like ‘L%’);
d. Display the list of the customerids and total Loan amount taken [10]
Select t1.Custid, Sum(t2.Amount) from Customer t1, Loan t2 where t1. Custid = T2.Custid group
by t2.custid;
e. Display the CustId and CustName who have taken less than 2 loans [10]
Select t1.Custid, t1.CustName from Customer t1, Loan t2 where t1. Custid = T2.Custid having
count(t2.Custid <2);
f. Create a view that keeps track of Custid, Custname, loanid and loan amount. [20]
Create View CustView as
Select t1.Custid, t1.CustName,t2.Loanid,t2.Amount from Customer t1, Loan t2 where t1. Custid
= T2.Custid
g. Create a sequence named Customer_Sequence that gets incremented by 3 and use it for
inserting Custid values in Customer table. [10]
Create sequence Customer_sequence minvalue 1 start with 3 increment by 3 cache 20;
h. Develop a function named Customer_Loan which accepts Loanid as input and displays
Custid, CustName and loan_amount. [20]
Create procedure CustLoan_sp (Lnid in Number )
as begin
Select t1.Custid, t1.CustName, t2.amount from Customer t1, Loan t2 where t1.Custid =
t2.Custid; and t2.loanid = Lnid
End;
Exercise – 15
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [10]
Create Table Customer (Custid Number, Custname Varchar2(20), Age Number, phno Number, Primary
Key (Custid));
Create table HLoan (HLoanid Number , Amount Number, Custid Number, Primay key(HLoanid));
Create table VLoan (VLoanid Number , Amount Number, Custid Number, Primay key(VLoanid));
Page 21 of 28
c. Include the constraint on HLoanid that it starts with letter ‘H’ and VLoanid starts with
Alter table HLoan add constraint Hlnidx Check (HLoanid like (‘H%));
Alter table VLoan add constraint Vlnidx Check (VLoanid like (‘V%));
e. Display the list of the customerids and total HLoan amount taken. [10]
f. Create a view that keeps track of customer details who have taken both HLoan and VLoan. [20]
Select t1.CustName from Customer t1, HLoan t2, VLoan t2 where t1.custid= t2.custid and t2.custid =
t3.custid;
g. Create a sequence named Customer_Sequence that gets incremented by 3 and use it for
h. Develop a procedure named Customer_Loan which accepts HLoanid as input and displays
i. In the above procedure include exceptions to display “No such HLoanid” when incorrect
IF (if not exist(Select HLoanid from HLoan where HLoanid = HLnid) ) Then
dbms_output.put_line('No Such HLoanid Exist ')
END IF;
End ;
Exercise – 16
Consider the following relational schema for a Loan database application:
Page 22 of 28
Account (Accid, Accbal, Custid)
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [10]
Create Table Customer (Custid Number , Custname Varchar2(20), Addr Varchar2(20), phno
Number ,pan_no Varchar2(20), Primary Key(Custid));
Create Table Loan (Loanid Number , Amount Number , Interest Number, Custid Number, Primary Key
(Loanid));
Create Table Account (Accid Number, Accbal Varchar2(20), Custid Number , Primary Key(Accid));
c. Include the constraint on Custid that it starts with letter ‘C’ [5]
Alter Table Customer add constraint custidctrn Check (Custid like ‘C%’);
d. Display the customer id, name and account balance. Sort the output using custid [10]
Select t1.Custid, t1.CustName, t2.Amount from Customer t1, Loan t2 where t1.Custid = T2.custid;
f. Display the custid who has account balance larger than other customers [5]
Select t1.Custid from Customer t1, Account t1 where t1.Custid = t2.custid and t2.Accbal =
max(Accbal);
h. Create a view that keeps track of customer id, loan amount and account balance. [20]
Select t1.Custid, t1.CustName, t2.Amount from Customer t1, Loan t2 where t1.Custid = T2.custid;
i. Develop a procedure named Customer_Loan that displays all the loan details [20]
j. In the above procedure include exceptions to display “No such Loanid” when incorrect
IF (if not exist(Select Loanid from Loan where Loanid = Lnid) ) Then
dbms_output.put_line('No Such Loanid Exist ')
END IF;
End ;
Page 23 of 28
Exercise – 17
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [20]
Create Table Product (ProductID Number. Prodesc Varchar2(20), Price Number, Stock Number,
Primary Key(Prodid));
Create Table Purchase (Purid Number, Proid Number, Qty Number, SupplierName Varchar2(20),
Primary Key(Purid));
Create Table Sales (Salesid Number, Proid Number, Qty Number, CustName Varchar2(20));
c. Include the constraint on Saleid that it starts with letter ‘S’. [5]
Alter Table Sales add Constraint Saleidx Check (Saleid like ‘S%’);
d. Display the names who are both supplier as well as customer [10]
Select t1.CustName from Sales t1, Purchase t2 where t1.Custname = t2.Supplier Name;
Select t1.price * t2. Qty from Product t1, Purchase t2, Sales t3 where t1. Proid = t2.proid and t2.proid
= t3.proid group by t3.proid
f. Create a view which displays Product ids and sum of quantity in sales [20]
g. Create a sequence named Product_Sequence that gets incremented by 10 and use it for
Page 24 of 28
Exercise – 18
Consider the following relational schema for a Loan database application:
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [10]
Create Table Customer (Custid Number, Custname Varchar2(20), Age Number, phno Number, Primary
Key(Custid));
Create Table Loan (Loanid Number, Amount Number, Custid Number, EMI Number, Primary
Key(Loanid));
c. Include the constraint on Custid that it starts with letter ‘C’. [5]
Alter table Customer Add constraint CustCnstrn Check (Custid like ‘C%’);
e. Display the custid and Custname whose loan amount lies in the range of 30,000 to 50,000
Select t1.Custid,t1.CustName, T2.Amount from Customer t1, Loan t2 where t1.Custid = t2.custid and
t2. Amount between 30000 and 50000;
f. Display the CustId and CustName who have taken less than 2 loans [10]
Select t1.Custid,t1.CustName, T2.Amount from Customer t1, Loan t2 where t1.Custid = t2.custid and
count(t2.custid) <2 group by t2.Custid;
g. Create a view that keeps track of Custid, Custname, loanid and loan amount. [20]
i. Develop a function named Customer_Loan which accepts Loanid as input and displays
Page 25 of 28
Exercise – 19
Consider the following relational schema for a Books Ordering database application:
Customers(cust_id)
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [20]
Create Table Books (isbn Number, title Varchar2(20), author Varchar2(20), stock_qty Number, price
Number, pub_year Number, Primary Key(isbn));
Create Table Orders (order_no Number, cust_id Number, order_date Date, Primary Key(Orderr_no));
Create Table Order_list (order_no Number, isbn Number , qty Number, ship_date Date);
c. Include the constraint on Cust_id that it starts with letter ‘C’. [5]
d. Display the custid and Custname who have ordered more than 3 books on the same date
[10]
Select t1.Custid, t1.Name from Cutomer t1, Orders t2, Order_list t3 where t1.Custid = T2.custid and
t2.orderno = t3.orderno group by t2.order_date.
e. Display the CustId and CustName who have ordered very few number of books. [10]
f. Create a view that keeps track of books that are ordered on 05-NOV-2014. Display isbn,
h. In the above created procedure include exception to display “No such Order Number”
if incorrect order number is given. [10]
Create procedure Books_Ordered (OrderNo in Number )
as begin
Page 26 of 28
Select t1.Custid, t1.CustName, t2.title, t3.qty from Customer t1, Books t2, Order_list t3 where
t1.Custid = t2.Custid; and t2.orderid = t3.orderid and t3.orderid = OrderNo;
IF (if not exist(Select Order_no from Loan where Order_no = OrderNo) ) Then
dbms_output.put_line('No Such Order_no Exist ')
END IF;
End ;
Exercise – 20
Consider the following relational schema for Products Order database application:
Products (p_id, p_name, retail_price, qty_on_hand)
Orders (order_id, order_date)
Order_details (order_number, product_number, qty_ordered)
Where: order_number references order_id
product_number references p_id
a. The primary keys are underlined. Identify the foreign keys and draw schema diagram [5]
b. Create the above mentioned tables and populate the tables [20]
Note: Read all questions and populate values accordingly.
Create Table Products (p_id Number , p_name Varcahr2(20), retail_price Number , qty_on_hand
Number, Primary Key(P_id)) ;
Create Table Orders (order_id Number , order_date Date, Primary Key(Order_id));
Create Table Order_details (order_number Number, productNumber Number, qty_ordered Number,
primary key(order_number)) ;
c. Include the constraint on orderid that it starts with letter ‘O’. [5]
Alter table order add constraint orderconstraint check (ordered like ‘O%’);
d. Display the ProdID and the sum of quantity ordered for each product. [10]
Select t1.prodid, SUM( t3. Qty) from Product t1, Order t2, Order_details t3 where t1. Proid = t2.proid
and t2.proid = t3.productNumber group by t3. productNumber
e. Create a view that keeps track of P_id, price, order_id, qty_ordered and ordered_date. [20]
Create View productSales as
Select t1.prodid, t1.price,t2.orderid,t2.order_date, t3.qty_ordered from Product t1, Orders t2,
Order_details t3 where t1. P_id = t3.productNumber and t2.orderid = t3.orderNumber group by
t3.proidproductNumber
h. Create a database TRIGGER, which deletes the order from Orders table, AFTER the deletion of
corresponding order_number in Order_details. [30]
Page 27 of 28
create trigger Order_delete
after delete on Order_details
for each row
begin
Delete * from Order where order_id = OrderNumber;
end;
Page 28 of 28