Jamia Hamdard
Department of Computer Science &
Engineering
School of Engineering Science & Technology,
New Delhi - 110080
Database Management Systems Lab
(Assignment)
B.Tech. (CSE) A.I.
3rd Year, 5th Sem.
Summitted by: Summitted to:
Sarah Fatima Islam Mr. Syed Ali Mehdi
2020-350-056
CONTENTS
S. Experiment Name Page No. Sign
No.
1. Assignment 1: Getting acquainted with
how to create table, insert data and
specify questionnaires in SQL.
2. Assignment 2: Getting acquainted with the
use of Arithmetical and logical operators
with the data manipulation language.
3. Assignment 3: Getting acquainted with the
use of various constraints / restrictions.
4. Assignment 4: Getting acquainted with
how to alter the table.
5. Assignment 5: Getting acquainted with the
use of various types of joins.
6. Assignment 6: Getting acquainted with the
use of group by and having clause.
7. Assignment 7: Getting acquainted with the
use of sub queries.
8. Assignment 8: Getting acquainted with the
use of indexes.
9. Assignment 9: Getting acquainted with the
use of PL/SQL.
10. Assignment 10: Getting acquainted with
the use of Cursor.
ASSIGNMENT -1
i) Find out the names of all the clients.
ii) Retrieve the list of names and cities of all the clients.
iii) List the various products available from the product_master table.
iv) List all the clients who are located in Bombay.
v) Display the information for client no 0001 and 0002.
vi) Find the products with description as ‘1.44 drive’ and ‘1.22 Drive’.
vii) Find all the products whose sell price is greater than 5000.
viii) Find the list of all clients who stay in in city ‘Bombay’ or city ‘Delhi’ or ‘Madras’.
ix) Find the product whose selling price is greater than 2000 and less than or equal to 5000.
x) List the name, city and state of clients not in the state of ‘Maharashtra’.
ASSIGNMENT -2
i. Change the selling price of ‘1.44 floppy drive to Rs.1150.00
ii. Delete the record with client 0001 from the client master table.
iii. Change the city of client_no’0005’ to Bombay.
iv. Change the bal_due of client_no ‘0001, to 1000.
v. Find the products whose selling price is more than 1500 and also find the new selling price as
original selling price *15.
vi. Find out the clients who stay in a city whose second letter is a.
vii. Find out the name of all clients having ‘a’ as the second letter in their names.
viii. List the products in sorted order of their description.
ix. Count the total number of orders
x. Calculate the average price of all the products.
xi. Calculate the minimum price of products.
xii. Determine the maximum and minimum prices. Rename the tittle as ‘max_price’ and
min_price respectively.
xiii. Count the number of products having price greater than or equal to 1500.
ASSIGNMENT - 03
Create the following tables:
i. Sales_master
ii. Sales_order
TABLE CREATION
create table Sales_master(
Salesman_no varchar2(6) primary key check(Salesman_no like 'S%'),
Sal_name varchar2(20) not null,
Address varchar2(60) not null,
City varchar2(20),
State varchar2(20),
Pincode numeric(6),
Sal_amt numeric(8,2) not null,
Tgt_to_get numeric(6,2) not null,
Ytd_sales numeric(6,2) not null,
Remarks varchar2(20)
);
create table sales_order(
s_order_no varchar2(6) primary key check(s_order_no like 'O%'),
s_order_date date,
client_no varchar2(6) references client_master,
dely_address varchar2(25),
salesman_no varchar2(6) references Sales_master,
dely_type char(1) default 'F',
billed_yn char(1),
dely_date date,
order_status varchar2(15) check ( order_status in('Fulfilled','BackOrder','In Process
','Cancelled')),
check(dely_date>s_order_date));
TABLE DESCRIPTION
i). Desc Sales_master
ii). Desc sales_order
i) DATA:-
Insert into Sales_master values (‘S001’, ‘Kiran’, ‘A/14’, ‘Worli’, ‘Mumbai’, 400002,
‘Maharashtra’, 3000, 100,50,‘Good’);
Insert into Sales_master values (‘S002’,‘Manish’,‘65’,‘Nariman’,‘Mumbai’,
400001,‘Maharashtra’, 3000, 200,100,‘Good’);
Insert into Sales_master values (‘S003’,‘Ravi’,‘P-
7’,‘Bandra’,‘Mumbai’,400032,‘Maharashtra’, 3000, 200,100,‘Good’);
Insert into Sales_master values
(‘S004’,‘Ashish’,‘A/5’,‘Juhu’,‘Mumbai’,400044,‘Maharashtra’, 3000, 200,150,‘Good’);
SELECT* FROM SALES_MASTER
Inserting values in table Sales_order
Insert into Sales_order values (‘O19001’,‘12-01-10’,‘C001’,‘F’,‘N’,‘S001’,‘20-01-10’,‘In Process’);
Insert into Sales_order values (‘O19002’,‘25-01-10’,‘C002’,‘P’,‘N’,‘S002’,‘27-01-10’,‘Cancelled’);
Insert into Sales_order values (‘O46865’,‘18-02-10’,‘C003’,‘F’,‘Y’,‘S003’,‘20-02-10’,‘Fulfilled’);
Insert into Sales_order values (‘O19003’,‘03-04-10’,‘C001’,‘F’,‘Y’,‘S001’,‘07-04-10’,‘Fulfilled’);
Insert into Sales_order values (‘O46866’,‘20-05-10’,‘C004’,‘P’,‘N’,‘S002’,‘22-05-10’,‘Cancelled’);
Insert into Sales_order values (‘O19008’,‘24-05-10’,‘C005’,‘F’,‘N’,‘S004’,‘26-05-10’,‘In Process’);
SELECT * FROM SALES_ORDER
Create table Sales_order_details (
S_order_no Varchar2(6) foreign key references s_order_no of sales_order,
Product_no Varchar2(6) foreign key references product_no of product_master,
Qty_order Number (8),
Qty_disp Number (8),
Product_rate Number(10,2));
Inserting values in table Sales_order_details
Inert into Sales_order_details values (‘O19001’,‘P00001’,4,4,525);
Inert into Sales_order_details values (‘O19001’,‘P07965’,2,1,8400);
Inert into Sales_order_details values (‘O19001’,‘P07885’,2,1,5250);
Inert into Sales_order_details values (‘O19002’,‘P00001’,10,0,525);
Inert into Sales_order_details values (‘O46865’,‘P07868’,3,3,3150);
Inert into Sales_order_details values (‘O46865’,‘P07885’,3,1,5250);
Inert into Sales_order_details values (‘O46865’,‘P00001’,10,10,525);
Inert into Sales_order_details values (‘O46865’,‘P03453’,4,4,1050);
Inert into Sales_order_details values (‘O19003’,‘P03453’,2,2,1050);
Inert into Sales_order_details values (‘O19003’,‘P06734’,1,1,12000);
Inert into Sales_order_details values (‘O46866’,‘P07965’,1,0,8400);
Inert into Sales_order_details values (‘O46866’,‘P07975’,1,0,1050);
Inert into Sales_order_details values (‘O19008’,‘P00001’,10,5,525);
Inert into Sales_order_details values (‘O19008’,‘P07975’,5,3,1050);
Select * from Sales_order_details;
ASSIGNMENT – 04
TABLE
create table challan_header(
challan_no varchar(6) primary key,
s_order_no varchar(6),
challan_date DATE,
billed_yn char (1) DEFAULT 'N');
alter table assiment1.challan_header
add foreign key(s_order_no) REFERENCES
assiment1.sales_order(S_order_no);
DATA
insert into challan_header values ( 'CH9001', 019001, '12-12-20' ,'Y'),
insert into challan_header values ('CH865' ,046865 ,'12-11-20' ,'Y'),
insert into challan_header values ('CH3965' ,010008 ,'12-10-20', 'Y');
TABLE create table challan_details (
challan_no varchar(6) primary key,
product_no varchar(6),
Qty_disp double(4,2) not null);
alter table assiment1. challan_detailsadd foreign key(product_no)
REFERENCES assiment1. challan_details(product_no);
DATA
insert into challan_details values ('CH9001', 'P00001', 4),
insert into challan_details values ('CH9002', 'P07965' ,1),
insert into challan_details values ('CH9003', 'P07885', 1),
insert into challan_details values ('CH6865', 'P07868', 3),
insert into challan_details values ('CH6866' ,'P03453' ,4),
insert into challan_details values ('CH6867', 'P00001' ,10),
insert into challan_details values ('CH3968', 'P00001' ,5),
insert into challan_details values ('CH3969', 'P07975', 2);
Q1. Make the primary key to client_no in client_master
Ans:- alter table client_master add PRIMARY KEY (Client_no);
Q2. Add a new column phone_no in the client_master table.
Ans:- alter table client_master add (phone_no varchar (11));
Q3. Add the not null constraint in the product_master table with the columns description,
profit percent , sell price and cost price.
Ans:- alter table product_master modify description varchar(90) not null;
alter table product_master modify profit_percent double(5,1) not null;
alter table product_master modify sell_price double(20,5) not null;
alter table product_master modify cost_price double(5,0) not null;
Q4.Change the size of client_no field in the client_master table.
Ans:- alter table client_master modify client_no varchar(10);
Q5. Select product_no, description where profit percent is between 2 and 6 both inclusive.
Ans:- Select product_no, description from product_master where profit_percent between 2
and 6 ;
ASSIGNMENT – 05
1.Find out the product which has been sold to ‘Ivan Sayross.’
Ans:-
select product_master.product_no,product_master.description,
sales_or der_details.qty_order from client_master,product_master,
sales_order_details,sales_order
where product_master.product_no=sales_order_details.product_no and
sales_order_details.s_order_no=sales_order.s_order_no and
sales_order.client_no=client_master.client_no and client_master.name like
'%Ivan%'
2.Find out the product and their quantities that will have do delivered.
Ans:-
select product_master.product_no,sales_order_details.Qty_order from
product_master,sales_order_details where
product_master.product_no=sales_order_details.product_no;
3.Find the product_no and description of moving products.
Ans:-
select product_no,description from product_master where description like
'%floppies%' union select product_no,description from product_master where
description like '%Drive%' union select product_no,description from
product_master where description like '%HDD%';
4. Find out the names of clients who have purchased ‘CD DRIVE’
Ans:-
select client_master.name from
client_master,product_master,sales_order_details,sales_o rder where
product_master.product_no=sales_order_details.product_ no and
sales_order_details.s_order_no=sales_order.s_order_no and
sales_order.client_no=client_master.client_no and
product_master.description='CD DRIVE';
5.List the product_no and s_order_no of customers haaving qty ordered less
than 5 from the order details table for the product “1.44 floppies”.
Ans:-
select sales_order_details.product_no,sales_order_details.s_ord er_no from
product_master,sales_order_details where
product_master.description='1.44floppies' and sales_order_details.qty_order
6.Find the products and their quantities for the orders placed by ‘Vandan
Saitwal ’ and “Ivan Bayross”. Ans:-
select product_master.product_no,product_master.description,s
ales_order_details.qty_order from
client_master,product_master,sales_order_details,sales_o rder where
product_master.product_no=sales_order_details.product_ no and
sales_order_details.s_order_no=sales_order.s_order_no and
sales_order.client_no=client_master.client_no and client_master.name like
'%Ivan%';
8. Find the order No, Client No and salesman No. where a client has been
received by more than one salesman.
Ans:-
select s_order_no,client_no,salesman_no from sales_order where client_no in
(select client_no from sales_order group by client_no having
count(salesman_no)>1);
9. Display the s_order_date in the format “dd-mm-yy” e.g. “12- feb-96”
Ans:-
select s_order_date,'YY-MON-DD' from sales_order;
Find the date , 15 days after date.
Ans:- Select s_order_date+15,'DD-MON-YY' from sales_order
ASSIGNMENT 6
Q1.- Print the description and total quantity sold for each product.
select sales_order_details_051.product_no,product_master.description,
sum(sales_order_details_051.qty_order) from
sales_order_details_051, product_master
where product_master.product_no=sales_order_details_051.product_no group by
sales_order_details_051.product_no, product_master.description;
Q2.- Find the value of each product sold.
select s.product_no, p.description, sum(s.qty_disp*s.product_rate) "Sales per Product" from
sales_order_details_051 s, product_master p where
p.product_no=s.product_no group by
s.product_no, p.description;
Q3.- Calculate the average quantity sold for each client that has a maximum order value of 15000.
select c.client_no,c.name,avg(s.qty_disp) "Avg. Sales" from
sales_order_details_051 s,sales_order_051 so,client_master c
where c.client_no=so.client_no and
so.s_order_no=s.s_order_no group by
c.client_no,c.name having
max(s.qty_order*s.product_rate)>1500;
Q4.- Find out the products which has been sold to Ivan.
select product_master.product_no,product_master.description,sales_order_details_051.qty_order
from
client_master, product_master, sales_order_details_051, sales_order_051 where
product_master.product_no=sales_order_details_051.product_no and
sales_order_details_051.s_order_no=sales_order_051.s_order_no and
sales_order_051.client_no=client_master.client_no and
client_master.name like '%Ivan%'
Q5.- Find the names of clients who have ‘CD Drive’.
select client_master.name from
client_master,product_master,sales_order_details_051,sales_order_051 where
product_master.product_no=sales_order_details_051.product_no and
sales_order_details_051.s_order_no=sales_order_051.s_order_no and
sales_order_051.client_no=client_master.client_no and
product_master.description='CD Drive';
Q6.- Find the products and their quantities for the orders placed by ‘Vandana’ and ‘Ivan’.
select product_master.product_no,product_master.description, sales_order_details_051.qty_order
from
client_master,product_master,sales_order_details_051,sales_order_051 where
(product_master.product_no=sales_order_details_051.product_no and
sales_order_details_051.s_order_no=sales_order_051.s_order_no and
sales_order_051.client_no=client_master.client_no and
client_master.name like '%Vandana%' or
product_master.product_no=sales_order_details_051.product_no and
sales_order_details_051.s_order_no=sales_order_051.s_order_no and
sales_order_051.client_no=client_master.client_no and
client_master.name like '%Ivan%');
Q7.- Select product_no, total qty_ordered for each product.
select s.product_no, sum(s.qty_order) from
sales_order_details_051 s, product_master p where
p.product_no=s.product_no group by
s.product_no, p.description;
Q8.- Select product_no, product description and qty ordered for each product.
select s.product_no, p.description, sum(s.qty_order) from
sales_order_details_051 s, product_master p where
p.product_no=s.product_no group by
s.product_no, p.description;
Q9.- Display the order number and day on which clients placed their order.
select s_order_no, s_order_date from sales_order_051;
Q10.- Display the month and Date when the order must be delivered.
select dely_date from sales_order_051;
ASSIGNMENT 7
1.Find the product_no and description of non- moving products.
select sales_order_details_051.product_no, description from
sales_order_details_051, product_master where
qty_order=0 and
sales_order_details_051.product_no=product_master.product_no;
2. Find the customer name, address, city and pincode for the client who has placed order no
“019001”
select name, address1, address2, city, pincode from
client_master, sales_order_051 where
s_order_no='o19001' and
sales_order_051.client_no=client_master.client_no;
3. Find the client names who have placed order before the month of may 96.
select name, sales_order_051.s_order_no, s_order_date from
client_master, sales_order_051, product_master, sales_order_details_051 where
s_order_date < '03/01/1996'and
product_master.product_no=sales_order_details_051.product_no and
sales_order_details_051.s_order_no=sales_order_051.s_order_no and
sales_order_051.client_no=client_master.client_no;
4. Find out if product “1.44 Drive” is ordered by only client and print the client_no name to whom
it was sold.
select description, sales_order_details_051.s_order_no, client_master.client_no, name from
product_master, client_master, sales_order_051, sales_order_details_051 where
description='1.44 Drive' and
product_master.product_no=sales_order_details_051.product_no and
sales_order_details_051.s_order_no=sales_order_051.s_order_no and
sales_order_051.client_no=client_master.client_no;
5. find the names of client who have placed orders worth Rs.10000 or more.
select name, product_master.product_no, sell_price from
client_master, sales_order_051, product_master, sales_order_details_051 where
sell_price >= 10000 and
product_master.product_no=sales_order_details_051.product_no and
sales_order_details_051.s_order_no=sales_order_051.s_order_no and
sales_order_051.client_no=client_master.client_no;
6. Select the orders placed by ‘Rahul Desai”
select name, sales_order_051.s_order_no from
client_master, sales_order_051, product_master, sales_order_details_051 where
name = 'Rahul Desai' and
product_master.product_no=sales_order_details_051.product_no and
sales_order_details_051.s_order_no=sales_order_051.s_order_no and
sales_order_051.client_no=client_master.client_no;
8. Select all the clients and the salesman in the city of Bombay.
select c.name from client_master c where c.city='Bombay'
UNION
select s.sal_name from sales_master_051 s where s.city='Bombay';
9. Select salesman name in “Bombay” who has atleast one client located at “Bombay”
select s.sal_name from sales_master_051 s, sales_order_051 so, client_master c where
so.salseman_no=s.salesman_no and
so.client_no=c.client_no and
c.city='Bombay';
10. Select the product_no, description, qty_on-hand,cost_price of non_moving items in the
product_master table
select sales_order_details_051.product_no, description, qty_on_hand, cost_price from
sales_order_details_051, product_master where
qty_order=0 and
sales_order_details_051.product_no=product_master.product_no;
ASSIGNMENT 8
Q1. Create an index on the table client_master, field client_no.
create index cm_cn on client_master(client_no);
Q2. Create an index on the sales_order, field s_order_no.
create index so_son on sales_order_051(s_order_no);
Q3. Create an composite index on the sales_order_details table for the columns s_order_no and
product_no.
create index son_pn on sales_order_details_051(s_order_no, product_no);
Q4. Create an composite index ch_index on challan_header table for the columns challan no and
s_order_no.
create index cn_son on challan_header_051(challan_no,s_order_no);
Q5. Create an uniQuestion index on the table salesman_master, field salesman_no.
create unique index uni_sn on sales_master_051(salesman_no);
Q6. Drop index ch_index on table challan_header.
drop index ch_index;
Q7. Create view on salesman_master whose sal_amt is less than 3500.
create view view_sal as
select sal_amt
from sales_master_051
where
sal_amt<3500;
select * from view_sal;
Q8. Create a view client_view on client_master and rename the columns as name, add1, add2,
city, pcode, state respectively.
create view client_view as
select name, ADDRESS1 "add1", ADDRESS2 "add2",city, state, PINCODE "pcode"
from client_master
select * from client_view;
Q9. Select the client names from client_view who lives in city ‘Bombay’.
select name from client_view where city='Bombay';
Q10. Drop the view client_view.
drop view client_view;
ASSIGNMENT 9
Q1. WAP in PL/SQL for addition of two numbers.
declare
a number(10);
b number(10);
c number(20);
begin
a:=2;
b:=7;
c:=a+b;
dbms_output.put_line('sum is '||c);
end;
Q2. WAP in PL/SQL for addition of 1 to 100 numbers.
declare
a number;
sums number;
begin
a:=1;
sums:=0;
loop
sums:=sums+a;
exit when (a=100);
a:=a+1;
end loop;
dbms_output.put_line('sum of nos 1 to 100 is '||sums);
end;
Q3. WAP in PL/SQL to check the given number is even or odd.
DECLARE
n NUMBER := 27;
r NUMBER;
BEGIN
r := MOD(n, 2);
IF r = 0 THEN
dbms_output.Put_line('Even');
ELSE
dbms_output.Put_line('Odd');
END IF;
END;
Q4. WAP in PL/SQL to inverse a number, eg. Number 5639 when inverted must be display output
9365.
declare
num number;
reverse number:=0;
begin
num:=5639;
while num>0
loop
reverse:=(reverse*10) + mod(num,10);
num:=trunc(num/10);
end loop;
dbms_output.put_line(' Reversed number is : '|| reverse);
end;
ASSIGNMENT 10
Q) The HRD manager has decided to raise the salary for all the employees in the Dept No 20 by 5%.
Whenever any such raise is given to the employees, a record for the same is maintained in the
emp_raise table. It includes the EmpNo, the date when the raise was given and the actual raise.
Write a PL/SQL block to update the salary of each employee and insert a record in the emp_raise
table.
DECLARE
CURSOR c_cum IS SELECT emp_code, salalry FROM employee
WHERE deptno = 20;
Str_emp_code employee.emp_code%type;
Num_salary employee.salary%type;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO str_emp_code, num_salary;
IF c_emp% FOUND THEN
UPDATE employee SET salary = num_salary + (num_salary * .05)
WHERE emp_code = str_emp_code ;
INSERT INTO emp_raise VALUES(str_emp_code, sysdate, num_salary * 0.05);
ELSE exit;
END IF ;
END LOOP;
COMMIT;
CLOSEc_emp;
END;
Q) Create a transparent audit system for a table Client_master. The system must keep track of the
records that are being deleted or modified and when they have been deleted or modified