Lab Manual of Dbms

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

MCA I Year – II Semester

Data Base Management System Lab

1. Course Objectives

1. Learn SQL queries


2. Learn PL/SQL stored procedures
3. Learn Triggers
4. Learn report generation methods
5. Learn database application creation

2. Syllabus

1. Simple to Complex condition query creation using SQL Plus.


2. Usage of Triggers and Stored Procedures.
3. Creation of Forms for Student information, Library information, Pay roll etc.
4. Writing PL/SQL procedures for data validation.
5. Report generation using SQL reports.
6. Creating password and security features for applications.
7. Usage of File locking, Table locking facilities in applications.
8. Creation of small full- fledged database application spreading over 3 sessions.

3. Course Outcomes

Upon completion of the course, the students will be able to:


1. Write SQL queries
2. Write stored procedures
3. Write triggers
4. Use file locking and table locking facilities
5. Create small full-fledged database application

1
4. Session Plan
S.No Week No Unit As Per
Syllabus Activity
1 1 Unit I Create a table to represent sb-account of a
bank consisting of account-no, customer- name,
balance-amount.
Write a PL/SQL block to implement deposit and
withdraw. Withdraws should not be allowed if the
balance goes below Rs.1000.
2 1 Unit I Create the following two tables College-info, Faculty-info:
College-info consists of fields : college-code, college-
name, address
Faculty-info consists of fields :
college-code, faculty-code,
faculty-name, qualification,
experience-in-no-of-years,
address.
The field college-code is foreign key.
(a) insert atleast 8 records into the
respective tables.
(b) Generate queries to do the following:
i. List all those faculty members
whose experience is greater than
or equal to 10 years and have M.
Tech degree
ii. List all those faculty members, who
have at least 10 years of experience
but do not have M. Tech degree

3 2 Unit I Create the following tables for Library Information


System :
a) Book : (accession-no, title, publisher, author,
status) Status could be issued, present in the library,
sent for binding, and cannot be issued.
b) Write a trigger which sets the status of a book to
"cannot be issued", if it is published 20 years back.

4 2 Unit I Create the following tables for Library Information


System : Book(accession-no, title, publisher, author,
status, date-of-purchase)
Status could be issued, present in the library, sent
for binding, and account be issued.
(a)insert minimum 8 records into the
respective tables with appropriate
validation checks.
(b)Generate queries to do the following:
(i) List all those books which are new arrivals.
The books which are acquired during the last
6 months are categorized as new arrivals
(ii) List all those books that cannot be issued
and purchased 20 years ago

5 3 Unit II Create the following tables :


Student(roll-no, name, date-of-birth, course-id)
Course (Course-id, name, fee, duration)
(a)insert atleast 8 records into the respective
tables.
(b) Generate queries to do the following :
(i) List all those students who are greater than
18 years of age and have opted for MCA course.
(ii) List all those courses whose fee is greater
than that of MCA course.
6 3 Unit II Create the following table:
Item (item-code, item-name, qty-in-stock,
reorder-level)
Supplier (supplier-code, supplier-name,
address)
Can-supply(supplier-code, item-code)
(a) insert atleast 8 records into the
respective tables
(b) Generate queries to do the following:
i. List all those suppliers who can
supply the given item
ii. List all those items which cannot be
supplied by given company
7 4 Unit II Crete the following tables :
Student (roll-no, name, subject-opted)
Subject —rank (subject-code, subject-name,
faculty-code)
Faculty (faculty-code, faculty-name,
specialization)
(a) enter atleast 8 records into the tables.
(b) Generate queries to do the following :
(i) Find the number of students who have
enrolled for the subject "DBMS".
(ii) Find all those subjects which are not offered
by any faculty members.
(iii) Find all those subjects which are offered by
more than one faculty member.
8 4 Unit II Create the following tables:
Student (roll-no, name, subject-opted)
Subject —rank (subject-code, subject-name,
faculty-code, specialization)
Faculty(faculty-code,faculty-name,specialization)
(a) enter atleast 8 records into the tables.
(b) Write PL/SQL procedure to the following :
Set the status of the subject to "not
offered" if the subject is not offered by
any of the faculty members.
9 5 Unit II Create the following table :
Item(item-code,item-name,qty-in-stock,
reorder-level)
Supplier(supplier-code, supplier-name,
address,status)
Can-supply(supplier-code, item-code)
(a) enter atleast 8 records into the tables.
(b) Write PL/SQL procedure to do the
following:
Set the status of the supplier to “important” if
the supplier can supply more than five items.
10 5 Unit II Create the following tables :
Student(roll-no,name,date-of-birth,course-
id)
Course(course-id,name,fee,duration,status)
(a) enter atleast 8 records into the tables.
(b) Write PL/SQL procedure to do the
following:
Set the status of course to "offered" in
which the number of candidates is at
least 10 otherwise set it to "not offered"
11 6 Unit II Create the following tables:
Student(roll-no,name,date-of-birth,course-
id)
Course(Course-id,name,fee,duration)
(a)enter atleast 8 records into the tables.
(b) Generate queries to:
(i) List all those students who are between
18-19 years of age and have opted for MCA
course
(ii)List all those courses in which number of
students are less than 10.

12 6 Unit II Create the following tables:


Branch(branch-id,branch-name,branch-city)
Customer(customer-id,customer-name,
customer-city,branch-id)
(a) enter atleast 8 records into the tables.
(b) Generate queries to do the following:
i) List all those customers who live in
the same city as the branch in which
they have account.
ii) List all those customers who have an
account in more than one branch

13 7 Unit II Create the following tables:


Book(accession-no,title,publisher,
year, date-of-purchase, status)
Book-Place(accession-no, rack-id,
rack-position)
Member(member-id,name,
number-of-books-issued, max-
limit, status)
Book –issue(accession-no, member-
id, date-of-issue)
(a)enter atleast 8 records into the
tables.
b) Write a PL/SQL procedure to
issue the book.
Write a trigger to set the status of
a book neither to "lost" which is
neither issued nor in the library.
14 7 Unit II Create the following tables:
Book(accession-no,title,publisher,year,date-
of-purchase,status)
Member(member-id,name,number-of-books-
issued, max-limit,status)
Book-issue(accession-no, member-id, date-
of-issue)
(a) enter atleast 8 records into the tables.
(b) Write a PLSQL procedure to issue the book.
Write a trigger to set the status of students
to "back listed" if they have taken book but
not returned even after one year.
15 8 Unit III a) WAP in PL/SQL for addition of two numbers
b) WAP in PL/SQL to check the given number is
even or odd.

16 8 Unit III Create the following tables:


client_master(client_no, name, address 1,
address2, city, state, pincode, bal_due)
Product_master(Product no, Description,
Profit_percent,Unit measure, Qty_on_hand,
Reoder_lvl, Sell price, Cost_price)
Insert the atleast 8 records into respective
tables:

On the basis of above two tables answer the


following Questionries:
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’.

17 9 Unit III Create the following tables:


client_master(client no, name, address1,
address2, city, state, pincode)
Product_master(Product no, Description,
Profit_percent,Unit measure,Qty_on_hand,
Reoder lvl, Sell_price, Cost_price)
Insert the atleast 8 records into respective
tables:
On the basis of above two tables answer the
following Questionries:
a)Find the products with description as
'1.44 drive' and '1.22 Drive'.
b)Find all the products whose sell price is
greater then 5000.
c)Find the list of all clients who stay in in city
`Bombay' or city 'Delhi' or `Madras'.
d)Find the product whose selling price is greater
than 2000 and less than or equal to 5000.
e)List the name, city and state of clients not
in the state of 'Maharashtra'.
18 9 Unit III Create the following tables:
Client_master(client no, name, address1,
address2, city, state, pincode)
Product_master(Product no, Description,
Profit_percent, Unit measure,Qty_on_hand,
Reoder lvl, Sell_price, Cost_price)
iii) Insert the atleast 8 records into
respective tables:
iv) On the basis of above two tables
answer the following Questionries:
a)Change the selling price of '1.44 floppy drive to
Rs.1150.00
b)Delete the record with client 0001 from the client
master table.
c)Change the city of client_no'0005' to Bombay.
d)Change the bal_due of client_no '0001, to
1000.
e)Find the products whose selling price is
more than 1500 and also find the new selling
price as original selling price *15.
f)Find out the clients who stay in a city
whose second letter is a.
g)Find out the name of all clients having 'a'
as the second letter in their names.
h) List the products in sorted order of their
description.
19 10 Unit III Create the following tables:
client_master(client no, name, address1,
address2, city, state, pincode)
Product_master(Product no, Description,
Profit_percent, Unit measure,Qty_on_hand,
Reoder lvl, Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective
tables:
iv) On the basis of above two tables answer the
following Questionries:
a)Count the total number of orders.
b)Calculate the average price of all the products.
c)Calculate the minimum price of products.
d)Determine the maximum and minimum prices.
e) Rename the tittle as `max_price' and min_price
respectively.
20 10 Unit IV Create the following tables:
client_master(client no, name, address1,
address2, city, state, pincode)
Product_master(Product no, Description,
Profit_percent, Unit measure,Qty_on_hand,
Reoder lvl, Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective
tables:
iii) On the basis of above two tables answer
the following Questionries:
1. Find out the product which has been sold
to 'Ivan Sayross.'
2. Find out the product and their quantities that
will have do delivered.
3. Find the product_no and description of moving
products.
4. Find out the names of clients who have
purchased 'CD DRIVE'.
5. List the product_no and re_order_no of
customers having.
qty ordered less than 5 f m the order details table
for the product "1.44 floppies".
Count the number of products having price
greater than or equal to 1500
21 11 Unit IV Create the following tables:
client_master(client no, name, address1,
address2, city, state, pincode)
Product_master(Product no, Description,
Profit_percent, Unit measure,Qty_on_hand,
Reoder lvl, Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective
tables:
On the basis of above two tables answer the
following Questionries:
1 Find the products and their quantities for the
orders placed by Wandan Saitwal ' and "Ivan
Bayross".
2. . Find the products and their quantities for
the orders placed by client_no " C00001" and
"C00002".
3. Find the order No„ Client No and salesman
No. where client has been received by more
than one salesman.
4. Display the s_order_date in the format "dd-
mm-yy" e.g. "12- feb-96".
5. Find the date , 15 days after date.
22 11 Unit IV Create the following tables:
client_master(client no, name, address1,
address2, city, state, pincode)
Product_master(Product no, Description,
Profit_percent, Unit measure,Qty_on_hand,
Reoder lvl, Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective
tables:
On the basis of above two tables answer the
following Questionries:
1)Print the description and total quantity sold for
each product.
2)Find the value of each product sold.
3)Calculate the average quantity sold for each
client that has a maximum order
value of 15000.
4) Find out the products which has been sold to
Ivan.
5) Find the names of clients who have 'CD Drive'
23 12 Unit IV Create the following tables:
client_master(client no, name, address1,
address2, city, state, pincode)
Product_master(Product no, Description,
Profit_percent, Unit measure,Qty_on_hand,
Reoder lvl, Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective
tables:
On the basis of above two tables answer the
following Questionries:
1)Find the products and their
quantities for the orders placed
by `Vandana' and `Ivan'.
2)Select product_no, total qty ordered for each
product.
3) Select product_no, product description and qty
ordered for each product.
4)Display the order number and day on which
clients placed their order.
5) Display the month and Date when the order
must be delivered.
24 12 Unit IV
Student (roll-no, name, subject-opted)

Subject -rank (subject-code, subject-name, faculty-


code, specialization)

Faculty (faculty-code, faculty-name, specialization)

(a)Create a form to accept the data from the user


with appropriate checks.

(b)Write PL/SQL procedure to the following:

Set the status of the subject to "not offered" if the


subject is not opted by at least 5 students.
25 13 Unit V Create the following tables:
Student(roll-no,name,date-of-birth,course-id)
Course (Course-id, name, fee, duration, status)
(a)Create a form to accept the data from the user
with appropriate checks.
(b)Write PL/SQL procedure to the following:
Set the status of course to “offered” in which the
number of candidates are at least 10 otherwise not
offered.
26 13 Unit V Create the following tables for Library Information
System:
Book(accession-no, title, publisher, author, status,
date-of-purchase)
Status could be issued, present in the library, sent
for binding, and account be issued.
(a)Create a form to accept the data from the user
Create a form to accept the data from the user with
appropriate validation checks.
(b)Generate queries to do the following:
i) List all those books which are new arrivals. The
books which are acquired during the last 6 months
are categorized as new arrivals.
ii) List all those books that cannot be issued and
purchased 20 years ago.
27 14 Unit V WAP in PL/SQL code to display multiplication
tables from 5 to 10.
WAP in PL/SQL to check the given number is
palindrome or not.
28 14 Unit V Create the following tables:
Student(roll- no,name,date-of-birth, course-id)
Course(Course- id,name,fee,duration)
(a)insert atleast 8 records into the respective
tables.
(b) Generate queries to do the following:
(i) List all those students who are greater than 18
years of age and have opted for ME course.
(ii) List all those courses whose fee is less than
that of MCA course.

29 15 Unit V Create the following tables:


Book(accessionno, title, publisher, year, date-of-purchase,
status)
Member(member-id,name, number-of-books-
issued, max- limit,status)
Book-issue(accession-no, member-id, date-of-
issue)
(a) insert atleast 8 records into the respective
tables
(b) Write a PUSQL procedure to issue the
book.
Write a trigger to set the status of
students to "not returned" if they have
taken book but not returned even after
one year.
30 15 Unit V Create the following tables:
Branch (branch-id, branch-name, branch-city)
Customer (customer-id, customer-name,
customer-city, branch-id)
(a) insert atleast 8 records into the respective
tables.
(b) Generate queries to do the following:
(i) List all those customers who live in
the same city as the branch in which
they have account.
(ii) List all those customers who have an
account in only one branch

List of Textbooks

1. Abraham Silberschatz, Henry F Korth, S Sudarshan, Database System Concepts,


McGraw-Hill International Edition, 6th Edition, 2010.
2. Ramakrishnan, Gehrke, Database Management Systems, McGraw-Hill International
Edition, 3rd Edition, 2003.
3. Elmasri, Navathe, Somayajulu, Fundamentals of Database Systems, Pearson
Education,4thEdition,2004.
4. Shashank Tiwari, “Professional NoSQL”, 1st Edition , Wiley publishers, 2011.

Websites

https://www.javapoint.com/dbms-tutorial

https://www.tutorialspoint.com/dbms/index.htm

https://www.geeksforgeeks.org/dbms

http://www.guru99.com/dbms-tutorial.html

https://www.studytonight.com/dbms
5. Equipment required

Hardware
No. of System : 60(IBM)
Processor : PIV™ 1.67 GHz
RAM : 512 MB
Hard Disk : 40 GB
Optical
Mouse : Mouse
Network Interface card : Present
Software
Operating System : Window XP
Oracle
database
Software : 10G
6.
7. Code of Conduct

 Students should report to the concerned lab as per the time table.
 Students should be present in the labs for total scheduled duration.
 Students who turn up late to the labs will in no case be permitted to do
the program scheduled for the day.
 After completion of the program, certification of the concerned staff in-
charge in the observation book is necessary.
 Student should bring a notebook of 100 pages and should enter the
readings/observations into the notebook while performing the
experiment.
 The record of observations along with the detailed experimental
procedure of the experiment in the immediate last session should be
submitted and certified by the staff member in-charge.
 Not more than 2-students in a group are permitted to perform the
experiment on the set.
 Any damage caused to the systems / equipment will be viewed seriously

and may lead to imposition of penalty or dismissal of the total group of

students from the lab for the entire semester/year.


7. List of Exercises
Ex.No.1 CREATION OF TABLES AND IMPLEMENT PL/SQL PROGRAM

AIM: Create a table to represent sb-account of a bank consisting of account-no, customer-name,


balance-amount.
Write a PL/SQL block to implement deposit and withdraw. Withdraws should
not be allowed if the balance goes below Rs.1000.

PROGRAM

SQL> create table sb_account (account_no number (5) primary key, customer_name
varchar2(20), balance_amount number(6));

Table created.

Sql> insert into sb_account values(101,’Narasimha’,2000);

1 row inserted

Sql> insert into sb_account values(102,’veena’,3000);

1 row inserted

Sql> insert into sb_account values(103,’pradeep’,5000);

1 row inserted

Sql> insert into sb_account values(104,’sireesha’,600);

1 row inserted

Sql> insert into sb_account values(105,’sudhir’,800);

1 row inserted

PL/SQL PROCEDURE FOR WITHDRAW AN AMOUNT:


SQL> Declare

ano sb_account.account_no%type;

balance sb_account.balance_amount%type;

withdraw number(5);

begin

withdraw:=&withdraw; 7 ano:=&account_no;
select balance_amount into balance from sb_account where 9 account_no=ano;

dbms_output.put_line('balance='||balance);
if(balance<1000) then

dbms_output.put_line('withdraw fails');

end if;

if(withdraw>balance) then

dbms_output.put_line('withdraw fails');

else

update sb_account set balance_amount=balance_amount-withdraw where

account_no=ano;

end if;

end;

/
Enter value for withdraw: 100 Enter value for

account_no: 101 balance=2000

PL/SQL procedure successfully completed.

PL/SQL BLOCK FOR DEPOSIT SOME AMOUNT:


SQL> Declare

deposit number(5);

ano sb_account.account_no%type;

begin

Deposit:=&deposit;

ano:=&account_no;

update sb_account set balance_amount=balance_amount+deposit where account_no=ano;

end;
/

Enter value for deposit: 500 Enter value for account_no:

104

PL/SQL procedure successfully completed.


Ex.No.2 CREATION OF TABLES AND APPLYING SIMPLW QUERIES

AIM: Create the following two tables College-info, Faculty-info:


College-info consists of fields : college-code, college-name, address
Faculty-info consists of fields : college-code, faculty-code, faculty-name, qualification,
experience-in-no-of-years, address.
The field college-code is foreign key.
(a) insert atleast 8 records into the espective tables.
(b) Generate queries to do the following :
(i)List all those faculty members whose experience is greater than or equal to 10
years and have M. Tech degree.
(ii) List all those faculty members, who have at least 10 years of experience
but do not have M. Tech degree

PROGRAM

SQL> create table coll_info(coll_code number(3) primary key, coll_name


varchar2(15),address varchar2(30));

Table Created

SQL> insert into coll_info

values(10,’AURR’,’HYD’); 1 row created

SQL> insert into coll_info

values(11,’AURI’,’HYD’); 1 row created

SQL> insert into coll_info

values(12,’AURO’,’DSNR’); 1 row created

SQL> insert into coll_info

values(13,’AURM’,’DSNR’); 1 row created

SQL> insert into coll_info

values(14,’AURB’,’Bhongir’); 1 row created

SQL> select * from coll_info;

Coll_code Coll_nam Address


e
10 AURR HYD
11 AURI HYD
12 AURO DSNR
13 AURM DSNR
14 AURB Bhongir

SQL> create table faculty_info(coll_code number(3) , fcode number(3), fname


varchar2(15), qualification varchar2(15),experience number(2),address varchar2(30));

(constraint fac foreign key(coll_code) references

coll_code(coll_code) Table Created

SQL> insert into faculty_info

values(10,101,’A’,’B.tech’,11,’Hyd’); 1 row inserted

SQL> insert into faculty_info

values(11,110,’B’,’M.tech’,12,’Hyd’); 1 row inserted

SQL> insert into faculty_info

values(12,121,’C’,’M.tech’,5,’Hyd’); 1 row inserted

SQL> insert into faculty_info

values(13,123,’D’,’M.tech’,14,’Hyd’); 1 row inserted

SQL> insert into faculty_info

values(14,114,’E’,’B.tech’,10,’Hyd’); 1 row inserted

SQL> select * from faculty_info;

Coll_code Fcode Fname qualification experience Address


10 101 A B.tech 11 HYD
11 110 B M.tech 12 HYD
12 121 C M.tech 5 HYD
13 123 D B.tech 14 HYD
14 114 E B.tech 10 HYD

Generate queries to do following:

(i) List all those faculty members whose experience is greater than or equal to 10 years and
have M.Tech degree.

SQL>select fname from faculty_info where qualification=’M.Tech’ and experience>=10;

Fname
B
(ii) List all those faculty members, who have at least 10 years of experience but do not
have M.Tech degree.

SQL>select fname from faculty_info where qualification!=’M.Tech’ and experience>=10;

Fname
A
B
E
Ex.No.3 CREATION OF LIBRARY INFORMATION SYSTEM TABLE

AIM: Create the following tables for Library Information System :


a) Book : (accession-no, title, publisher, author, status)
Status could be issued, present in the library, sent for binding, and cannot be issued.
b) Write a trigger which sets the status of a book to "cannot be issued", if it is published 20 years
back.

PROGRAM
SQL>create table book(accno number(3),title varchar2(10),publisher varchar2(15),
author varchar2(10),status varchar2(30),dateofpurchase date);

Table Created

SQL> insert into book values(100,'C','AB publishers','Narsimha','issued','30-APR-08');

1 row inserted

SQL> insert into book values(200,'CPP',' AB publishers ','Ramesh','present in


library','14-DEC-05');

1 row inserted

SQL> insert into book values(300,'java','CM publications','Sundeep','sent for


binding','11-JUL-10');

1 row inserted

SQL> insert into book values(400,'DBMS',' CM publications','Dheeraj', 'present


in library','08-JUN-91');

1 row inserted

SQL> insert into book values(500,'C&DS',' CM publications ','Sai','present


in library','28-JAN-92');

1 row inserted

SQL>select * from book;

ACCNO TITLE PUBLISHER AUTHOR STATUS DATEOFPURCHASE


100 C AB publishers Narsimha Issued 30-APR-08
200 Cpp AB publishers Ramesh Present in library 14-DEC-05
300 Java CM publications Sundeep Sent for binding 11-JUL-10
400 Dbms CM publications Dheeraj Present in library 08-JUN-91
500 C&ds CM publications Sai Present in library 28-JAN-92
a) Write a trigger which sets the status of a book to "cannot be issued", if it

is published 20 years back.

SQL> ed trg.sql;

create or replace trigger

book_trig before insert or update

on book for each row

begin

if (sysdate-dateofpurchase)/365>=20 then

:new.status:='cannot be issued';

end if;

end;

Trigger Created

SQL>select * from

book;

ACCNO TITLE PUBLISHER AUTHOR STATUS DATEOFPURCHASE


100 C AB publishers Narsimha Issued 30-APR-08
200 Cpp AB publishers Ramesh Present in library 14-DEC-05
300 Java CM publications Sundeep Sent for binding 11-JUL-10
400 Dbms CM publications Dheeraj Cannot be issued 08-JUN-91
500 C&ds CM publications Sai Cannot be issued 28-JAN-92
Ex.No.4 CREATION OF TABLE AND GENERATE QUERIES

AIM: Create the following tables for Library Information System :

Book(accession-no, title, publisher, author, status, date-of-purchase)

Status could be issued, present in the library, sent for binding, and account be issued
(a)insert minimum 8 records into the respective tables
with appropriate validation checks.
(b) Generate queries to do the following :
(i) List all those books which are new arrivals. The books which are acquired during
the last 6 months are categorized as new arrivals.
(ii) List all those books that cannot be issued and purchased 20 years ago.

PROGRAM
SQL>create table book(accno number(3),title

varchar2(10),publisher varchar2(15), author varchar2(10),status

varchar2(30),dateofpurchase date);

Table Created

SQL> insert into book values(100,'C','AB publishers','Narsimha','issued','30-


APR-08');

1 row inserted

SQL> insert into book values(200,'CPP',' AB publishers


','Ramesh','present in library','14-DEC-01');

1 row inserted

SQL> insert into book values(300,'java','CM


publications','Sundeep','sent for binding','11-JUL-21');

1 row inserted

SQL> insert into book values(400,'DBMS',' CM


publications','Dheeraj', 'cannot be issued','08-JUN-91');

1 row inserted

SQL> insert into book values(500,'C&DS',' CM publications


','Sai',' cannot be issued ','28-JAN-06');

1 row inserted

SQL>select * from book;

ACCNO TITLE PUBLISHER AUTHOR STATUS DATEOFPURCHASE


100 C AB publishers Narsimha Issued 30-APR-08
200 Cpp AB publishers Ramesh Present in library 14-DEC-01
300 Java CM publications Sundeep Sent for binding 11-JUL-21
400 Dbms CM publications Dheeraj cannot be issued 08-JUN-91
500 C&ds CM publications Sai cannot be issued 28-JAN-06

(b) Generate queries to do the following:

(i) List all those books which are new arrivals.


The books which are acquired during the
last 6 months are categorized as new
arrivals.

SQL>select * from book where (sysdate-dateofpurchase)/30<=6;

ACCNO TITLE PUBLISHER AUTHOR STATUS DATEOFPURCHASE


300 Java CM publications Sundeep Sent for binding 11-JUL-21

(ii) List all those books that cannot be issued and purchased 20 years ago.

SQL> select * from book where status=’cannot be


issued’ and (sysdate- dateofpurchase)/365>=20;

ACCNO TITLE PUBLISHER AUTHOR STATUS DATEOFPURCHASE


400 Dbms CM publications Dheeraj cannot be issued 08-JUN-91
Ex.No.5 CREATION OF STUDENT AND COURSE TABLES

AIM: Create the following tables :


Student(roll-no, name, date-of-birth, course-id)
course (Course-id, name, fee, duration)
(a) insert atleast 8 records into the respective tables.
(b) Generate queries to do the following :
(i) List all those students who are greater than 18 years of age and have
opted for MCA course.
(ii) List all those courses whose fee is greater than that of MCA course.

PROGRAM
SQL>create table student(roll_no number(2),name
varchar2(20),dob date,course_id number(3));

Table Created

SQL> insert into student values(1,’Vignesh’,’15-MAR-90’,101);

1 row inserted

SQL> insert into student values(2,’Ramesh’,’17-APR-90’,101);

1 row inserted

SQL> insert into student values(3,’Naveen’,’21-MAY-91’,102);

1 row inserted

SQL> insert into student values(4,’Ashok’,’01-JAN-91’,103);

1 row inserted

SQL> insert into student values(5,’Sandeep’,’23-AUG-90’,104);

1 row inserted

SQL>Select * from student;

Roll_no name dob Course_id


1 Vignesh 15-MAR-90 101
2 Ramesh 17-APR-90 101
3 Naveen 21-MAY-91 102
4 Ashok 01-JAN-91 103
5 Sandeep 23-AUG-90 104

SQL>create table course(course_id number(3), cname


varchar2(20),fee number(5),duration number(1));

Table Created

SQL> insert into course values(101,’MCA’,30000,3);

1 row inserted

SQL> insert into course values(102,’M.Tech’,35000,2);

1 row inserted

SQL> insert into course values(103,’MBA’,33000,2);


1 row inserted

SQL> insert into course values(104,’B.Tech’,27000,4);

1 row inserted

SQL>Select * from course;

Course_id Cname fee duration


101 MCA 30000 3
102 M.Tech 35000 2
103 MBA 33000 2
104 B.Tech 27000 4

(b) Generate queries to do the following :

(i) List all those students who are greater than 18 years of

age and have opted for MCA course.

SQL>select s.roll_no,s.name,s.dob,c.cname "Course" from student s,course c


where s.course_id=c.course_id and (sysdate-dob)/365>18 and c.cname='MCA';

SQL>select roll_no,name,dob,cname from student,course where


student.course_id=course.course_id and (sysdate-dob)/365>18 and
cname='MCA';

Roll_no name dob Cname


1 Vignesh 15-MAR-90 MCA
2 ramesh 17-APR-90 MCA

(ii) List all those courses whose fee is greater than that of MCA course.

SQL> select cname "Course",fee from course where fee>(select


fee from course where name='MCA');
SQL>select cname,fee from course where fee>(cname=’MCA’);

cname Fee
M.Tech 35000
MBA 33000

15
Ex.No.6 CREATION OF TABLES AND WITH NESTED QUERIES

AIM: Create the following table :


Item (item-code, item-name, qty-in-stock, reorder-level) Supplier (supplier-
code, supplier-name, address)
Can-supply(supplier-code, item-code)
(a) insert atleast 8 records into the respective tables
(b) Generate queries to do the following :
(i) List all those suppliers who can supply the given item.
(ii) List all those items which cannot be supplied by given company

PROGRAM

SQL> create table item(icode number(3),iname varchar2(10),stock number(3),reorder


number(3));

Table Created

SQL> insert into item

values(1,’A’,10,5); 1 row created

SQL> insert into item

values(2,’B’,50,60); 1 row created

SQL> insert into item

values(3,’C’,150,100); 1 row created

SQL> insert into item

values(4,’D’,100,100); 1 row created

SQL> insert into item

values(5,’E’,80,60); 1 row created

SQL> insert into item

values(6,’F’,95,50); 1 row created

SQL> insert into item

values(7,’G’,70,70); 1 row created

SQL> insert into item

values(8,’H’,65,0); 1 row created

SQL> insert into item

values(9,’I’,100,90); 1 row created

SQL> insert into item


values(10,’J’,110,100); 1 row created

SQL>Select * from item;

1 A 10 5
2 B 50 60
3 C 150 100
4 D 100 100
5 icode iname stock reorder
6 F 95 50
7 G 70 70
8 H 65 0
9 I 100 90
10 J 110 100

SQL> create table supplier(scode number(3),sname varchar2(10),address


varchar2(10));

Table Created

SQL> insert into supplier

values(11,’AA’,’Hyd’); 1 row created

SQL> insert into supplier

values(12,’BB’,’Sec’); 1 row created

SQL> insert into supplier

values(13,’CC’,’Upl’); 1 row created

SQL> insert into supplier

values(14,’DD’,’Hyd’); 1 row created

SQL> insert into supplier

values(15,’EE’,’Sec’); 1 row created

SQL>Select * from suplier;

scode sname address


11 AA Hyd
12 BB Sec
13 CC Upl
14 DD Hyd
15 EE Sec
SQL> create table cansupply(scode number(3), icode

number(3)); Table Created

SQL> insert into cansupply

values(11,1); 1 row created

SQL> insert into cansupply

values(11,2); 1 row created

SQL> insert into cansupply

values(11,3); 1 row created

SQL> insert into cansupply

values(11,4); 1 row created

SQL> insert into cansupply

values(11,5); 1 row created

SQL> insert into cansupply

values(11,6); 1 row created

SQL> insert into cansupply

values(12,7); 1 row created

SQL> insert into cansupply

values(13,6); 1 row created

SQL> insert into cansupply

values(13,8); 1 row created

SQL> insert into cansupply

values(14,9); 1 row created

SQL> insert into cansupply

values(15,10); 1 row created

SQL> insert into cansupply

values(15,4); 1 row created


SQL>Select * from cansupply;

11
scode 1
icode
11 2
11 3
11 4
11 5
11 6
12 7
13 6
13 8
14 9
15 10
15 4

(a)Generate queries to do the following :

(i) List all those suppliers who can supply the given item.

SQL> select sname from supplier where scode=(select scode from cansupply where icode=(select
icode from item where iname=a));

Sname
AA

(ii)List all those items which cannot be supplied by given company.

SQL> select iname from item where icode!=(select icode from cansupply where scode=(select
scode from supplier where sname=cc));

Iname
F
H
Ex.No.7 CREATION OF STUDENT TABLE WITH SUB QUERIES

AIM: Crete the following tables :


Student (roll-no, name, subject-opted)
Subject —rank (subject-code, subject-name, faculty-code)
Faculty (faculty-code, faculty-name, specialization)
(a) enter atleast 8 records into the tables.
(b) Generate queries to do the following :
(i) Find the number of students who have enrolled for the subject "DBMS"
(ii) Find all those subjects which are not offered by any faculty members.
(iii) Find all those subjects which are offered by more than one faculty member.

PROGRAM
SQL>create table student(roll_no number(2),name varchar2(20),subject-name
varchar2(20));

Table Created

SQL> insert into student values(1,’Rajesh’,’SE’);

1 row inserted

SQL> insert into student values(2,’Ashok’,’DBMS’);

1 row inserted

SQL> insert into student values(3,’Sharma’,’JAVA’);

1 row inserted

SQL> insert into student values(4,’Naveen’,’DBMS’);

1 row inserted

SQL> insert into student values(5,’Raju’,’DAA’);

1 row inserted

SQL>Select * from student;

Roll_no name Subject-name


1 Rajesh SE
2 Ashok DBMS
3 Sharma JAVA
4 Naveen DBMS
5 Raju DAA
SQL>create table subject(fcode number(2),fname varchar2(20),specialization
varchar2(20));

Table Created

SQL> insert into subject values(11,’Sai’,’SE’);

1 row inserted

SQL> insert into subject values(12,’Ramu’,’DBMS’);

1 row inserted

SQL> insert into subject values(13,’Narsimha’,’java’);

1 row inserted

SQL> insert into subject values(14,’Vignesh’,’DAA’);

1 row inserted

SQL> insert into subject values(15,’Ramesh’,’CPP’);

1 row inserted

SQL>Select * from subject;

Fcode fname Specialization


11 Sai SE
12 Ramu DBMS
13 Narsimha JAVA
14 Vignesh DAA
15 Ramesh CPP

(a) Generate queries to do the following :

(i) Find the number of students who have enrolled for the subject "DBMS".

SQL>select count(*) “No of Students” from students where subject=’DBMS’;

No of students
2
(ii) Find all those faculty members who have not offered any subject.

SQL> select fname,specialization,name from subject,student where


subject.specialization(+)=student.subject-name;

Fname Specialization Name


Ramesh CPP
Sai SE Rajesh
Ramu DBMS Ashok
Narsimh JAVA Sharma
A
Ramu DBMS Naveen
Vignesh DAA Raju
Ex.No.8 CREATION OF STUDENT AND COURSE TABLE USING PRIMARY KEY

AIM: Create the following tables :student(roll-no, name, date-of-birth, course-id)


course (course-id, name, fee, duration, status)
Write PL/SQL procedure to do the following :
set the status of course to "not offered" in which the number of candidates is less than 5.

SQL > CREATE TABLE student(roll char(10) PRIMARY KEY , dob date, name varchar2(10), courseid char(10));
table created.
SQL > CREATE TABLE course( courseid char(10) PRIMARY KEY , name varchar2(10),fee
number,2 duration number status varchar2(10));
table created.
insert into course values ('c001','mca',75000,3,'');
insert into course values ('m003','mba',200000,2,'');
insert into student values ('04808015', '07-sep-1990','suchitra','c001');
insert into student values ('04708015', '07-sep-1990','radhika','c001');
insert into student values ('04708012','12-jan-1988','hemasri','c001');
insert into student values ('04807010','26-dec-1990','jayasri','c001');
insert into student values ('04708016','07-jul-1985','radha','c001');
insert into student values ('04708013','12-jun-1988','hemalatha','c001');
insert into student values ('04807011','26-dec-1990','vasuda','c001');
insert into student values ('04808018','06-nov-1978','sadhana','c001');
insert into student values ('04708019','07-oct-1985','ragini','c001');
insert into student values ('04708020','21-feb-1988','anmol','m003');
insert into student values ('04807021','20-dec-1990','teja','m003');
insert into student values('0460909','17-jul-1990','kareena','c001');

(b) PL/SQL procedure to set the status of course to ‘not offered’ in which the number of candidates is
lessthan .

SQL > edit status.SQL ;


Declare
id char(10);
num number;
cursor course_cr is SELECT * from course;
course_val course_cr%rowtype;
begin
open course_cr;
loop
fetch course_cr into course_val;
exit
when course_cr%notfound;
id:=course_val.courseid;
SELECT count(*) into num from student where courseid=id;
where courseid=id;
if num < 3 then update course set status='not-off' where courseid=id;
end if;
if num >= 3 then update course set status='offered' where courseid=id;
end if;
end loop;
close course_cr;

end;

SQL > SELECT * from course;


courseid name fee duration status
c001 mca 75000 3
m003 mba 200000 2

SQL > start status.SQL ;


31 /pl/SQL procedure successfully completed.

SQL > SELECT * from course;

courseid name fee duration status


c001 Mca 75000 3 offered
m003 Mba 200000 2 not-off
Ex.No.9 CREATION TABLE USING PROCEDURE

AIM: Create the following table :


Item(item-code,item-name,qty-in-stock, reorder-level),
Supplier(supplier-code, supplier-name,address,status)
Can-supply(supplier-code, item-code)
(a) enter atleast 8 records into the tables.
(b) Write PL/SQL procedure to do the following :
Set the status of the supplier to "important" if the supplier can supply More
than five items.

PROGRAM

SQL> create table item(icode number(3),iname varchar2(10),stock number(3),reorder


number(3));

Table Created

SQL> insert into item

values(1,’A’,10,5); 1 row created

SQL> insert into item

values(2,’B’,50,60); 1 row created

SQL> insert into item

values(3,’C’,150,100); 1 row created

SQL> insert into item

values(4,’D’,100,100); 1 row created

SQL> insert into item

values(5,’E’,80,60); 1 row created

SQL> insert into item

values(6,’F’,95,50); 1 row created

SQL> insert into item

values(7,’G’,70,70); 1 row created

SQL> insert into item

values(8,’H’,65,0); 1 row created

SQL> insert into item

values(9,’I’,100,90); 1 row created


SQL> insert into item

values(10,’J’,110,100); 1 row created

SQL>Select * from item;

1 A 10 5
2 B 50 60
3 C 150 100
4 D 100 100
5 icode iname Stock reorder
6 F 95 50
7 G 70 70
8 H 65 0
9 I 100 90
10 J 110 100

SQL> create table supplier(scode number(3),sname varchar2(10),address


varchar2(10));

Table Created

SQL> insert into supplier

values(11,’AA’,’Hyd’); 1 row created

SQL> insert into supplier

values(12,’BB’,’Sec’); 1 row created

SQL> insert into supplier

values(13,’CC’,’Upl’); 1 row created

SQL> insert into supplier

values(14,’DD’,’Hyd’); 1 row created

SQL> insert into supplier

values(15,’EE’,’Sec’); 1 row created

SQL>Select * from suplier;

Scode sname address


11 AA Hyd
12 BB Sec
13 CC Upl
14 DD Hyd
15 EE Sec
SQL> create table cansupply(scode number(3), icode

number(3)); Table Created

SQL> insert into cansupply

values(11,1); 1 row created

SQL> insert into cansupply

values(11,2); 1 row created

SQL> insert into cansupply

values(11,3); 1 row created

SQL> insert into cansupply

values(11,4); 1 row created

SQL> insert into cansupply

values(11,5); 1 row created

SQL> insert into cansupply

values(11,6); 1 row created

SQL> insert into cansupply

values(12,7); 1 row created

SQL> insert into cansupply

values(13,6); 1 row created

SQL> insert into cansupply

values(13,8); 1 row created

SQL> insert into cansupply

values(14,9); 1 row created

SQL> insert into cansupply

values(15,10); 1 row created

SQL> insert into cansupply

values(15,4); 1 row created


SQL>Select * from cansupply;

11
scode 1
icode
11 2
11 3
11 4
11 5
11 6
12 7
13 6
13 8
14 9
15 10
15 4

(b) Write PL/SQL procedure to do the following :


Set the status of the supplier to "important" if the supplier can supply More than five
items.
PL/SQL procedure:
CREATE OR REPLACE procedure pr22 AS
CURSOR c1 is SELECT distinct S.* from supplier S,can_supply C WHERE C.s_code=S.s_code and 4<=(SELECT count(*) from can_supply C
where S.s_code=C.s_code);
BEGIN
for i in c1
loop
UPDATE supplier set status='important' WHERE s_code=i.s_code;
end loop;
dbms_output.put_line('SUCCESSFULLY UPDATED');
END pr22;
/

CREATION:
SQL> @PR22

Procedure created.

EXECUTION:
SQL> EXECUTE PR22
SUCCESSFULLY UPDATED

PL/SQL procedure successfully completed.

OUTPUT:
SQL> SELECT * FROM supplier;

S_CODE S_NAME ADDRESS STATUS


---------- --------------- --------------- ----------------------
111 SMITH SEC
112 ALEX HYD important
113 PONTING HYD
114 CLARK SEC
Ex.No.10 CREATION OF TABLES USING PRIMARYKEY AND PL/SQL PROCEDURE
AIM: Create the following tables :
Student(roll-no, name, date-of-birth, course-id)
Course (Course-id, name, fee, duration, status)
(a)enter atleast 8 records into the tables.
(b)Write PL/SQL procedure to do the following :
Set the status of course to "offered" in which the number of candidates is at
least 10 otherwise set it to "not offered".

SQL > CREATE TABLE student(roll char(10) PRIMARY KEY ,dob date, name varchar2(10),courseid char(10));
table created.
SQL > CREATE TABLE course(courseid char(10) PRIMARY KEY , name varchar2(10),2 fee number, duration number);
table created.
insert into course values ('c001','mca',75000,3);
insert into course values ('m003','mba',200000,2);

insert into student values ('04808015', '07-sep-1990','suchitra','c001');


insert into student values ('04708015', '07-sep-1990','radhika','c001');
insert into student values ('04708012','12-jan-1988','hemasri','c001');
insert into student values ('04807010','26-dec-1990','jayasri','c001');
insert into student values ('04708016','07-jul-1985','radha','c001');
insert into student values ('04708013','12-jun-1988','hemalatha','c001');
insert into student values ('04807011','26-dec-1990','vasuda','c001');
insert into student values ('04808018','06-nov-1978','sadhana','c001');
insert into student values ('04708019','07-oct-1985','ragini','c001');
insert into student values ('04708020','21-feb-1988','anmol','m003');
insert into student values ('04807021','20-dec-1990','teja','m003');
insert into student values('0460909','17-jul-1990','kareena','c001');

PL/SQL procedure to set the status of course to 'offered' in which the number of candidates is at least 10 otherwise
set to ‘not offered’ .

SQL > edit status.SQL ;


Declare
id char(10);
num number;
cursor course_cr is SELECT * from course;
course_val course_cr%rowtype;
begin open course_cr;
loop fetch course_cr into course_val;
exit
when course_cr% notfound;
id:=course_val.courseid;
SELECT count(*) into num from student_all where courseid=id;
if num < 10 then update course set status='not-off' where courseid=id;
end if;
if num >= 10 then update course set status='offered' where courseid=id;
end if;
end loop;
close course_cr;
end;

SQL > SELECT * from course;

courseid name fee duration status


c001 mca 75000 3 not-off
003 mba 200000 2 not-off

SQL > start status.SQL ;

31 /pl/SQL procedure successfully completed.

SQL > SELECT * from course;

courseid name fee duration status


c001 mca 75000 3 offered
m003 mba 200000 2 Not-off

Ex.No.11 CREATION OF TABLES USING COMPLEX QUERIES


AIM: Create the following tables :
Student(roll-no,name,date-of-birth,course-id)
Course(Course-id,name,fee,duration)
(a)enter atleast 8 records into the tables.
(b) Generate queries to :
(i) List all those students who are between 18-19 years of age and have opted
for MCA course.
(ii)List all those courses in which number of students are less than 10.

PROGRAM
SQL> create table course(course-id number(4) primary key, name varchar2(10),fee
number(7),duration number(1));

Table created

SQL> insert into course values(101,TMCAT,60000,3);

1 row created

SQL> insert into course values(102,TMBAT,50000,2);

1 row created

SQL> insert into course values(103,TB.techT,80000,4);

1 row created

SQL> insert into course values(104,TM.techT,55000,2);

1 row created

SQL> insert into course values(105,TM.scT,60000,2);

1 row created

SQL>select*from course;

Course_id cname fee Duration


101 MCA 60000 3
102 MBA 50000 2
103 B.Tech 80000 4
104 M.Tech 55000 2
105 M.Sc 60000 2
SQL> create table student(roll-no number(4) primary key,name
varchar2(15),dob date,course-id number(4), foreign key(course- id)
references course(course-id));

Table created
SQL> insert into student values(1,TbangT,T14-FEB-03T,101) ;

1 row created

SQL> insert into student values(2,TmaruthiT,T15-JAN-91T,102);

1 row created

SQL> insert into student values(3,TkiranT,T14-NOV-89T,104);

1 row created

SQL> insert into student values(4,TnarsimhaT,T03-OCT-94T,103);

1 row created

SQL> insert into student values(5,TvignuT,T16-JAN-95T,103);

1 row created

SQL>select*from student;
Roll_no name dob Course_id
1 Bang 14-FEB-03 101
2 Maruthi 15-JAN-91 102
3 Kiran 14-NOV-89 104
4 Narasimha 03-OCT-94 103
5 Vignu 16-JAN-95 103

(i) List all those students who are between 18-19 years of age and have opted

for MCA course.

Sql> select * from student where months_between(sysdate.dob)/12>=18 and


months_between(sysdate.dob)/12<=19 and course_id=(select course_id from course
where name=’mca’);

Roll_no name Dob Course_id


1 Bang 14-FEB-03 101
(ii)List all those courses in which number of durations are less than

5.

Sql> select * from course where duration<5:

Course_id cname Fee Duration


101 MCA 60000 3
102 MBA 50000 2
103 B.Tech 80000 4
104 M.Tech 55000 2
105 M.Sc 60000 2
Ex.No.12 CREATION OF BRANCH AND OF TABLES USING QUERIES

AIM: Create the following tables :


Branch (branch-id, branch-name, branch-city)
Customer (customer-id, customer-name, customer-city, branch-id)
(a) enter atleast 8 records into the tables.
(b) Generate queries to do the following :
(i) List all those customers who live in the same city as the branch in which
they have account.
(ii) List all those customers who have an account in more than one branch

PROGRAM

SQL>create table branch(bid number(3),bname varchar2(20),bcity varchar2(20));

Table Created

SQL> insert into branch

values(101,’ABC’,’Hyderabad’); 1 row created

SQL> insert into branch

values(102,’DEF’,’Adilabad’); 1 row created

SQL> insert into branch

values(103,’MNO’,’Warangal’); 1 row created

SQL> insert into branch

values(104,’PQR’,’Tirupathi’); 1 row created

SQL> insert into branch

values(105,’XYZ’,’Hyderabad’); 1 row created

Sql> select * from branch;

Bid bname bcity


101 ABC Hyderabad
102 DEF Adilabad
103 MNO Warangal
104 PQR Tirupathi
105 XYZ Hyderabad

SQL>create table customer(cid number(3),cname varchar2(20),

ccity varchar2(20),bid number(3));

Table created
SQL> insert into customer values(201,’A’,’Hyderabad’,101);

1 row created

SQL> insert into customer values(202,’A’,’Hyderabad’,102);

1 row created

SQL> insert into customer values(203,’C’,’Warangal’,102);

1 row created

SQL> insert into customer values(204,’D’,’Warangal’,103);

1 row created

SQL> insert into customer

values(205,’E’,’Tirupathi’,104); 1 row created

Sql> select * from customer;

cid Cname Ccity Bid


201 A Hyderabad 101
202 A Hyderabad 102
203 C Warangal 102
204 D Warangal 103
205 E Tirupathi 104

(i) List all those customers who live in the same city as the branch in which

they have account.

Sql> select cid,cname,ccity,bcity from branch,customer where branch.bid=customer.bid and


branch.bcity=customer.bcity;

cid Cname Ccity Bcity


201 A Hyderabad Hyderabad
204 D Warangal Warangal
205 E Tirupathi Tirupathi

(ii) List all those customers who have an account in more than one branch.

Sql>

Cname
A
A

Ex.No.13 CREATION OF TABLES AND USING TRIGGER


AIM: Create the following tables :
Book(accession-no,title,publisher,year,date-of-purchase,
status)
Book-Place(accession-no, rack-id, rack-position)
Member(member-id,name,number-of-books-issued,max-limit, status)
Book-issue(accession-no, member-id, date-of-issue)
(a)enter atleast 8 records into the tables.
(b) Write a PL/SQL procedure to issue the book.
Write a trigger to set the status of a book neither to "lost" which is
neither issued nor in the library.

PROGRAM
SQL>create table book1(accession_no number(4) primary key, title
varchar2(15),publisher varchar2(15),year number(4),dopdate, status varchar2(20));

Table Created

SQL>insert into book values(101,'cobol','elite',2008,'10-aug-09','issued');

1 row created

SQL>insert into book values(102,'sucess','sakshi',2009,'22-jul-09','issued');

1 row created

SQL>insert into book values(103,'plsql,sql','bpb',2003,'14-nov-05','issued');

1 row created

SQL>insert into book values(104,'java','james',2002,'20-jun-02','issued');

1 row created

SQL>insert into book values(105,'c,c++','dennis',1991,'07-dec-94','not issued');

1 row created

select * from book;

accession_no title Publisher Year Dop status


101 cobol Elite 2008 10-aug-09 issued
102 sucess Sakshi 2009 22-jul-09 issued
103 Plsql,sql Bpb 2003 14-nov-05 issued
104 Java James 2002 20-jun-02 issued
105 C,c++ Dennis 1991 07-dec-94 Not issued
SQL>create table member(member_id number(4) primarykey, name
varchar2(15),no_of_books_issued number(2),max_limit number(2));

Table Created

SQL>insert into member values(1,'sandeep',2,2);

1 row created

SQL>insert into member values(2,'narsimha',1,2);

1 row created

SQL>insert into member values(3,'sai',2,3);

1 row created

SQL>insert into member values(4,'bang',3,3);

1 row created

SQL>insert into member values(5,'kiran',2,2);

1 row created

select * from member;

member_id name no_of_books_issued max_limit


1 Sandeep 2 2
2 Narasimha 1 2
3 Sai 2 3
4 Bang 3 3
5 kiran 2 2

SQL>create table book_issue2(accession_no number(5), member_id number(4), date_of_issue


date,foreign key(accession_no) references book2(accession_no),foreign key(member_id)
references member2(member_id));

Table Created

SQL>insert into book_issue2 values(101,2,'04-sep-09');

1 row created

SQL>insert into book_issue2 values(102,5,'10-jul-11');

1 row created

SQL>insert into book_issue2 values(101,5,'10-jul-11');

1 row created

SQL>insert into book_issue2 values(105,3,'22-jan-12');


1 row createdSQL>insert into book_issue2 values(103,4,'15-feb-10');

1 row created

Select * from book_issue2;

accession_no member_id date_of_issue


101 2 04-sep-09
102 5 10-jul-11
101 5 10-jul-11
105 3 22-jan-12
103 4 15-feb-10

(ii) Write a trigger to set the status of a book neither to "lost" which is

neither issued nor in the library.

a) Procedure:

sql> edit one.sql

create or replace procedure

p11 is

ac_no book2.accession_no%type;

sta book2.status%type;

begin

ac_no:='&ac_no';

select status into sta from book2 where accession_no=ac_no;

if(sta='not issued') then

update book2 set status='issued' where accession_no=ac_no;

dbms_output.put_line('book issued');

else

dbms_output.put_line('already book has been

issued'); end if;

end;

sql>@one.sql;

enter the value for

ac_no:105 procedure
created.

Sql>exec p11;

Book issued

Pl/sql procedure successfully

completed. Sql> select * from book;

accession_no title Publisher Year dop status


101 cobol Elite 2008 10-aug-09 issued
102 sucess Sakshi 2009 22-jul-09 issued
103 Plsql,sql Bpb 2003 14-nov-05 issued
104 Java James 2002 20-jun-02 issued
105 C,c++ Dennis 1991 07-dec-94 issued

Trigger:

sql> ed two.sql;

create or replace trigger t2

before insert or update on

book_place for each row

begin

if(:new.rack_position=00)then

update book2 set

status='lost'; end if;

end;

sql>@two.s

ql; trigger

created.

Sql.select * from book;

accession_no title Publisher Year dop status


101 cobol Elite 2008 10-aug-09 lost
102 sucess Sakshi 2009 22-jul-09 lost
103 Plsql,sql Bpb 2003 14-nov-05 lost
104 Java James 2002 20-jun-02 lost
105 C,c++ Dennis 1991 07-dec-94 lost
Ex.No.14 CREATION OF PROCESURE TO ISSUE THE BOOK

AIM: Create the following tables :


Book(accession-no, title, publisher, year, date-of-purchase, status)
Member(member-id,name,number-of-books-issued,max-limit,status)
Book-issue(accession-no,member-id,date-of-issue)
a) enter atleast 8 records into the tables.
(b) Write a PUSQL procedure to issue the book.
Write a trigger to set the status of students to "back listed" if
they have taken book but not returned even after one year.

PROGRAM

SQL>create table book1(accession_no number(4) primary key,title


varchar2(15),publisher varchar2(15),year number(4),dop date,status varchar2(20));

Table Created

SQL>insert into book values(101,'cobol','elite',2008,'10-aug-09','issued');

1 row created

SQL>insert into book values(102,'sucess','sakshi',2009,'22-jul-09','issued');

1 row created

SQL>insert into book values(103,'plsql,sql','bpb',2003,'14-nov-05','issued');

1 row created

SQL>insert into book values(104,'java','james',2002,'20-jun-02','issued');

1 row created

SQL>insert into book values(105,'c,c++','dennis',1991,'07-dec-94','not issued');

1 row created

select * from book;

accession_no title Publisher year Dop status


101 cobol Elite 2008 10-aug-09 issued
102 sucess Sakshi 2009 22-jul-09 issued
103 Plsql,sql Bpb 2003 14-nov-05 issued
104 Java James 2002 20-jun-02 issued
105 C,c++ Dennis 1991 07-dec-94 Not issued

SQL>create table member(member_id number(4) primarykey, name


varchar2(15),no_of_books_issued number(2),max_limit number(2));

Table Created
SQL>insert into member values(1,'sandeep',2,2);

1 row created

SQL>insert into member values(2,'narsimha',1,2);

1 row created

SQL>insert into member values(3,'sai',2,3);

1 row created

SQL>insert into member values(4,'bang',3,3);

1 row created

SQL>insert into member values(5,'kiran',2,2);

1 row created

select * from member;

member_id name no_of_books_issued max_limit


1 Sandeep 2 2
2 Narasimha 1 2
3 Sai 2 3
4 Bang 3 3
5 kiran 2 2

SQL>create table book_issue2(accession_no number(5), member_id number(4), date_of_issue


date,foreign key(accession_no) references book2(accession_no),foreign key(member_id)
references member2(member_id));

Table Created

SQL>insert into book_issue2 values(101,2,'04-sep-09');

1 row created

SQL>insert into book_issue2 values(102,5,'10-jul-11');

1 row created

SQL>insert into book_issue2 values(101,5,'10-jul-11');

1 row created

SQL>insert into book_issue2 values(105,3,'22-jan-12');

1 row created
SQL>insert into book_issue2 values(103,4,'15-feb-10');

1 row created

Select * from book_issue2;

accession_no member_id date_of_issue


101 2 04-sep-09
102 5 10-jul-11
101 5 10-jul-11
105 3 22-jan-12
103 4 15-feb-10

b)(i) Write a PL/SQL procedure to issue the book.

Procedure:

sql> edit one.sql

create or replace procedure

p11 is

ac_no book2.accession_no%type;

sta book2.status%type;

begin

ac_no:='&ac_no';

select status into sta from book2 where accession_no=ac_no;

if(sta='not issued') then

update book2 set status='issued' where accession_no=ac_no;

dbms_output.put_line('book issued');

else

dbms_output.put_line('already book has been

issued'); end if;

end;

sql>@one.sql;

enter the value for

ac_no:105 procedure

created.
Sql> exec p11;

Book Issued

Pl/sql procedure successfully completed.

Sql> select * from book;

accession_no title Publisher year dop status


101 cobol Elite 2008 10-aug-09 issued
102 sucess Sakshi 2009 22-jul-09 issued
103 Plsql,sql Bpb 2003 14-nov-05 issued
104 Java James 2002 20-jun-02 issued
105 C,c++ Dennis 1991 07-dec-94 issued

(ii)Write a trigger to set the status of students to "back

listed" if they have taken book but not returned even after

one year.

Trigger:

sql>edit tri2.sql;

create or replace trigger t1

before insert or

update on

book_issue2 for

each row

begin

if(:new.date_of_is

sue<'01-jan-

07')then update

book2 set

status='back

listed'; end if;

end;
sql> @tri2.sql;

trigger created.

Sql> select * from book2;

accession_no title Publisher year dop status


101 cobol Elite 2008 10-aug-09 Back listed
102 sucess Sakshi 2009 22-jul-09 Back listed
103 Plsql,sql Bpb 2003 14-nov-05 Back listed
104 Java James 2002 20-jun-02 Back listed
105 C,c++ Dennis 1991 07-dec-94 Back listed
Ex.No.15 ADDITION OF TWO NUMNERS

a)WAP in PL/SQL for addition of two numbers.


declare

x number(5);
y number(5);
z number(7);

begin

x:=10;

y:=20;

z:=x+y;

dbms_output.put_line('Sum is '||z);
end;
/

Output
Sum is 30

b)WAP in PL/SQL to check the given number is even or odd.

DECLARE
n1 NUMBER := &num1;
BEGIN
-- test if the number provided by the user is even
IF MOD(n1,2) = 0 THEN
DBMS_OUTPUT.PUT_LINE ('The number. '||n1||
' is even number');
ELSE
DBMS_OUTPUT.PUT_LINE ('The number '||n1||' is odd number.');
END IF;
DBMS_OUTPUT.PUT_LINE ('Done Successfully');
END;
/

Output

Enter value for num1:19


Old 2: n1 NUMBER := &num1;
New 2: n1 NUMBER := 19;
The number 19 is odd number
PL/SQL procedure successfully completed.
Ex.No.16 CREATION OF CLIENT AND PRODUCT TABLES

AIM: Create the following tables:


client_master(client_no, name, address 1, address2, city, state, pincode, bal_due)
Product_master(Product no, Description, Profit_percent,Unit measure, Qty_on_hand,
Reoder_lvl, Sell price, Cost_price)
iii) Insert the atleast 8 records into respective tables:
iv) On the basis of above two tables answer the following Questionries:
a. Find out the names of all the clients.
b. Retrieve the list of names and cities of all the clients.
c. List the various products available from the product_master
table. List all the clients who are located in Bombay.
d. Display the information for client no 0001 and 0002.
e. Find the products with description as '1.44 drive' and '1.22 Drive'.
client_master(client_no, name, address 1, address2, city, state, pincode, bal_due)

TABLE: client_master

create table client_master


(client_no varchar2(6) primary key,
name varchar2(20),
address1 varchar2(30),
address2 varchar2(30),
city varchar2(15),
state varchar2(15)
pincode number(6),
bal_due number(10,2));

iii) Insert the atleast 8 records into respective tables:

Insert the following data into table:

insert into client_master values('0001', 'Ivan','','', 'Bombay', 'Maharashtra',400054, 15000);

insert into client_master values('0002', 'Vandana','','', 'Madras','Tamilnadu',780001, 0);

insert into client_master values('0003', 'Pramada','','', 'Bombay', 'Maharashtra',400057, 5000);

insert into client_master values('0004', 'Basu','','', 'Bombay','Maharashtra',400056, 0);

insert into client_master values('0005', 'Ravi','','', 'Delhi' ,'',100001, 2000);

insert into client_master values('0006', 'Rukmini','','', 'Bombay', 'Maharashtra', 400050, 0);


Product_master(Product no, Description, Profit_percent,Unit measure, Qty_on_hand,
Reoder_lvl, Sell price, Cost_price)

TABLE : Product_master :
create table product_master (
product_no varchar2(6) primary
key, description varchar2(20),
profit_percent number(5,2),
unit_measure varchar2(8),
qty_on_hand number(6),
reoder_lvl number(6),
sell_price number(10),
cost_price number(10));

Insert the following data into table:

insert into product_master values ('P00001', '1.44floppies', 5, 'piece', 100, 20, 525,
500);
insert into product_master values('P03453', 'Monitors',6, 'piece', 10, 3, 12000, 11200);
insert into product_master values('P06734', 'Mouse',5, 'piece', 20, 5, 1050,
500);
insert into product_master values('P07865', '1.22 floppies', 5, 'piece', 100, 20, 525,
500);
insert into product_master values('P07868', 'Keyboards',2, 'piece', 10, 3, 3150, 3050);
insert into product_master values('P07885', 'CD Drive',2.5, 'piece',10, 3, 5250, 5100);
insert into product_master values('P07965', '540 HDD',4, 'piece', 10, 3, 8400, 8000);
insert into product_master values('P07975', '1.44 Drive',5, 'piece', 10, 3, 1050, 1000);
insert into product_master values('P08865', '1.22 Drive',5, 'piece', 2, 3, 1050, 1000);

iv) On the basis of above two tables answer the following Questionries:

i) Find out the names of all the clients.

select name from lient_master;

Output:

ii) Retrieve the list of names and cities of all the clients.

select client_no , name, city from client_master;


Output:

iii) List the various products available from the product_master table.

select product_no, description from product_master;

Output:
iv) List all the clients who are located in Bombay.

select client_no, name from client_master where city =’Bombay’;

Output:

v) Display the information for client no 0001 and 0002.

select * from client_master where client_no in (0001,0002);

Output:

vi) Find the products with description as ‘1.44 drive’ and ‘1.22 Drive’. select *
from

product_master where description in (‘1.44 Drive’,’1.22 Drive’) ;

Output:
Ex.No.17 CREATION OF TABLES USING PRIMARY KEY

AIM: Create the following tables:


client_master(client no, name, address1, address2, city, state, pincode)
Product_master(Product no, Description, Profit_percent,
Unit measure,Qty_on_hand, Reoder lvl, Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective tables:
iv) On the basis of above two tables answer the following
Questionries:

a)Find the products with description as '1.44 drive' and '1.22 Drive'.
b)Find all the products whose sell price is greater then 5000.
c)Find the list of all clients who stay in in city `Bombay' or city 'Delhi' or
`Madras'.
d)Find the product whose selling price is greater than 2000
and less than or equal to 5000.
e)List the name, city and state of clients not in the state of 'Maharashtra'

client_master(client no, name, address1, address2, city, state, pincode)

create table client_master


(client_no varchar2(6) primary key, name
varchar2(20),
address1 varchar2(30),
address2 varchar2(30),
city varchar2(15),
state varchar2(15), pincode
number(6),
bal_due number(10,2));

Insert the following data into table:

insert into client_master values('0001', 'Ivan','','', 'Bombay', 'Maharashtra',400054, 15000);


insert into client_master values('0002', 'Vandana','','', 'Madras' ,'Tamilnadu',780001, 0);

insert into client_master values('0003', 'Pramada','','', 'Bombay', 'Maharashtra',400057, 5000);

insert into client_master values('0004', 'Basu','','', 'Bombay' ,'Maharashtra',400056, 0);

insert into client_master values('0005', 'Ravi','','', 'Delhi' ,'',100001, 2000);

insert into client_master values('0006', 'Rukmini','','', 'Bombay', 'Maharashtra', 400050, 0);

ii) Product_master(Product no, Description, Profit_percent,


Unit measure,Qty_on_hand, Reoder lvl, Sell_price, Cost_price)

create table product_master (


product_no varchar2(6) primary key, description
varchar2(20), profit_percent
number(5,2), unit_measure varchar2(8),
qty_on_hand number(6),
reoder_lvl number(6),
sell_price number(10),
cost_pricenumber(10));

Insert the following data into table:

insert into product_master values '1.44floppies' 5, 'piece', 100, 20, 525,


('P00001', ,
500);
insert into product_master 'Monitors',6, 'piece', 10, 3, 12000 11200)
values('P03453', , ;
insert into product_master 'Mouse',5, 'piece', 20, 5, 1050,
values('P06734',
500);

insert into product_master '1.22 5, 'piece', 100, 20, 525,


values('P07865', floppies',
500);
insert into product_master 'Keyboards',2, 'piece', 10, 3, 3150, 3050)
values('P07868', ;
insert into product_master 'CD Drive',2.5, 'piece',10, 3, 5250, 5100)
values('P07885', ;
insert into product_master '540 HDD',4, 'piece', 10, 3, 8400, 8000)
values('P07965', ;
insert into product_master '1.44 Drive',5, 'piece', 10, 3, 1050, 1000)
values('P07975', ;
insert into product_master '1.22 Drive',5, 'piece', 2, 3, 1050, 1000)
values('P08865', ;
iv) On the basis of above two tables answer the following
Questionries:

a) Find the products with description as ‘1.44 drive’ and ‘1.22 Drive’.

select * from product_master where description in (‘1.44 Drive’,’1.22 Drive’) ;

Output:

b) Find all the products whose sell price is greater then 5000.

select * from product_master where sell_price > 5000;


Output:

c) Find the list of all clients who stay in in city ‘Bombay’ or city ‘Delhi’ or ‘Madras’.

select * from client_master where city in(‘Bombay’,’Delhi’,’Madras’);

Output:

d) Find the product whose selling price is greater than 2000 and less than or equal to 5000.

select * from product_master where sell_price between 2000 and 5000;

Output:
e) List the name, city and state of clients not in the state of ‘Maharashtra’.

select name, city, state from client_master where state=’Maharashtra’;

Output:

Ex.No.18 EXECUTION OF SIMPLE QUERIES


AIM: Create the following tables:
client_master(client no, name, address1, address2, city, state, pincode)
Product_master(Product no, Description, Profit_percent, Unit measure,Qty_on_hand, Reoder lvl,
Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective tables:
iv) On the basis of above two tables answer the following Questionries:

a)Change the selling price of '1.44 floppy drive to Rs.1150.00


b)Delete the record with client 0001 from the client master table.
c)Change the city of client_no'0005' to Bombay.
d)Change the bal_due of client_no '0001, to 1000.
e)Find the products whose selling price is more than 1500 and also find the new selling price as
original selling price *15.
f)Find out the clients who stay in a city whose second letter is a.
g)Find out the name of all clients having 'a' as the second letter in their names.
h) List the products in sorted order of their description

i) client_master(client no, name, address1, address2, city, state, pincode)

create table client_master


(client_no varchar2(6) primary key, name
varchar2(20),
address1 varchar2(30),
address2 varchar2(30),
city varchar2(15),
state varchar2(15),
pincodenumber(6),
bal_due number(10,2)
);

Insert the following data into table:

insert into client_master values('0001', 'Ivan','','', 'Bombay', 'Maharashtra',400054, 15000);

insert into client_master values('0002', 'Vandana','','', 'Madras','Tamilnadu',780001, 0);

insert into client_master values('0003', 'Pramada','','', 'Bombay',’ Maharashtra',400057, 5000);

insert into client_master values('0004', 'Basu','','', 'Bombay','Maharashtra',400056, 0);

insert into client_master values('0005', 'Ravi','','', 'Delhi','',100001, 2000);


insert into client_master values('0006', 'Rukmini','','', 'Bombay', 'Maharashtra', 400050, 0);

ii) Product_master(Product no, Description, Profit_percent, Unit measure,


Qty_on_hand, Reoder lvl, Sell_price, Cost_price)

create table product_master (


product_no varchar2(6) primary key, description
varchar2(20), profit_percent
number(5,2), unit_measure varchar2(8),
qty_on_hand number(6),
reoder_lvl number(6),
sell_price number(10), cost_price
number(10));

Insert the following data into table:

insert into product_master values '1.44floppies' 5, 'piece', 100, 20, 525,


('P00001', ,
500);
insert into product_master 'Monitors',6, 'piece', 10, 3, 12000 11200)
values('P03453', , ;
insert into product_master 'Mouse',5, 'piece', 20, 5, 1050,
values('P06734',
500);

nsert into product_master '1.22 5, 'piece', 100, 20, 525,


values('P07865', floppies',
500);
insert into product_master 'Keyboards',2, 'piece', 10, 3, 3150, 3050)
values('P07868', ;
insert into product_master 'CD Drive',2.5, 'piece',10, 3, 5250, 5100)
values('P07885', ;
insert into product_master '540 HDD',4, 'piece', 10, 3, 8400, 8000)
values('P07965', ;
insert into product_master '1.44 Drive',5, 'piece', 10, 3, 1050, 1000)
values('P07975', ;
insert into product_master '1.22 Drive',5, 'piece', 2, 3, 1050, 1000)
values('P08865', ;

iv) On the basis of above two tables answer the following Questionries:

a) Change the selling price of ‘1.44 floppy drive to Rs.1150.00

update product_master set sell_price=1150.00 where description='1.44floppies';


Result:

b)Delete the record with client 0001 from the client master table.

c) Change the city of client_no’0005’ to Bombay.

update client_master set city='Bombay' where client_no='0005';

Result:

d) Change the bal_due of client_no ‘0001, to 1000.

update client_master set bal_due=1000 where client_no='0001';

output

e) Find the products whose selling price is more than 1500 and also find the new
selling price as original selling price *15.
select description from product_master where sell_price
> 1500; update product_master set
sell_price=sell_price*15;
select sell_price from product_master;

Output

e) Find out the clients who stay in a city whose second letter is a.
select * from CLIENT_MASTER where city like '_a%';

Output

f) Find out the name of all clients having ‘a’ as the second letter in their names.

select name from CLIENT_MASTER where name like '_a%';

Output

g) List the products in sorted order of their description.

select * from product_master order by description asc;

Output

Ex.No.19 USING AGGREGATE FUNCTIONS

AIM: Create the following tables:


client_master(client no, name, address1, address2, city, state, pincode)
Product_master(Product no, Description, Profit_percent, Unit measure,Qty_on_hand, Reoder lvl,
Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective tables:
iv) On the basis of above two tables answer the following Questionries:

a)Count the total number of orders


b)Calculate the average price of all the products.
c)Calculate the minimum price of products.
d)Determine the maximum and minimum prices.
e) Rename the tittle as `max_price' and min_price respectively.

client_master(client no, name, address1, address2, city, state, pincode)

create table client_master


(client_no varchar2(6) primary key, name
varchar2(20),
address1 varchar2(30),
address2 varchar2(30),
city varchar2(15),
state varchar2(15),
pincode number(6),
bal_due number(10,2));

Insert the following data into table:

insert into client_master values('0001', 'Ivan','','', 'Bombay', 'Maharashtra',400054, 15000);

insert into client_master values('0002', 'Vandana','','', 'Madras','Tamilnadu',780001, 0);

insert into client_master values('0003', 'Pramada','','', 'Bombay',’ Maharashtra',400057, 5000);

insert into client_master values('0004', 'Basu','','', 'Bombay','Maharashtra',400056, 0);

insert into client_master values('0005', 'Ravi','','', 'Delhi','',100001, 2000);

insert into client_master values('0006', 'Rukmini','','', 'Bombay', 'Maharashtra', 400050, 0);

ii) Product_master(Product no, Description, Profit_percent, Unit measure,


Qty_on_hand, Reoder lvl, Sell_price, Cost_price)

create table product_master (


product_no varchar2(6) primary key, description
varchar2(20), profit_percent
number(5,2), unit_measure varchar2(8), qty_on_hand
number(6),
reoder_lvl number(6),
sell_price number(10), cost_price
number(10));
Insert the following data into table:

insert into product_master values ('P00001', '1.44floppies', 5, 'piece', 100, 20, 525,
500);
insert into product_master values('P03453', 'Monitors',6, 'piece', 10, 3, 12000, 11200);
insert into product_master values('P06734', 'Mouse',5, 'piece', 20, 5, 1050,
500);

nsert into product_master values('P07865', '1.22 floppies', 5, 'piece', 100, 20, 525,
500);
insert into product_master values('P07868', 'Keyboards',2, 'piece', 10, 3, 3150, 3050);
insert into product_master values('P07885', 'CD Drive',2.5, 'piece',10, 3, 5250, 5100);
insert into product_master values('P07965', '540 HDD',4, 'piece', 10, 3, 8400, 8000);
insert into product_master values('P07975', '1.44 Drive',5, 'piece', 10, 3, 1050, 1000);
insert into product_master values('P08865', '1.22 Drive',5, 'piece', 2, 3, 1050, 1000);

v) On the basis of above two tables answer the following Questionries:

a) Count the total number of orders

select sum (reorder_lvlnumber) from product_master;

Output:

b) Calculate the average price of all the products.

select avg (cost_price) from product_master;

Output:

i. c) Calculate the minimum price of products.

select min (cost_price) from product_master;

Output:
d) Determine the maximum and minimum prices . Rename the tittle as ‘max_price’ and min_price
respectively.

select min (cost_price) as min_price,


max (cost_price) as max_price from product_master;

Output:

e) Count the number of products having price greater than or equal to 1500.

select count (cost_price) from product_master where cost_price >= 1500;

Output:

Ex.No.20 CREATION TABLES USING JOINS

AIM: Create the following tables:

client_master(client no, name, address1, address2, city, state, pincode)


Product_master(Product no, Description, Profit_percent, Unit measure,Qty_on_hand, Reoder lvl,
Sell_price, Cost_price)
iii)Insert the atleast 8 records into respective tables:
iv) On the basis of above two tables answer the following Questionries:
1. Find out the product which has been sold to 'Ivan Sayross.'
2. Find out the product and their quantities that will have do delivered.
3. Find the product_no and description of moving products.
4. Find out the names of cl ents who have purchased 'CD DRIVE'
5. List the product_no and re_order_no of customers haaving qty ordered less than 5 f om the order
details table for the product "1.44 floppies".
6. Count the number of products having price greater than or equal to 1500.

client_master(client no, name, address1, address2, city, state, pincode)

create table client_master


(client_no varchar2(6) primary key,
name varchar2(20),
address1 varchar2(30),
address2 varchar2(30),
city varchar2(15),
state varchar2(15),
pincode number(6),
bal_due number(10,2));

Insert the following data into table:

insert into client_master values('0001', 'Ivan','','', 'Bombay', 'Maharashtra',400054, 15000);

insert into client_master values('0002', 'Vandana','','', 'Madras','Tamilnadu',780001, 0);

insert into client_master values('0003', 'Pramada','','', 'Bombay',’ Maharashtra',400057, 5000);

insert into client_master values('0004', 'Basu','','', 'Bombay','Maharashtra',400056, 0);

insert into client_master values('0005', 'Ravi','','', 'Delhi','',100001, 2000);

insert into client_master values('0006', 'Rukmini','','', 'Bombay', 'Maharashtra', 400050, 0);

ii) Product_master(Product no, Description, Profit_percent, Unit measure,


Qty_on_hand, Reoder lvl, Sell_price, Cost_price)

create table product_master (product_no varchar2(6) primary key, description varchar2(20), profit_percent
number(5,2), unit_measure varchar2(8), qty_on_hand number(6), reoder_lvl number(6),sell_price number(10),
cost_price number(10));

Insert the following data into table:

insert into product_master values ('P00001', '1.44floppies', 5, 'piece', 100, 20, 525,
500);
insert into product_master values('P03453', 'Monitors',6, 'piece', 10, 3, 12000, 11200);
insert into product_master values('P06734', 'Mouse',5, 'piece', 20, 5, 1050,
500);

nsert into product_master values('P07865', '1.22 floppies', 5, 'piece', 100, 20, 525,
500);
insert into product_master values('P07868', 'Keyboards',2, 'piece', 10, 3, 3150, 3050);
insert into product_master values('P07885', 'CD Drive',2.5, 'piece',10, 3, 5250, 5100);
insert into product_master values('P07965', '540 HDD',4, 'piece', 10, 3, 8400, 8000);
insert into product_master values('P07975', '1.44 Drive',5, 'piece', 10, 3, 1050, 1000);
insert into product_master values('P08865', '1.22 Drive',5, 'piece', 2, 3, 1050, 1000);

iv) On the basis of above two tables answer the following Questionries:

1) find out the product which has been sold to 'ivan bayroos'.

select d.product_no,p.description from sales_order_details d , product_master p , client_master c,sales_order s


where p.product_no=d.product_no and s.s_order_no=d.s_order_no and c.client_no=s.client_no and c.name='Ivan
Bayross';

Output:

2) find out the product and their quantities that will have to delivered in the current month.

select d.product_no,p.description,sum(d.qty_ordered)
from sales_order_details d,sales_order s,product_master p
where p.product_no=d.product_no and s.s_order_no=d.s_order_no and to_char(dely_date,'mon-
yy')=to_char(sysdate,'mon-yy')
group by d.product_no,p.description;

Output:
3) find the product_no and description of moving products.

select distinct p.product_no,p.description from product_master p ,sales_order_details d


where p.product_no=d.product_no;

Output:

4. Find out the names of clients who have purchased 'CD DRIVE'

select distinct s.client_no, c.name from sales_order_details d,sales_order s,product_master p, client_master c


where p.product_no=d.product_no and s.s_order_no=d.s_order_no and c.client_no=s.client_no and
p.description='CD Drive'

Output:

5) List the product_no and s_order_no of customers having qty_ordered less than 5 from the order detail Table
for the product '1.44 Floppies'.
select d.product_no,d.s_order_no from sales_order_details d,sales_order s,product_master p
where s.s_order_no=d.s_order_no and p.product_no=d.product_no and d.qty_ordered<5 and
p.description='1.44 Floppies';
Output:

Ex.No.21 USING COMPLEX QUERIES

AIM: client_master(client no, name, address1, address2, city, state, pincode)


Product_master(Product no, Description, Profit_percent, Unit measure,Qty_on_hand, Reoder lvl,
Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective tables:

iv) On the basis of above two tables answer the following Questionries:

1. Find the products and their quantities for the orders placed by Wandan Saitwal ' and "Ivan
Bayross".
2. Find the products and their quantities for the orders placed by client_no " C00001" and
"C00002".
3. Find the order No„ Client No and salesman No. where client has been received by more than one
salesman.
4. Display the s_order_date in the format "dd-mm-yy" e.g. "12- feb-96"
5. Find the date , 15 days after date.

client_master(client no, name, address1, address2, city, state, pincode)


create table client_master
(client_no varchar2(6) primary key,
name varchar2(20),
address1 varchar2(30),
address2 varchar2(30),
city varchar2(15),
state varchar2(15),
pincode number(6),
bal_due number(10,2));

Insert the following data into table:

insert into client_master values('0001', 'Ivan','','', 'Bombay', 'Maharashtra',400054, 15000);

insert into client_master values('0002', 'Vandana','','', 'Madras','Tamilnadu',780001, 0);

insert into client_master values('0003', 'Pramada','','', 'Bombay',’ Maharashtra',400057, 5000);

insert into client_master values('0004', 'Basu','','', 'Bombay','Maharashtra',400056, 0);

insert into client_master values('0005', 'Ravi','','', 'Delhi','',100001, 2000);

insert into client_master values('0006', 'Rukmini','','', 'Bombay', 'Maharashtra', 400050, 0);

ii) Product_master(Product no, Description, Profit_percent, Unit measure,


Qty_on_hand, Reoder lvl, Sell_price, Cost_price)

create table product_master (


product_no varchar2(6) primary key, description varchar2(20), profit_percent number(5,2), unit_measure
varchar2(8), qty_on_hand number(6), reoder_lvl number(6), sell_price number(10), cost_price number(10));

Insert the following data into table:


insert into product_master values ('P00001', '1.44floppies', 5, 'piece', 100, 20, 525,
500);
insert into product_master values('P03453', 'Monitors',6, 'piece', 10, 3, 12000, 11200);
insert into product_master values('P06734', 'Mouse',5, 'piece', 20, 5, 1050,
500);

nsert into product_master values('P07865', '1.22 floppies', 5, 'piece', 100, 20, 525,
500);
insert into product_master values('P07868', 'Keyboards',2, 'piece', 10, 3, 3150, 3050);
insert into product_master values('P07885', 'CD Drive',2.5, 'piece',10, 3, 5250, 5100);
insert into product_master values('P07965', '540 HDD',4, 'piece', 10, 3, 8400, 8000);
insert into product_master values('P07975', '1.44 Drive',5, 'piece', 10, 3, 1050, 1000);
insert into product_master values('P08865', '1.22 Drive',5, 'piece', 2, 3, 1050, 1000);

f. On the basis of above two tables answer the following Questionries:

1. Find the products and their quantities for the orders placed by 'Vandana Saitwal' and 'Ivan Bayross'.

select d.product_no,p.description,sum(qty_ordered)"Qty Ordered"


from sales_order_details d,sales_order s,product_master p,client_master c
where s.s_order_no=d.s_order_no and p.product_no=d.product_no and c.client_no=s.client_no
and (c.name='Ivan Bayross' or c.name='Vandana Saitwal')
group by d.product_no,p.description;

Output:

2. Find the products and their quantities for the orders placed by client_no'C00001' and 'C00002'.

select s.client_no,d.product_no,p.description ,sum(qty_ordered)"Qty_ordered"


from sales_order s,sales_order_details d,product_master p,client_master c
where s.s_order_no=d.s_order_no and d.product_no=p.product_no and s.client_no=c.client_no
group by s.client_no,d.product_no,p.description
having s.client_no='C00001' or s.client_no='C00002';

Output:
3. Find the order No,, Client No and salesman No. where a client has
been received by more than one salesman.

SELECT SALES_ORDER.CLIENT_NO,SALES_ORDER.SALESMAN_NO,SALES_ORDER.S_ORDER_NO FROM


SALES_ORDER WHERE SALES_ORDER.CLIENT_NO = (SELECT SALES_ORDER.CLIENT_NO
FROM SALES_ORDER GROUP BY SALES_ORDER.CLIENT_NO
HAVING COUNT(SALES_ORDER.CLIENT_NO)>1);

Output

4.Display the s_order_date in the format “dd-mm-yy” e.g. “12- feb-96”.

SELECT TO_CHAR(SYSDATE, 'DD/MM/YY')


FROM DUAL;

Output:

6. Find the date , 15 days after date.

SELECT SYSDATE+15 MYDATE FROM DUAL;

Output:

Ex.No.22 USING GROUP CLAUSE AND INNER JOINS


AIM: client_master(client no, name, address1, address2, city, state, pincode)
Product_master(Product no, Description, Profit_percent, Unit measure,Qty_on_hand, Reoder lvl,
Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective tables:
iv) On the basis of above two tables answer the following Questionries:

1)Print the description and total quantity sold for each product.
2)Find the value of each product sold.
3)Calculate the average quantity sold for each client that has a maximum order
value of 15000.
4) Find out the products which has been sold to Ivan.
5) Find the names of clients who have 'CD Drive'.

client_master(client no, name, address1, address2, city, state, pincode)

create table client_master


(client_no varchar2(6) primary key,
name varchar2(20),
address1 varchar2(30),
address2 varchar2(30),
city varchar2(15),
state varchar2(15),
pincode number(6),
bal_due number(10,2));

Insert the following data into table:

insert into client_master values('0001', 'Ivan','','', 'Bombay', 'Maharashtra',400054, 15000);

insert into client_master values('0002', 'Vandana','','', 'Madras','Tamilnadu',780001, 0);

insert into client_master values('0003', 'Pramada','','', 'Bombay',’ Maharashtra',400057, 5000);

insert into client_master values('0004', 'Basu','','', 'Bombay','Maharashtra',400056, 0);

insert into client_master values('0005', 'Ravi','','', 'Delhi','',100001, 2000);

insert into client_master values('0006', 'Rukmini','','', 'Bombay', 'Maharashtra', 400050, 0);

ii) Product_master(Product no, Description, Profit_percent, Unit measure,


Qty_on_hand, Reoder lvl, Sell_price, Cost_price)

create table product_master (product_no varchar2(6) primary key, description varchar2(20),


profit_percent number(5,2), unit_measure varchar2(8), qty_on_hand number(6),
reoder_lvl number(6),sell_price number(10), cost_price number(10));

Insert the following data into table:

insert into product_master values '1.44floppies' 5, 'piece', 100, 20, 525,


('P00001', ,
500);
insert into product_master 'Monitors',6, 'piece', 10, 3, 12000 11200)
values('P03453', , ;
insert into product_master 'Mouse',5, 'piece', 20, 5, 1050,
values('P06734',
500);

insert into product_master '1.22 5, 'piece', 100, 20, 525,


values('P07865', floppies',
500);
insert into product_master 'Keyboards',2, 'piece', 10, 3, 3150, 3050)
values('P07868', ;
insert into product_master 'CD Drive',2.5, 'piece',10, 3, 5250, 5100)
values('P07885', ;
insert into product_master '540 HDD',4, 'piece', 10, 3, 8400, 8000)
values('P07965', ;
insert into product_master '1.44 Drive',5, 'piece', 10, 3, 1050, 1000)
values('P07975', ;
insert into product_master '1.22 Drive',5, 'piece', 2, 3, 1050, 1000)
values('P08865', ;

iv) On the basis of above two tables answer the following Questionries:

1) Print the description and total quantity sold for each product.

SELECT DISTINCT PRODUCT_MASTER.DESCRIPTION,SUM(SALES_ORDER_DETAILS.QTY_ORDER)


FROM PRODUCT_MASTER
INNER JOIN SALES_ORDER_DETAILS ON PRODUCT_MASTER.PRODUCT_NO =
SALES_ORDER_DETAILS.PRODUCT_NO
GROUP BY PRODUCT_MASTER.DESCRIPTION;

Output:
2) Find the value of each product sold.

SELECT DISTINCT PRODUCT_MASTER.DESCRIPTION,SALES_ORDER_DETAILS.PRODUCT_RATE FROM


PRODUCT_MASTER
INNER JOIN SALES_ORDER_DETAILS ON PRODUCT_MASTER.PRODUCT_NO =
SALES_ORDER_DETAILS.PRODUCT_NO;

Output:

3) Calculate the average quantity sold for each client that has a maximum order
value of 15000.

SELECT CLIENT_MASTER.NAME,AVG(DISTINCT SALES_ORDER_DETAILS.QTY_ORDER)


FROM SALES_ORDER_DETAILS
INNER JOIN SALES_ORDER S on SALES_ORDER_DETAILS.S_ORDER_NO = S.S_ORDER_NO
INNER JOIN CLIENT_MASTER ON S.CLIENT_NO= CLIENT_MASTER.CLIENT_NO
GROUP BY CLIENT_MASTER.NAME,SALES_ORDER_DETAILS.S_ORDER_NO
HAVING SUM(DISTINCT SALES_ORDER_DETAILS.PRODUCT_RATE ) >15000;

Output:

4) Find out the products which has been sold to Ivan.


SELECT PRODUCT_MASTER.DESCRIPTION
FROM PRODUCT_MASTER
INNER JOIN SALES_ORDER_DETAILS ON PRODUCT_MASTER.PRODUCT_NO =
SALES_ORDER_DETAILS.PRODUCT_NO
INNER JOIN SALES_ORDER ON SALES_ORDER_DETAILS.S_ORDER_NO = SALES_ORDER.S_ORDER_NO
INNER JOIN CLIENT_MASTER ON SALES_ORDER.CLIENT_NO = CLIENT_MASTER.CLIENT_NO WHERE
CLIENT_MASTER.NAME LIKE 'ivan';

Output:

5. Find the names of clients who have ‘CD Drive’.

SELECT CLIENT_MASTER.NAME
FROM SALES_ORDER_DETAILS
INNER JOIN SALES_ORDER ON SALES_ORDER_DETAILS.S_ORDER_NO = SALES_ORDER.S_ORDER_NO
INNER JOIN CLIENT_MASTER ON SALES_ORDER.CLIENT_NO= CLIENT_MASTER.CLIENT_NO
INNER JOIN PRODUCT_MASTER MASTER2 on SALES_ORDER_DETAILS.PRODUCT_NO =
MASTER2.PRODUCT_NO WHERE
MASTER2.DESCRIPTION LIKE 'CD_drive';

Output:
Ex.No.23 USING OPERATORS, JOINS AND CLAUSES

AIM:Create the following tables:


(i)client_master(client no, name, address1, address2, city, state, pincode)

(ii)Product_master(Product no, Description, Profit_percent, Unit measure,Qty_on_hand, Reoder lvl,


Sell_price, Cost_price)
iii) Insert the atleast 8 records into respective tables:

iv) On the basis of above two tables answer the following Questionnaires:
1) Find the products and their quantities for the orders placed by `Vandana' and
`Ivan'.
2)Select product_no, total qty ordered for each product.
3) Select product_no, product description and qty ordered for each product.
4)Display the order number and day on which clients placed their order.
5) Display the month and Date when the order must be delivered.

client_master(client no, name, address1, address2, city, state, pincode)

create table client_master


(client_no varchar2(6) primary key, name
varchar2(20),
address1 varchar2(30),
address2 varchar2(30),
city varchar2(15),
state varchar2(15),
pincodenumber(6),
bal_due number(10,2)
);

Insert the following data into table:

insert into client_master values('0001', 'Ivan','','', 'Bombay', 'Maharashtra',400054, 15000);

insert into client_master values('0002', 'Vandana','','', 'Madras','Tamilnadu',780001, 0);


insert into client_master values('0003', 'Pramada','','', 'Bombay',’ Maharashtra',400057, 5000);

insert into client_master values('0004', 'Basu','','', 'Bombay','Maharashtra',400056, 0);

insert into client_master values('0005', 'Ravi','','', 'Delhi','',100001, 2000);

insert into client_master values('0006', 'Rukmini','','', 'Bombay', 'Maharashtra', 400050, 0);

ii) Product_master(Product no, Description, Profit_percent, Unit measure,


Qty_on_hand, Reoder lvl, Sell_price, Cost_price)

create table product_master (


product_no varchar2(6) primary key, description
varchar2(20), profit_percent
number(5,2), unit_measure varchar2(8), qty_on_hand
number(6),
reoder_lvl number(6),
sell_price number(10), cost_price
number(10));

Insert the following data into table:

insert into product_master values ('P00001', '1.44floppies', 5, 'piece', 100, 20, 525,
500);
insert into product_master values('P03453', 'Monitors',6, 'piece', 10, 3, 12000, 11200);
insert into product_master values('P06734', 'Mouse',5, 'piece', 20, 5, 1050,
500);

nsert into product_master values('P07865', '1.22 floppies', 5, 'piece', 100, 20, 525,
500);
insert into product_master values('P07868', 'Keyboards',2, 'piece', 10, 3, 3150, 3050);
insert into product_master values('P07885', 'CD Drive',2.5, 'piece',10, 3, 5250, 5100);
insert into product_master values('P07965', '540 HDD',4, 'piece', 10, 3, 8400, 8000);
insert into product_master values('P07975', '1.44 Drive',5, 'piece', 10, 3, 1050, 1000);
insert into product_master values('P08865', '1.22 Drive',5, 'piece', 2, 3, 1050, 1000);

iV) On the basis of above two tables answer the following Questionnaires:

1) Find the products and their quantities for the orders placed by `Vandana' and
`Ivan'.

SELECT PRODUCT_MASTER.DESCRIPTION, S.QTY_ORDER


FROM PRODUCT_MASTER
INNER JOIN SALES_ORDER_DETAILS S on PRODUCT_MASTER.PRODUCT_NO = S.PRODUCT_NO
INNER JOIN SALES_ORDER SO on S.S_ORDER_NO = SO.S_ORDER_NO
INNER JOIN CLIENT_MASTER ON SO.CLIENT_NO = SYSTEM.CLIENT_MASTER.CLIENT_NO WHERE
CLIENT_MASTER.NAME LIKE 'Vandan' or CLIENT_MASTER.NAME like 'ivan';
Output

2) Select product_no, total qty ordered for each product.

SELECT DISTINCT PRODUCT_MASTER.PRODUCT_NO, SUM(S.QTY_ORDER)


FROM PRODUCT_MASTER
INNER JOIN SALES_ORDER_DETAILS S on PRODUCT_MASTER.PRODUCT_NO = S.PRODUCT_NO
INNER JOIN SALES_ORDER SO on S.S_ORDER_NO = SO.S_ORDER_NO
INNER JOIN CLIENT_MASTER ON SO.CLIENT_NO =
SYSTEM.CLIENT_MASTER.CLIENT_NO GROUP BY PRODUCT_MASTER.PRODUCT_NO;

Output:

3) Select product_no, product description and qty ordered for each product.

SELECT DISTINCT PRODUCT_MASTER.PRODUCT_NO,PRODUCT_MASTER.DESCRIPTION,


SUM(S.QTY_ORDER)
FROM PRODUCT_MASTER
INNER JOIN SALES_ORDER_DETAILS S on PRODUCT_MASTER.PRODUCT_NO = S.PRODUCT_NO
INNER JOIN SALES_ORDER SO on S.S_ORDER_NO = SO.S_ORDER_NO
INNER JOIN CLIENT_MASTER ON SO.CLIENT_NO =
SYSTEM.CLIENT_MASTER.CLIENT_NO GROUP BY
PRODUCT_MASTER.PRODUCT_NO,PRODUCT_MASTER.DESCRIPTION;

Output:
4) Display the order number and day on which clients placed their order.

SELECT
TO_CHAR(SALES_ORDER.S_ORDER_DATE,'DAY'),SALES_ORDER.S_ORDER_NO FROM
SALES_ORDER;

Output:

5) Display the month and Date when the order must be delivered.

SELECT
TO_CHAR(SALES_ORDER.S_ORDER_DATE,'MONTH'),SALES_ORDER.DELY_DATE
FROM SALES_ORDER;

Output:
Ex.No.24 CREATION OF STUDENT AND STUDENT-RANK TABLE

AIM: Student (roll-no, name, subject-opted)


Subject —rank (subject-code, subject-name, faculty-code,
specialization) Faculty (faculty-code, faculty-name, specialization)
(a) Create a form to accept the data from the user with appropriate
validation checks.
(b) Write PL/SQL procedure to the following :
Set the status of the subject to "not offered" if the subject is not opted
by at least 5 students.

SQL > CREATE TABLE student(roll char(5), 3 name varchar2(20),subjopt varchar2(30));


table created.
SQL > CREATE TABLE subject(subcode char(3),name varchar2(10),faccode
char(5),2 specialization varchar2(10), status varchar2(15));
table created.
SQL > CREATE TABLE faculty(faccode char(5),name varchar2(20),experience number,2
specialization varchar2(10));
table created.
insert into student values('s8015','suchitra','m01,m02,s01');
insert into student values('s8012',' siddu','m01,s02,p01');
insert into student values('s8010','rekha','m01,s01,m02,');
insert into student values('s8021','ravi','s01,m02,d01');
insert into student values('s8016','chitragada','m01,m02,s01’);
insert into student values('s8013',' sid roy','m01,m02,s01’);
insert into subject values('m01','dismaths','f07','','');
insert into subject values('m02',' geomaths','','','');
insert into subject values('s01',' stats',' f07','','');
insert into subject values('p01','physics',' f05','','');
insert into subject values('s02','opresearch','f07','','');
insert into subject values('d01','dbms',' f03','','');

PL/SQL procedure to set the status of the subject to’not offered’ if the subject is not opted by at least5
students.

SQL > edit status.SQL ;


declare
sub varchar2(10);
num_student number;
cursor sub_cr is SELECT * from subject;
sub_val sub_cr%rowtype;
beginopen sub_cr;
loopfetch sub_cr into sub_val;
exit when sub_cr%notfound;
sub:=sub_val.name;
SELECT count(*) into num_studentfrom student,subject where (subcode=substr(subjopt,0,3)or
subcode=substr(subjopt,5,3)or subcode=substr(subjopt,9,3))and subject.name=sub;

if num_student<5 thenupdate subjectset status=''||num_studentwhere name=sub;


end if;
end loop;
close sub_cr;
end;
SQL > SELECT * from subject;

sub name facco specialize status


--- ---------- ----- ---------- ---------------
m01 dismaths f07
m02 geomaths
s01 stats f07 p01 physics f05
s02 opresearch f07d01 dbms f0
36 rows SELECT ed.
SQL > start status.SQL ;
32 /pl/SQL procedure successfully completed.

SQL > SELECT * from subject;

sub name facco specializa status


m01 dismaths f07 offered
m02 geomaths offered
s01 stats f07 offered
p01 physics f05 notoffered
s02 opresearch f07 notoffered
d01 dbms f03 notoffered

6 rows SELECTED.
Ex.No.25 CREATION OF STUDENT AND COURSE TABLE USING PRIMARY KEY

Create the following tables :


Student(roll-no, name, date-of-birth, course-id)
Course (Course-id, name, fee, duration, status)
(a) Create a form to accept the data from the user with appropriate
validation
checks.
(b) Write PL/SQL procedure to do the following :
Set the status of course to “offered” in which the number of candidates are
at least 10 otherwise not offered.

SQL>CREATE TABLE student( roll char(10) PRIMARY KEY , dob date, name
varchar2(10),courseid char(10));
table created;

CREATE TABLE course(courseid char(10) PRIMARY KEY , name varchar2(10), fee number,
durationnumber);
table created;
SQL >INSERT into course values ('c001','mca',75000,3);
1 row created.
SQL >INSERT into course values ('m003','mba',200000,2);
1 row created.
SQL >INSERT into student values ('04808015','06-aug-1978','suchitra','c001');
1 row created.
SQL >INSERT into student values ('04708015','07-sep-1991','radhika','c001');
1 row created;

(b) Write PL/SQL procedure to do the following :


Set the status of course to “offered” in which the number of candidates
are at least 10 otherwise not offered.

PROCEDURE:
SQL> declare2
c_id course18.course_id%type;
ctr number;
cursor c20 is select course_id from course20;
begin
open c20;
loop
fetch c20 into c_id;
select count(roll_no) into ctr from student20 where course_id=c_id;
if(ctr>=10) then
update course20 set status='offered' where course_id=c_id;
else
update course20 set status=' not offered' where course_id=c_id;
end if;
exit when c20 % notfound;
dbms_output.put_line('table has been updated');
end loop;
close c20;
end;

OUTPUT:
table has been updated
table has been updated
table has been updated
PL/SQL procedure successfully completed.

SQL> select * from course20;

COURSE_ID NAME FEE DURATION STATUS


---------------- --------- ------- ------------------- -----------------
101 MCA 24700 3 not offered
102 MBA 20000 2 not offered
103 MSc 15000 2 offered
Ex.No.26 CREATION OF BOOK TABLES AND USED TO CHECK VALIDATION

AIM: Create the following tables for Library Information System :


Book(accession-no, title, publisher, author, status, date-of-purchase)
Status could be issued, present in the library, sent for binding, and account
be
issued.
(a) Create a form to accept the data from the user Create a form to accept the
data from the user with appropriate validation checks.
(b) Generate queries to do the following :

i) List all those books which are new arrivals. The books which are
acquired during the last 6 months are categorized as new arrivals.
ii) List all those books that cannot be issued and purchased 20 years ago.

SQL > CREATE TABLE book(accno number PRIMARY KEY ,title varchar2(50),publisher
varchar(25),authorvarchar2(25),dop date);
table created.
SQL > ALTER TABLE book add (status varchar2(50));table altered.
SQL > ALTER TABLE book add constraint c1 check (status in ('issued','present in library','sent for binding','a/cbe
issued'))table altered.
SQL > desc bookname null?
typeaccno not null numbertitle varchar2(50)publisher varchar2(25)author varchar2(25)dop datestatus
varchar2(50));

SQL >INSERT
intobook(accno,publisher,status,title,author,dop)values(&acno,'&pub','&st','&title','&author','&dop');
enter value for acno: 100
enter value for pub: tataenter value for st: issued
enter value for title: networka
enter value for author: stallings
enter value for dop: 01-aug-84old
1:INSERT intobook(accno,publisher,status,title,author,dop)values(&acno,'&pub','&st','&title','&author','&dop'
)
new
1:INSERT intobook(accno,publisher,status,title,author,dop)values(100,'tata','issued','networka','stallings','01-
aug-84')1 row created.
SQL > /enter value for acno: 180
enter value for pub: oracle
enter value for st: abcd
enter value for title: ad

value for author: William


enter value for dop: 01-feb-08old
1:INSERT intobook(accno,publisher,status,title,author,dop)values(&acno,'&pub','&st','&title','&author','&dop'
)new 1:
INSERT into book(accno,publisher,status,title,author,dop)values(180,'oracle','abcd','ad','william','01-feb-
08')insert into book(accno,publisher,status,title,author,dop)values(180,'oracle','abcd','ad','william','01-feb-
08')*error at line 1:ora-02290: check constraint (neerus.c1) violated
SQL > commit;
commit complete.
SQL >INSERT
intobook(accno,publisher,status,title,author,dop)values(&acno,'&pub','&st','&title','&author','&dop');
enter value for acno: 150
enter value for pub: chand
enter value for st: sent for binding
enter value for title: numerical
enter value for author: schand
enter value for dop: 01-sep-08old 1:
INSERT intobook(accno,publisher,status,title,author,dop)values(&acno,'&pub','&st','&title','&author','&dop')n
ew 1:
INSERT into book(accno,publisher,status,title,author,dop)values(150,'chand','sent forbinding','numerical','scha
nd','01-sep-08')
1 row created.
SQL > commit;

List all those books which are new arrivals. the books which are acquired during the last 6 months are
categorized as new arrivals.
commit complete.

SQL > SELECT * from book where dop<=add_months(sysdate,-6);

accno title publisher author dop status


100 networka tata stallings 01-aug-84 issued

SQL >SELECT * from book where add_months(sysdate,-6)<=dop;

accno title Publish autho dop statu


er r s
150 numeric tata schan 01- sent
alc d sep- for
08 bindi
ng
LIST ALL THOSE BOOKS THAT CANNOT BE ISSUED AND PURCHASED 20 YEARS AGO.

SQL >SELECT * from book where (sysdate-dop)/365>=20;

Accno title publisher author dop status


100 network atata stallings 01-aug-84 issued

SQL > commit;


commit complete.
Ex.No.27 DISPLAY MULTIPLICATION TABLES AND CHECK GIVEN NUMBER IS PALLINDROME OR NOT

(i) WAP in PL/SQL code to display multiplication tables from 5 to 10.

declare
i number(2);
n number(2);
begin
n:=&n;
for i in 1..10 loop
dbms_output.put_line( n || ' * ' || i || ' = ' || n*i);
end loop;
end;

output
Enter value for n: 5
old 6: n:=&n;
new 6: n:=5;
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

(ii) WAP in PL/SQL to check the given number is palindrome or not.


declare

-- declare variable n, m, temp


-- and temp of datatype number
n number;
m number;
temp number:=0;
rem number;

begin
n:=5432112345;
m:=n;

-- while loop with condition till n>0


while n>0
loop
rem:=mod(n,10);
temp:=(temp*10)+rem;
n:=trunc(n/10);
end loop; -- end of while loop here

if m = temp
then
dbms_output.put_line('true');
else
dbms_output.put_line('false');
end if;
end;
/

Output:
True
Ex.No.28 CREATION OF TABLES USING PRIMARY KEY AND SUBQUERIES

AIM:Create the following tables


Student(roll-no,name,date-of-birth,course-id)
Course(Course-id,name,fee,duration)
(a)insert atleast 8 records into the respective tables.
(b) Generate queries to do the following :
(i) List all those students who are greater than 18 years of age
and have opted for ME course.
(ii) List all those courses whose fee is less than that of MCA course.

SQL > CREATE TABLE student( roll char(10) PRIMARY KEY , dob date, name varchar2(10),courseid char(10));
table created.
SQL > CREATE TABLE course(courseid char(10) PRIMARY KEY , name varchar2(10), fee number, durationnumber);
table created.
SQL >INSERT into course values ('c001','mca',75000,3);
1 row created.
SQL >INSERT into course values ('m003','mba',200000,2);
1 row created.
SQL >INSERT into student values ('04808015','06-aug-1978','suchitra','c001');
1 row created.
SQL >INSERT into student values ('04708015','07-sep-1991','radhika','c001');
1 row created.

(b) (i) list all student names who have opted for mca course and are above 18 yrs of age.

SQL >SELECT s.name "student list" from student "s",course "c" where
(months_between(sysdate,dob)/12)>=18 and c.name='mca' and c.courseid=s.courseid;

student li
----------
suchitra

(b)(ii) List all the course whose fee is more than course’mca’.

SQL > SELECT c.name from course "c"2 where fee > (SELECT fee from course where name='mca');

Name
----------
mba

Ex.No.29 CREATION OF PL/SQL PROCEDURE USING TRIGGER

AIM: Create the following tables :


Book(accessionno, title, publisher, year, date-of-purchase, status)
Member(member-id, name, number-of-books-issued,max-limit,status)
Book-issue(accession-no, member-id, date-of-issue)
(a) insert atleast 8 records into the respective tables
(b) Write a PL/SQL procedure to issue the book.
(i) Write a trigger to set the status of students to "not returned" if they have
(ii) taken book but not returned even after one year.
TABLE CREATION:

SQL> CREATE table BOOK1(AC_NO number PRIMARY KEY,TITLE varchar2(8),PUBLISHER varchar2(8),YEAR number,D_O_PUR
date,STATUS varchar2(17));

Table created.

SQL> CREATE table MEMBER(M_ID number PRIMARY KEY,NAME varchar2(10),NO_OF_BOOK number,M_LIMIT number,STATUS
varchar2(10));

Table created.

SQL> CREATE table BOOK_ISSUE(AC_NO number REFERENCES BOOK1(ac_no),M_ID number REFERENCES MEMBER(m_id),D_O_ISSUE
date);

Table created.

VALUE INSERTION:

SQL> INSERT into book1 VALUES('&ac_no','&title','&publisher','&year','&d_o_pur','&status');


Enter value for ac_no: 111
Enter value for title: JAVA
Enter value for publisher: ALEX
Enter value for year: 1985
Enter value for d_o_pur: 20-DEC-1999
Enter value for status:
old 1: INSERT into book1 VALUES('&ac_no','&title','&publisher','&year','&d_o_pur',upper('&status'))
new 1: INSERT into book1 VALUES('111','JAVA','ALEX','1985','20-DEC-1999','')

1 row created.
SQL> INSERT into member VALUES('&m_id','&name','&no_of_book_issued','&max_limit');
Enter value for m_id: 1
Enter value for name: LAXMI
Enter value for no_of_book_issued: 1
Enter value for max_limit: 4
old 1: INSERT into member VALUES('&m_id','&name','&no_of_book_issued','&maxlimit')
new 1: INSERT into member VALUES('1','LAXMI','1','4')

1 row created.
SQL> INSERT into book_issue VALUES('&ac_no','&m_id','&d_o_issue');
Enter value for ac_no: 111
Enter value for m_id: 5
Enter value for d_o_issue: 14-oct-2009
old 1: INSERT into book_issue VALUES('&ac_no','&m_id','&d_o_issue')
new 1: INSERT into book_issue VALUES('111','5','14-oct-2009')

1 row created.

TABLE VALUES:

SQL> SELECT * from book1;

AC_NO TITLE PUBLISHE YEAR D_O_PUR STATUS


---------- -------- -------- ---------- --------- --------------------------------------------
111 JAVA ALEX 1985 20-DEC-99
112 ORACLE SMITH 1999 12-APR-01
113 C CLARK 1986 16-MAR-88
114 C++ PONTING 1995 23-SEP-88
115 DBMS SYMONDS 2005 17-JUL-06

SQL> SELECT * from member;

M_ID NAME NO_OF_BOOK M_LIMIT STATUS


---------- ---------- ---------- -----------------------------------------
1 LAXMI 1 4
2 BALAJI 1 4
3 RAMU 2 4
4 RAJU 1 4
5 BABU 4 4
6 BHEEM 0 2

SQL> SELECT * from book_issue;

AC_NO M_ID D_O_ISSUE


---------- ---------- ----------------------

TRIGGER:

CREATE OR REPLACE Trigger trg11 AFTER insert or update on book_issue

DECLARE
CURSOR c1 is SELECT d_o_issue,m_id from book_issue;
Begin
for i in c1
loop
if ADD_MONTHS(i.d_o_issue,12)<=sysdate then
UPDATE member set status='back listed' where m_id=i.m_id;
end if;
end loop;
End;
/
PL/SQL PROCEDURE with name pro1:

CREATE OR REPLACE PROCEDURE pro1 AS


acno book_issue.ac_no%type :='&accssion_no';
mid book_issue.m_id%type :='&member_id';
doi book_issue.d_o_issue%type:='&date_of_issue';
m number;
n number;

BEGIN
SELECT no_of_book,m_limit into n,m from member WHERE m_id=mid;

if m>n then
INSERT into book_issue VALUES(acno,mid,doi);
dbms_output.put_line('VALUE SUCCESSFULLY INSERTED');
n:=n+1;
else
dbms_output.put_line('MAX LIMIT REACHED FOR THIS MEMBER');
end if;
UPDATE member set no_of_book=n WHERE m_id=mid;
END pro1;
/

CREATION:

SQL> @pro1
Enter value for accssion_no: 115
Enter value for member_id: 1
Enter value for date_of_issue: 23-sep-2008
old 5: Insert into book_issue values(&accssion_no,&member_id,'&date_of_issue')
new 5: Insert into book_issue values(115,1,'23-sep-2008');

Procedure created.

EXECUTION:

SQL> EXECUTE pro1


VALUE SUCCESSFULLY INSERTED

PL/SQL procedure successfully completed.


OUTPUT:

SQL> SELECT * FROM BOOK_ISSUE;

AC_NO M_ID D_O_ISSUE


---------- ---------- ---------------------
115 1 23-SEP-08
114 2 19-SEP-09
SQL> SELECT * FROM MEMBER;

M_ID NAME NO_OF_BOOK M_LIMIT STATUS


---------- ---------- ---------- ---------- --------------------------------
1 LAXMI 2 4 back listed
2 BALAJI 2 4
3 RAMU 2 4
4 RAJU 1 4
5 BABU 4 4
6 RAKHI 1 2

6 rows selected.
Ex.No.30 CREATION OF TABLES AND USING QUERIES

AIM: Create the following tables :


Branch (branch-id, branch-name, branch-city)
Customer (customer-id, customer-name, customer-city, branch-id)
(a) insert atleast 8 records into the respective tables
(b) Generate queries to do the following :
(i) List all those customers who live in the same city as the
branch in which they have account.
(ii) List all those customers who have an account in only one
branch.

SQL > CREATE TABLE branch( branchid char(5) PRIMARY KEY ,name varchar2(10),city varchar2(10));
table created.
SQL > CREATE TABLE customer(custid char(5) PRIMARY KEY , name varchar2(20),city varchar2(10),branchid
char(5));
table created.
SQL >INSERT into branch values('b002','chikkad','hyd');
1 row created.
SQL >INSERT into branch values('b003','marred','sec');
1 row created.
SQL >INSERT into customer values('c002','suchitra2','hyd','b002');
1 row created.
SQL >INSERT into customer values('c003','suchitra3','hyd','b002');
1 row created.
SQL >INSERT into customer values('c004','suchitra4','hyd','b003');
1 row created.

List all those customers who live in the same city as the branch in which they have account.

SQL > SELECT customer.name, branch.name from branch,customer where branch.branchid=customer.branchid


and customer.city=branch.city;
name name
---------------- ----------
suchitra2 chikkad
suchitra3 chikkad

List all customers who have an account in a given branch city.

SQL > SELECT customer.name from customer,branchwhere customer.branchid=branch.branchid and


branch.city='sec';
Name
--------------------
suchitra4

You might also like