RDBMS LAB Manual
RDBMS LAB Manual
create table branch (branch_name varchar2(15), branch_city varchar2(10),assets
number(10));
2. create table customer (customer_name varchar2(15), customer_street varchar2(
20), customer_city varchar2(10));
create table customer (customer_name varchar2(15), customer_street varchar2(20), cu
stomer_city varchar2(10));
3. create table account (account_number varchar2(10), branch_name varchar2(15)
,balance number(10));
create table account (account_number varchar2(10), branch_name varchar2(15),balan
ce number(10));
4. create table loan (loan_number varchar2(10),branch_name varchar2(15),amoun
t number(10));
create table loan (loan_number varchar2(10), branch_name varchar2(15),amount num
ber(10));
5. create table depositor (account_number varchar2(10), customer_name varchar2(
15));
create table depositor (account_number varchar2(10),customer_name varchar2(15));
6. create table borrower (customer_name varchar2(15), loan_number varchar2(10)
);
create table borrower (customer_name varchar2(15), loan_number varchar2(10));
Tutorial-2
Apply the INSERT, SELECT (DML) on the created in tutorial-1 statements to satisfy
the following Use aggregate function to retrieve summarized information from table.
Also Experiments various clauses (WHERE, IN, NOT IN, BETWEEN …) to filter the
information.
1. Insert records:
1) Insert records in branch table.
insert into branch values('SBI','Dehli',3000);
insert into branch values('SBM','Mysore',5000);
insert into branch values('CYNDICATE','Mandya',2500);
insert into branch values('CANARA','Mangalore',10000);
insert into branch values('ICICI','Mangalore',7500);
insert into customer values('Mohammed','V.V.Nagar','Delhi');
insert into customer values('Punith','V.Puram','Mandya');
insert into customer values('Zeel','Yalahanka','Bangalore');
insert into customer values('Paul','R.T.Nagar','Bangalore');
insert into customer values('Sachin','Newtown','Bangalore');
insert into account values('A-100','SBI',10000);
insert into account values('A-101','SBM',20000);
insert into account values('A-103','CYNDICATE',50000);
insert into account values('A-104','CANARA',20000);
insert into account values('A-105','ICICI',50000);
insert into loan values(1000,'SBI',10000);
insert into loan values(2000,'SBM',20000);
insert into loan values(3000,'CYNDICATE',25000);
insert into loan values(4000,'CANARA',36000);
insert into loan values(5000,'ICICI',60000);
insert into depositor values('Mohammed',100);
insert into depositor values('Punith',200);
insert into depositor values('Zeel',300);
insert into depositor values('Paul',400);
insert into depositor values('Sachin',500);
insert into borrower values('Mohammed',1000);
insert into borrower values('Punith',2000);
insert into borrower values('Zeel',3000);
insert into borrower values('Paul',4000);
insert into borrower values('Sachin',5000);
2. View tables:
10. select account_no from account where branch name in(‘kalavad road’,
‘mavdiroad’);
select account_number from account where branch_name in('SBM', 'SBI');
Tutorial-3
Tutorial: 4
Identify the limitation of database design created in tutorial-1. Make sure
such limitation should not be there in the new database of sales data to do so
Use database constraints on the following tables.
Table Creation:
1. create table customer_master(cust_no varchar2(10)primary key,product_no var
char2(6) references product_master, cust_nm varchar2(10)unique,cust_city varc
har2(20),cust_state varchar2(20),balance number(10));
create table customer_master(cust_no varchar2(10)primary key,product_no varchar2(6) refere
nces product_master, cust_nm varchar2(10)unique,cust_city varchar2(20),cust_state varchar2
(20),balance number(10));
2. create table product_master(product_no varchar2(6) primary key,name varchar
2(20) not null, description varchar2(50) not null, profite_percentage number(5,2)
, qty_on_hand number(8), reorder_level number(8) not null, sell_price number(8
,2) not null, cost_price number(8,2) not null);
create table product_master(product_no varchar2(6) primary key,name varchar2(20) n
ot null, description varchar2(50) not null, profite_percentage number(5,2), qty_on_ha
nd number(8), reorder_level number(8) not null, sell_price number(8,2) not null, cost
_price number(8,2) not null);
3. create table salesman_master(salesman_no varchar2(6)primary key,salesman_n
ame varchar2(20)not null, address varchar2(15)not null,city varchar2(10),state v
archar2(10),pincode number(8),sal_amt number(9,2)not null,remark varchar2(1
00));
create table salesman_master(salesman_no varchar2(6)primary key,salesman_na me
varchar2(20)not null, address varchar2(15)not null,city varchar2(10),sta te varchar2
(10),pincode number(8),sal_amt number(9,2)not null,remark varcha r2(100));
4. create table sales_order(order_no varchar2(6) primary key,CHECK(order_no li
ke'O%'),cust_no varchar2(10) references customer_master,order_date date not
null,salesman_no varchar2(6)references salesman_master,dely_type varchar2(1),
bill varchar2(1),dely_date date,order_status varchar2(15)CHECK(order_status i
n('in process','fulfilled','cancelled')));
create table sales_order(order_no varchar2(6) primary key,CHECK(order_no like'O
%'),cust_no varchar2(10) references customer_master,order_date date not null,salesm
an_no varchar2(6)references salesman_master,dely_type varchar2(1),bill varchar2(1),
dely_date date,order_status varchar2(15)CHECK(order_status in('in process','fulfilled'
,'cancelled')));
5. create table sales_order_details( order_no varchar2(6) references sales_order,pr
oduct_no varchar2(6) references product_master, qty_ordered number(8)not nul
l,qty_disp number(8)not null, product_rate number(10,2));
create table sales_order_details( order_no varchar2(6) references sales_order,product_
no varchar2(6) references product_master, qty_ordered number(8)not null,qty_disp nu
mber(8)not null, product_rate number(10,2));
Data Insertion:
1. Insert records in customer table.
Insert into customer_master values('C001','P00001','Man','Surat','Gujarat',50000);
insert into customer_master values('C002','P00345','Zeel','Mumbai','Maharashtra',400
0);
insert into customer_master values('C003','P06734','Teju','Mumbai','Maharashtra',300
00);
insert into customer_master values('C004','P07865','Anil','Rajkot','Gujarat',12000);
insert into customer_master values('C005','P07868','Divay','Mumbai','Maharashtra',10
000);
select * from customer_master;
insert into product_master values ('P00001','T-shirts',5,'Piece',200,300,400);
insert into product_master values('P00345','Shirts',6, 'Piece',150,500,600);
insert into product_master values('P0634','Cotton jeans',5,'Piece',100,600,800);
insert into product_master values('P07865','Jeans',5,'Piece',100,750,950);
Insert into product_master values('P07868','Trousers',2,'Piece',150,850,1000);
select * from product_master;
insert into sales_order_details values ('O19001','P00001',4,4,525);
insert into sales_order_details values ('O19002','P00345',2,1,8400);
insert into sales_order_details values ('O19003','P06734',2,1,5250);
insert into sales_order_details values ('O19003','P07865',10,0,525);
insert into sales_order_details values ('O19004','P07868',10,10,525);
select * from sales_order_details;
Tutorial-5
Analyze the tables and extract information from one table based on another table.
1. select product_no,description from product_master where product_no not in(select
product_no from sales_order_details);
select product_no,description from product_master where product_no not in(select produ
ct_no from sales_order_details);
2. select cust_no,cust_city,balance from customer_master where cust_no in(select cust
_no from sales_order where order_no='O19003');
select cust_no,cust_city,balance from customer_master where cust_no in(select cust_no
from sales_order where order_no='O19003');
3. select cust_no,cust_nm from customer_master where cust_no in(select cust_no fro
m sales_order where order_no in (select order_no from sales_order_details where p
roduct_no in(select product_no from product_master where description='Jeans')));
select cust_no,cust_nm from customer_master where cust_no in(select cust_no from sale
s_order where order_no in (select order_no from sales_order_details where product_no i
n(select product_no from product_master where description='Jeans')));
4. select cust_nm from customer_master where cust_no in(select cust_no from sales_o
rder where to_char(order_date,'mon-yy') > 'jul-09');
select cust_nm from customer_master where cust_no in(select cust_no from sales_order
where to_char(order_date,'mon-yy') > 'jul-09');
Tutorial: 6
Apply various database JOINS to discover information from multiple tables for the
followings.
1. select sod.product_no,description from sales_order_details sod, sales_order so, p
roduct_master pm, customer_master cm where pm.product_no=sod.product_no
and so.order_no=sod.order_no and cm.cust_no=so.cust_no and cm.cust_nm='An
il';
select sod.product_no,description from sales_order_details sod, sales_order so, produ
ct_master pm, customer_master cm where pm.product_no=sod.product_no and so.ord
er_no=sod.order_no and cm.cust_no=so.cust_no and cm.cust_nm='Anil';
2. select sales_order_details.product_no,order_no from sales_order_details,product
_master where sales_order_details.product_no = product_master.product_no an
d qty_ordered < 150 and description = 'Jeans';
select sales_order_details.product_no,order_no from sales_order_details,pro
duct_master where sales_order_details.product_no = product_master.product_n o a
nd qty_ ordered < 150 and description = 'Jeans';
3. select sod.product_no, description, sum(qty_disp) from sales_order_details sod, s
ales_order so, product_master pm where sod.product_no=pm.product_no and so
d.order_no=sod.order_no and to_char(order_date,'mon') = to_char(sysdate,'mo
n') group by description, sod.product_no;
select sod.product_no, description, sum(qty_disp) from sales_order_details sod, sales
_order so, product_master pm where sod.product_no=pm.product_no and sod.order_n
o=sod.order_no and to_char(order_date,'mon') = to_char(sysdate,'mon') group by desc
ription, sod.product_no;
4. select so.cust_no, sod.product_no, description, sum(qty_ordered) from sales_ord
er_details sod, product_master pm, sales_order so, customer_master cm where s
od.order_no=so.order_no and sod.product_no=pm.product_no and cm.cust_no=
so.cust_no group by so.cust_no, sod.product_no, description having so.cust_no='
C001' or so.cust_no='C002';
select so.cust_no, sod.product_no, description, sum(qty_ordered) from sales_order_de
tails sod, product_master pm, sales_order so, customer_master cm where sod.order_n
o=so.order_no and sod.product_no=pm.product_no and cm.cust_no=so.cust_no group
by so.cust_no, sod.product_no, description having so.cust_no='C001' or so.cust_no='
C002';
Tutorial-7
Identify critical / secret information in tables. Create different views to
provide access to other users.
1. Create view vw_sales as select sales_order.order_no,order_date,order_status,pro
duct_no, qty_ordered from sales_order, sales_order_details.
Create view vw_sales as select sales_order.order_no,order_date,order_status,product_
no, qty_ordered from sales_order, sales_order_details;
select * from vw_sales;
2. Create view vw_prodas AS select product_no, description, sell_price from produ
ct_master;
Create view vw_prodas AS select product_no, description, sell_price from product_m
aster;
1) Insert into vw_prod values('P08','cap',200);
Insert into vw_prod values ('P08','cap',200);
2) Update vw_prodas set sell_price=1600 where product_no='P06734';
Update vw_prodas set sell_price=1600 where product_no='P06734';
Delete from vw_prodas where product_no ='P07865';
3. Create view sels_deptas as select salesman_no, Salesman_name, City, Sal_amt fr
om Salesman_master;
Create view sels_deptas as select salesman_no, Salesman_name, City, Sal_amt from
Salesman_master;
Tutorial: 8
Create PL/SQL block for the followings to understand its basic programming
constructs:
1. Write a PL/SQL block to calculate the area of a circle for a value of radius
varying from 3 to 7 store the radius and the corresponding values of
calculated area in an empty table named areas consisting of two columns
radius and area.
create table areas (radius number(5),area number(14,2));
declare
pi constant number(4,2) :=3.14;
radius number(5);
area number(14,2);
begin
radius :=3;
while radius <=7 loop
area := pi * power(radius,2);
insert into areas values (radius, area);
radius := radius + 1;
end loop;
end;
2. Write a PL/SQL block of code for inverting the number 5639 to9365
declare
given_number varchar2(5) := '5639';
str_length number(2);
inverted_number varchar(5);
begin
str_length := length(given_number);
for cntr in reverse 1..str_length
loop
Tutorial: 9
Create reusable modules (function and stored procedure) to perform specific task as
mentioned below:
Functions:
1. Write a function that finds whether the input parameters are a prime no.
declare
n number;
i number;
temp number;
begin
n := 13;
i := 2;
temp := 1;
for i in 2..n/2
loop
if mod(n, i) = 0
then
temp := 0;
exit;
end if;
end loop;
if temp = 1
then
dbms_output.put_line('true');
else
2. Write a function that accepts the order no from the user and displays the
product no and name of the product for that order no.
Create or replace function f_order_no(vorder_no IN varchar2)
Return number is
dummy_order_no varchar2(6);
begin
select order_no into dummy_order_no from sales_order_details where o
rder_no = vorder_no;
return 1;
Exception
When No_Data_Found then return 0;
End;
Declare
Ans number(1);
Begin
Ans:= f_order_no('&order_no');
If ans = 0 then
Insert into sale_order(order_no,product_no,qty_ordered,qty_disp) values ('O101','P2',20,5)
;
End if;
Commit;
End;
Stored procedures:
1. Write a procedure which updates the field order_status from in
progress to fulfilled in the sales_order table.(dely_date + 5 days)
Create or replace procedure p1(dely_date in date, order_status in varchar2) IS
mdate date;
Status varchar2(20);
Begin
Select dely_date into mdate from sales_order where dely_date + 5 = sysdate;
Update sales_order set order_status = 'Fulfilled' where status ='cancelled';
End;
Declare
mdate date;
status varchar(20);
Begin
dbms_output.put_line(mdate || '' || status);
End
Tutorial: 10
Experiment the followings to understand CURSOR in Oracle:
Declare
CURSOR Crs_account IS Select account_no,branch_name,balance from account_master;
a_no account_master.account_no%type;
a_branch account_master.branch_name%type;
a_balance account_master.balance%type;
Begin
OPEN Crs_account;
dbms_output.put_line('Account_no Branch_name balance');
dbms_output.put_line('--------- ---------- -------');
LOOP
FETCH Crs_account into a_no,a_branch,a_balance;
EXIT when Crs_account%NOTFOUND;
Dbms_output.put_line(a_no || '' || a_branch|| '' || a_balance);
END LOOP;
CLOSE Crs_account;
End;
Tutorial: 11
Apply mechanism to maintain data consistency using trigger. Create
trigger that allow to perform the following operations on specified time.
1. Write a database trigger before insert for each row on table sales_order not
allowing transactions for the order_date as Sundays.
Create or replace trigger check_date Before insert on sales_order for each row
Declare
s_date char(3);
Begin
s_date := to_char(sysdate,'day');
If s_date in('SUNDAY') then
raise_application_error(-20001,'Trnasation not allow on sunday');
End if;
End;
Tutorial: 12
In client-server architecture, we need to make sure that every user has
proper rights to access database tables. Use GRANT & REVOKE
commands to set proper rights as mention below:
1. Give the user Rahul permission only to view records in the table
sales_order and sales_order_details along with an option to further grant
permission on these tables to other users.
CREATE USER rahul IDENTIFIED BY rahul;
Grant select on sales_order, sales_order_details to rahul with admin option;
2. Use the Grant and Revoke Command by giving authority to Other User to
update, delete or insert into Table customer_master.
3. Take back all privileges given to the user Rahul on the table
customer_master.
Revoke all on customer_master from rahul;