0% found this document useful (0 votes)
206 views

Dbms Record1

The document describes creating tables to store employee, client, and book data in an Oracle database. It includes the SQL commands to: 1. Create tables with the appropriate columns, data types, constraints and indexes. 2. Insert records into the tables. 3. Write SQL queries to retrieve, manipulate and display data from the tables including filtering, aggregating, updating records.

Uploaded by

Johnpaul John
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
206 views

Dbms Record1

The document describes creating tables to store employee, client, and book data in an Oracle database. It includes the SQL commands to: 1. Create tables with the appropriate columns, data types, constraints and indexes. 2. Insert records into the tables. 3. Write SQL queries to retrieve, manipulate and display data from the tables including filtering, aggregating, updating records.

Uploaded by

Johnpaul John
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

DBMS LAB

1. Create a table employee using SQL command to store details of


employees such as empno, name, designation, department, gender
and salary. Specify primary key and not null constraints on the
table. Allow only ‘M’ or ‘F’ for the column gender. Department can
be sales, accounts, it. Choose designation as clerk, analyst, manager,
accountant and supervisor that depends on department.

SQL> create table employee(emp_no varchar2(10) primary key,


ename varchar2(10) not null,
designation varchar2(10),
check(designation in('clerk','analyst','manager','accountant','supervisor')),
department varchar2(10) check(department in('sales','accounts','it')),
gender varchar2(10) check(gender in('M','F')),
salary number(10,2));
Table created.

SQL> insert into employee values ('e1','prakashraj','clerk','sales','M',20000.67);


1 row created.

SQL> insert into employee values('e2','roopa','analyst','it','F',25659.50);


1 row created.

SQL> insert into employee values ('e3','akshith','manager','sales','M',30000);


1 row created.

SQL> insert into employee values


('e4','monika','accountant','accounts','F',45769.51);
1 row created.

SQL> insert into employee values ('e5','shreya','accountant','accounts','F',45000);

MGM DCK Page 1


DBMS LAB

1 row created.

SQL> insert into employee values ('e6','nikhil','supervisor','it','M',50697.62);


1 row created.

SQL> insert into employee values('e7','Neha','clerk','accounts','F',60500.);


1 row created.

SQL> insert into employee values ('e8','kanthraj','manager','sales','M',70234.5)


1 row created.

SQL> insert into employee values('e9','sharon','analyst','it','M',40694.89);


1 row created.

SQL> insert into employee values('e10','vinith','supervisor','it','M',50000);


1 row created.

Write the following sql queries:-


a) Display empno, name and designation of all employees whose
name ends with raj.
SQL> select emp_no,ename,designation from employee where ename like'%raj';

EMP_NO ENAME DESIGNATION


---------------------------------------------------
e1 prakashraj clerk
e8 kanthraj manager

MGM DCK Page 2


DBMS LAB

b) Display the details of all female employees who is earning salary


within the range 20000 to 40000 in sales or it departments.
SQL> select *from employee where gender='F' and salary between 20000 and
40000 and department in('it','sales');

EMP_NO ENAME DESIGNATION DEPARTMENT GENDER SALARY


---------------------------------------------------------------------------------------------------
e2 roopa analyst it F 25659.5

c) List the different departments with the designations in that


department.
SQL> select distinct department,designation from employee;
DEPARTMENT DESIGNATION
--------------------------------------------
sales clerk
it supervisor
accounts accountant
accounts clerk
it analyst
sales manager
6 rows selected.

MGM DCK Page 3


DBMS LAB

d) Display the department name, total, average, maximum,


minimum salary of the department only if the total salary given in
that department is more than 30000.
SQL> select department,
sum(salary)total,avg(salary)average,min(salary)minimum,max(salary)maximum
from employee group by department having sum(salary)>30000;

DEPARTMENT TOTAL AVERAGE MINIMUM MAXIMUM


----------------------------------------------------------------------------------------
accounts 151269.51 50423.17 45000 60500
it 167052.01 41763.0025 25659.5 50697.62
sales 120235.17 40078.39 20000.67 70234.5

e) List the departments which have more than employee.


SQL> select department from employee group by department having count(*)>2;

DEPARTMENT
--------------------
Accounts
it
sales

MGM DCK Page 4


DBMS LAB

2. Create a table client to store client_no, name, address, state, bal_due.


Client no must start with ‘c’. Apply the suitable structure for the
columns. Specify primary key and not null constraints on the table.
Insert 10 records.

SQL> create table client(client_no varchar2(10) primary key,


name varchar2(10) not null,
address varchar2(14),
state varchar2(14),
bal_due number(10,2),
check(client_no like 'c%'));
Table created.

SQL> insert into client values('c001','prakash','kushalanagar','karnataka',20000.67);


1 row created.

SQL> insert into client values('c002','mahesh','madikeri','kerala',49991.67);


1 row created.

SQL> insert into client values('c003','prathap','suntikoppa','goa',56443.90);


1 row created.

SQL> insert into client values('c004','prajwal','udipi','tamil nadu',89008.30);


1 row created.

SQL> insert into client values('c005','pramod','basavanahalli','andhra',94321.30);


1 row created.

SQL>insert into client values('c006','mokshi','guddehosur','madhya


pradesh',45430.53);

MGM DCK Page 5


DBMS LAB

1 row created.

SQL> insert into client values('c007','ajith','kodagarahalli','karnataka',85643.95);


1 row created.

SQL> insert into client values('c008','kavan','siddapura','karnataka',76464.47);


1 row created.

SQL> insert into client values('c009','sharon','virajpet','karnataka',66554.98);


1 row created.

SQL> insert into client values('c010','nikhil','gonikoppa','karnataka',47464.47);


1 row created.

Write the following sql queries:


a) From the table client, create a new table client1 that contains only
client_no and name, bal_due from specified state. Accept the state
during run time.
SQL> create table client1(client_no,name,bal_due)as select client_no,name,bal_due
from client where state='&state';
Enter value for state: karnataka
old 1: create table client1(client_no,name,bal_due)as select client_no,name,bal_due
from client where state='&state'
new 1: create table client1(client_no,name,bal_due)as select client_no,name,bal_due
from client where state='karnataka'
Table created.

SQL> select*from client1;

MGM DCK Page 6


DBMS LAB

CLIENT_NO NAME BAL_DUE


------------------------------------------------
c001 Prakash 20000.67
c007 ajith 85643.95
c008 kavan 76464.47
c009 sharon 66554.98
c010 nikhil 47464.47

b) Create a new table client2 that has the same structure as client but
with no records. Display the structure and records.
SQL> create table client2(client_no,name,address,state,bal_due)as select
client_no,name,address,state,bal_due from client where 1=2;
Table created.
SQL> select*from client2;
no rows selected

c) Add a new column by name penalty number (10, 2) to the client.


SQL> alter table client add(penalty number(10));
Table altered.

d)Assign penalty as 10% of bal_due for the clients c1002, c1005, c1009
and for others 8%. Display records.
SQL> update client set penalty=0.08*bal_due;
10 rows updated.
SQL> update client set penalty=0.10*bal_due where client_no in('c002','c005','c009');
3 rows updated.

MGM DCK Page 7


DBMS LAB

e) Change the name of client1 as new_client.


SQL> rename client1 to new_clients;
Table renamed.

f) Delete the table client2.


SQL> drop table client2;
Table dropped.

MGM DCK Page 8


DBMS LAB

3. Create a table book using sql command to store accession no, title,
author, publisher, year, price. apply the suitable structure for the
columns. Specify primary key and not null constraints on the table.
Insert 10 records.

SQL> create table book (ano varchar2(4) primary key,


title varchar2(14) not null,
author varchar2(15),
pub varchar2(18),
year number(4),
prize number(5,2));
Table created.

SQL> insert into book values('c01','DBMS','ramakrishan','schand',1990,745.67);


1 row created.

SQL> insert into book values('c02','c#','jennifer','abode press',1985,983.67);


1 row created.

SQL> insert into book values('c03','java','johannes','microsoft press',2000,768.89);


1 row created.

SQL> insert into book values('c04','DBMS','ramez','pearson',1999,756.67);


1 row created.

SQL> insert into book values('c05','java','bilaguru swamy','tata mc graw


hill',1989,896.89);
1 row created.

SQL> insert into book values('c06','maths','manohar','mc graw hill',2012,343.67);

MGM DCK Page 9


DBMS LAB

1 row created.

SQL> insert into book values('c07','data structure','seymour','tata mc graw


hill',2011,678.67);
1 row created.

SQL> insert into book values('c08','c++','lischutz','schaums outline',2003,456.67);


1 row created.

SQL> insert into book values('c09','dot net','shirish','schand',2009,746.67);


1 row created.

SQL> insert into book values('c10','DBMS','visual basic','schand',1997,745.67);


1 row created.

SQL> select*from book;


ANO TITLE AUTHOR PUB YEAR PRIZE
---------------------------------------------------------------------------------------------
c01 DBMS ramakrishan schand 1990 745.67
c02 c# jennifer abode press 1985 983.67
c03 java johannes microsoft press 2000 768.89
c04 DBMS ramez pearson 1999 756.67
c05 java bilaguru swamy tata mc graw hill 1989 896.89
c06 maths manohar mc graw hill 2012 343.67
c07 data structure seymour tata mc graw hill 2011 678.67
c08 c++ lischutz schaums outline 2003 456.67
c09 dot net shirish schand 2009 746.67
c10 DBMS visual basic schand 1997 745.67
10 rows selected.

MGM DCK Page 10


DBMS LAB

Write the following sql queries:


a) List the details of publishers having ‘a’ as the second character
in their names.
SQL> select pub from book where pub like '_a%';

PUB
--------------------
tata mc graw hill
tata mc graw hill

b) Display accession no, title, publisher and year of the books


published by the specified author before 2010 in the descending order
of year. Accept author during run time
SQL> select ano,title,pub,year from book where author='&author' and year<2010
order by year desc;
Enter value for author: ramakrishan
old 1: select ano,title,pub,year from book where author='&author' and year<2010
order by year desc
new 1: select ano,title,pub,year from book where author='ramakrishan' and
year<2010 order by year desc

ANO TITLE PUB YEAR


-------------------------------------------------
c01 DBMS schand 1990

MGM DCK Page 11


DBMS LAB

c) Modify the size of title to increase the size 5 characters more.


SQL> alter table book modify title varchar2(55);
Table altered.

d) Display the details of all books other than microsoft press


publishers.
SQL> select * from book where pub not in ('microsoft press');

ANO TITLE AUTHOR


--------------------------------------------------------------------
PUB YEAR PRIZE
--------------------------------------------
c01 DBMS ramakrishan
schand 1990 745.67

c02 c# jennifer
abode press 1985 983.67

c04 DBMS ramez


pearson 1999 756.67

ANO TITLE AUTHOR


--------------------------------------------------------------------
PUB YEAR PRIZE
--------------------------------------------
c05 java bilaguru swamy
tata mc graw hill 1989 896.89
c06 maths manohar

MGM DCK Page 12


DBMS LAB

mc graw hill 2012 343.67


c07 data structure seymour
tata mc graw hill 2011 678.67

ANO TITLE AUTHOR


---------------------------------------------------------------------
PUB YEAR PRIZE
--------------------------------------------
c08 c++ lischutz
schaums outline 2003 456.67

c09 dot net shirish


schand 2009 746.67

c10 DBMS visual basic


schand 1997 745.67
9 rows selected.

e) Remove the records of the books published before 1990.


SQL> delete from book where year<1990;
2 rows deleted.

SQL> select*from book;

MGM DCK Page 13


DBMS LAB

ANO TITLE AUTHOR


------------------------------------------------------------------------
PUB YEAR PRIZE
--------------------------------------------
c01 DBMS ramakrishan
schand 1990 745.67

c03 java johannes


microsoft press 2000 768.89

c04 DBMS ramez


pearson 1999 756.67

ANO TITLE AUTHOR


----------------------------------------------------------------------
PUB YEAR PRIZE
--------------------------------------------
c06 maths manohar
mc graw hill 2012 343.67

c07 data structure seymour


tata mc graw hill 2011 678.67

c08 c++ lischutz


schaums outline 2003 456.67

MGM DCK Page 14


DBMS LAB

ANO TITLE AUTHOR


------------------------------------------------------------------------
PUB YEAR PRIZE
---------------------------------------------
c09 dot net shirish
schand 2009 746.67

c10 DBMS visual basic


schand 1997 745.67
8 rows selected.

MGM DCK Page 15


DBMS LAB

4. Create a table sales with columns sno, sname, manager_name,


join_date, date_birth, salary, sales_amount and commission.
minimum age for joining the company must be 18 yrs. Default value
for commission should be 0. Apply the suitable structure for the
columns. Specify primary key and not null constraints on the table.
Insert 10 records with data except commission. Manager of manager
can be null.

SQL>create table sales(sno varchar2(5) primary key,


sname varchar2(3) not null,
mname varchar2(4),
doj date,
dob date,
salary number(7,2),
msalary number(7,2),
samount number(7,2),
com number(7) default 0,
check((doj-dob)/365>=18));
Table created.

SQL> insert into sales(sno,sname,mname,doj,dob,salary,msalary,samount)values


('s101','a','aaa','06-feb-2013','30-nov-1989',25000,10000,20000);
1 row created.

SQL> insert into sales(sno,sname,mname,doj,dob,salary,msalary,samount)values


('s102','b','bbb','06-feb-2020','12-jan-1992',12000,15000,2000);
1 row created.

SQL> insert into sales(sno,sname,mname,doj,dob,salary,msalary,samount)values


('s103','c','ccc','07-jan-2012','12-jan-1992',40000,25000,3000);
1 row created.

MGM DCK Page 16


DBMS LAB

SQL> insert into sales(sno,sname,mname,doj,dob,salary,msalary,samount)values


('s104','d','ddd','07-jan-2022','12-jan-1995',14000,20000,1000);
1 row created.

SQL> insert into sales(sno,sname,mname,doj,dob,salary,msalary,samount)values


('s105','e','eee','07-dec-2000','12-sep-1979',14000,20000,15000);
1 row created.

SQL> select*from sales;


SNO SNAME MNAME DOJ DOB SALARY MSALARY SAMOUNT COM
---------------------------------------------------------------------------------------------------------
s101 a aaa 06-FEB-13 30-NOV-89 25000 10000 20000 0
s102 b bbb 06-FEB-20 12-JAN-92 12000 15000 2000 0
s103 c ccc 07-JAN-12 12-JAN-92 40000 25000 3000 0
s104 d ddd 07-JAN-22 12-JAN-95 14000 20000 1000 0
s105 e eee 07-DEC-00 12-SEP-79 14000 20000 15000 0

Write the following sql queries:


A) Display the details of sales persons whose salary is more than
average salary in the company.

SQL> select *from sales where salary>(select avg(salary) from sales);

SNO SNAME MNAME DOJ DOB SALARY MSALARY SAMOUNT COM


---------------------------------------------------------------------------------------------------------
s101 a aaa 06-FEB-13 30-NOV-89 25000 10000 20000 0
s103 c ccc 07-JAN-12 12-JAN-92 40000 25000 3000 0

MGM DCK Page 17


DBMS LAB

B) Update commission as 20% of sales amount.


SQL> update sales set com =0.2*samount;
5 rows updated.

SQL> select*from sales;


SNO SNAME MNAME DOJ DOB SALARY MSALARY SAMOUNT COM
---------------------------------------------------------------------------------------------------------
s101 a aaa 06-FEB-13 30-NOV-89 25000 10000 20000 4000
s102 b bbb 06-FEB-20 12-JAN-92 12000 15000 2000 400
s103 c ccc 07-JAN-12 12-JAN-92 40000 25000 3000 600
s104 d ddd 07-JAN-22 12-JAN-95 14000 20000 1000 200
s105 e eee 07-DEC-00 12-SEP-79 14000 20000 15000 3000

C) Display sno, sname, manager_name, salary, commission,


manager_salary of the sales persons getting sum of salary and
commission more than salary of manager.(self join)
SQL> select sno,sname,mname,salary,com,msalary from sales where sno in(select sno
from sales where (salary+com)> msalary);

SNO SNAME MNAME SALARY COM MSALARY


----------------------------------------------------------------------------
s101 a aaa 25000 4000 10000
s103 c ccc 40000 600 25000

MGM DCK Page 18


DBMS LAB

D) Display the records of employees who finished the service of 10


years.
SQL> select*from sales where (sysdate-doj)/365>10;

SNO SNAME MNAME DOJ DOB SALARY MSALARY SAMOUNT COM

s101 a aaa 06-FEB-13 30-NOV-89 25000 10000 20000 4000


s103 c ccc 07-JAN-12 12-JAN-92 40000 25000 3000 600
s105 e eee 07-DEC-00 12-SEP-79 14000 20000 15000 3000

MGM DCK Page 19


DBMS LAB

5. Create a table sales_details with the columns sno, month, target and
qty_sold to store the sales details of one year. Specify the composite
primary key to the columns sno and month. Target and sales must be
positive numbers.

SQL> create table sales_details(sno varchar2(10),


month varchar2(5),
target number(10),
qtysold number(10),
primary key(sno,month),
check(target>0 and qtysold>0));
Table created.

SQL> insert into sales_details values('s0001','jan',50,70);


1 row created.

SQL> insert into sales_details values('s0001','feb',100,60);


1 row created.

SQL> insert into sales_details values('s0001','mar',100,120);


1 row created.

SQL> insert into sales_details values('s0002','apr',100,80);


1 row created.

SQL> insert into sales_details values('s0003','apr',100,80);


1 row created.

SQL> insert into sales_details values('s0003','nov',100,150);


1 row created.

SQL> insert into sales_details values('s0004','nov',100,200);

MGM DCK Page 20


DBMS LAB

1 row created.

SQL> commit;
Commit complete
SQL> select * from sales_details;

SNO MONTH TARGET QTYSOLD


---------------------------------------------------------
s0001 jan 50 70
s0001 feb 100 60
s0001 mar 100 120
s0002 apr 100 80
s0003 apr 100 80
s0003 nov 100 150
s0004 nov 100 200
7 rows selected.

Write the following sql queries:


a)Display the total sales by each sales person considering only those
months sales where target was reached.
SQL> select sno,sum(qtysold) totalsales from sales_details where qtysold>=target
group by sno;

SNO TOTALSALES
-------------------------------
s0001 190
s0003 150
s0004 200

MGM DCK Page 21


DBMS LAB

B) If a commission of Rs. 50 provided for each item after reaching


target, Calculate and Display the total commission for each sales
person.
SQL> select sno, sum(qtysold*50) as commision from sales_details where
qtysold>=target group by sno;

SNO COMMISION
-------------------------------
s0001 9500
s0003 7500
s0004 10000

C) Display the sno of those who never reached the target.


SQL> select sno from sales_details where qtysold<target;

SNO
------
s0001
s0002
s0003

d) Dispaly the sno, month and qtysold of the sales persons with sno
s0001 or s0003.
SQL> select sno,month,qtysold from sales_details where sno in('s0001','s0003');
SNO MONTH QTYSOLD
-----------------------------------------
s0001 feb 60
s0001 jan 70
s0001 mar 120
s0003 apr 80
s0003 nov 150

MGM DCK Page 22


DBMS LAB

6. Create a table bank with the columns acno, act_name, act_type and
bal. specify the primary key. Initial bal must be greater than 500.
Write a pl/sql program to perform debit operation by providing
acct_no and amount required. the amount must be greater than 100
and less than 20000 for one transaction. If the account exist and bal-
amount>100 bank table must be updated, otherwise “no suffficient
balance” message should be displayed. If account number is not
present then display “no such account” message to the user.

SQL> create table bank(acno number(5) primary key,


actname varchar2(10),
acttype varchar2(5),
bal number(10,2),
check(bal>500));
Table created.

SQL> insert into bank values(101, 'a', 'sb', 2000);


1 row created.

SQL> insert into bank values(102, 'b', 'sb', 10000);


1 row created.

SQL> insert into bank values(103, 'c', 'sb', 1200);


1 row created.

SQL> insert into bank values(104, 'd', 'sb', 12000);


1 row created.

SQL> select * from bank;

MGM DCK Page 23


DBMS LAB

ACNO ACTNAME ACTTYPE BAL


------------------------------------------------------------
101 a sb 2000
102 b sb 10000
103 c sb 1200
104 d sb 12000

SQL> set serveroutput on;

declare
ano number(10);
balance number(10,2);
amount number(10,2);
begin
ano:=&ano;
amount:=&amount;
select acno into ano from bank where acno=ano;
if sql%found then
if amount>100 and amount<20000 then
select bal into balance from bank where acno=ano;
balance:=balance-amount;
if balance>500 then
update bank set bal=bal-amount where acno=ano;
dbms_output.put_line('transaction successful');
commit;
else
dbms_output.put_line('no sufficient balance');
end if;
else
dbms_output.put_line('amount must be greater than 100 and less than 20000');
end if;
end if;

MGM DCK Page 24


DBMS LAB

exception
when no_data_found then
if(sql%notfound)then
dbms_output.put_line('account does not exist');
end if;
end;
/

OUTPUT 1

Enter value for ano: 102


old 6: ano:=&ano;
new 6: ano:=102;
Enter value for amount: 1000
old 7: amount:=&amount;
new 7: amount:=1000;
transaction successful
PL/SQL procedure successfully completed.

OUTPUT 2

Enter value for ano: 106


old 6: ano:=&ano;
new 6: ano:=106;
Enter value for amount: 500
old 7: amount:=&amount;
new 7: amount:=500;
account does not exist
PL/SQL procedure successfully completed.

MGM DCK Page 25


DBMS LAB

OUTPUT 3

Enter value for ano: 101

old 6: ano:=&ano;

new 6: ano:=101;

Enter value for amount: 5000

old 7: amount:=&amount;

new 7: amount:=5000;

no sufficient balance

PL/SQL procedure successfully completed.

OUTPUT 4

Enter value for ano: 104

old 6: ano:=&ano;

new 6: ano:=104;

Enter value for amount: 20

old 7: amount:=&amount;

new 7: amount:=20;

amount must be greater than 100 and less than 20000

PL/SQL procedure successfully completed.

MGM DCK Page 26


DBMS LAB

7. Create a table stock_detail with the columns pno, pname and


qty_avl to store stock details of computer accessories. Specify
primary key and not null constraints on the table.
qty_avl should be positive number.
Write a PL/SQL program to define a user defined exception named
“low_stock” to validate the transaction. The program facilitates the
user to purchase the product by providing product number and
quantity required. It should display an error message “no sufficient
stock” when the user tries to purchase a product with quantity more
than qty_avl, otherwise the stock_detail table should be updated for
valid transaction.

SQL> create table stock_detail(pno varchar2(5) primary key,


pname varchar2(10) not null,
qtyavail number(5),
check((qtyavail)>0));
Table created.

SQL> insert into stock_detail values('p101','keyboard',20);


1 row created.

SQL> insert into stock_detail values('p102','mouse',20);


1 row created.

SQL> insert into stock_detail values('p103','ram',30);


1 row created.

SQL> insert into stock_detail values('p104','hard disk',20);


1 row created.

MGM DCK Page 27


DBMS LAB

SQL> set serveroutput on;


SQL> declare
pnumber varchar2(5);
qtyreq number(5);
stock number(5);
low_stock exception;
begin
pnumber:='&pnumber';
qtyreq:=&qtyreq;
select qtyavail into stock from stock_detail where pno=pnumber;
if(sql%found) then
if stock>=qtyreq then
update stock_detail set qtyavail=qtyavail-qtyreq where pno=pnumber;
dbms_output.put_line('stock updated');
commit;
else
raise low_stock;
end if;
end if;
exception
when low_stock then
dbms_output.put_line('no sufficient stock');
when no_data_found then
if(sql%notfound) then
dbms_output.put_line('product not found');
end if;
end;
/

MGM DCK Page 28


DBMS LAB

OUTPUT1:
Enter value for pnumber: p101
old 7: pnumber:='&pnumber';
new 7: pnumber:='p101';
Enter value for qtyreq: 3
old 8: qtyreq:=&qtyreq;
new 8: qtyreq:=3;
stock updated

PL/SQL procedure successfully completed.

OUTPUT2:
SQL> /
Enter value for pnumber: p104
old 7: pnumber:='&pnumber';
new 7: pnumber:='p104';
Enter value for qtyreq: 60
old 8: qtyreq:=&qtyreq;
new 8: qtyreq:=60;
no sufficient stock

PL/SQL procedure successfully completed.

MGM DCK Page 29


DBMS LAB

OUTPUT3:
SQL> /
Enter value for pnumber: p107
old 7: pnumber:='&pnumber';
new 7: pnumber:='p107';
Enter value for qtyreq: 7
old 8: qtyreq:=&qtyreq;
new 8: qtyreq:=7;
product not found

PL/SQL procedure successfully completed.

MGM DCK Page 30


DBMS LAB

1. Create the following tables by identifying primary and foreign


keys. Specify the not null property for mandatory keys. Suppliers
(supplier_no, sname, saddress, scity) computer_items (item_no,
supplier_no, item_name, iquantity) consider three suppliers. a
supplier can supply more than one type of items. Write the sql
queries for the following.

SQL> create table supplier(sno varchar2(5) primary key,


sname varchar2(14) not null,
saddr varchar2(11),
scity varchar2(11));
Table created.

SQL> insert into supplier values('s01','microtech','mysore','bangalore');


1 row created.

SQL> insert into supplier values('s02','cats','bombay','mumbai');


1 row created.

SQL> insert into supplier values('s03','electrotech','madras','chennai');


1 row created.

SQL> insert into supplier values('s04','mice','knagar','kodagu');


1 row created.

SQL> insert into supplier values('s05','electrotech','udupi','mangalore');


1 row created.

SQL> select*from supplier;

MGM DCK Page 31


DBMS LAB

SNO SNAME SADDR SCITY


---------------------------------------------------
s01 microtech mysore bangalore
s02 cats bombay mumbai
s03 electrotech madras chennai
s04 mice knagar kodagu
s05 electrotech udupi mangalore

SQL> create table citems(ino varchar2(5) primary key,


sno varchar2(5) references supplier(sno),
iname varchar2(10) not null,
iqty number(4));
Table created.

SQL> insert into citems values('i01','s01','keyboard',4);


1 row created.

SQL> insert into citems values('i02','s02','mouse',5);


1 row created.

SQL> insert into citems values('i03','s03','printer',10);


1 row created.

SQL> insert into citems values('i04','s04','mouse',20);


1 row created.

SQL> insert into citems values('i05','s05','keyboard',10);


1 row created.

MGM DCK Page 32


DBMS LAB

SQL> insert into citems values(‘i06’,’s01’,’monitor’,10);


1 row created.

SQL> select*from citems;


INO SNO INAME IQTY
------------------------------------------
i01 s01 keyboard 4
i02 s02 mouse 5
i03 s03 printer 10
i04 s04 mouse 20
i05 s05 keyboard 10
i06 s01 monitor 10
6 rows selected.

Write the following sql queries:


a) List item and supplier details in alphabetical order of city name and
in each city decreasing order of iquantity.
SQL> select i.ino,i.iname,s.sno,s.sname,s.scity,i.iqty
from citems i,supplier s
where i.sno=s.sno
order by s.scity, i.iqty desc;
INO INAME SNO SNAME SCITY IQTY
--------------------------------------------------------------------------
i06 monitor s01 microtech bangalore 10
i01 keyboard s01 microtech bangalore 4
i03 printer s03 electrotech chennai 10
i04 mouse s04 mice kodagu 20
i05 keyboard s05 electrotech mangalore 10
i02 mouse s02 cats mumbai 5

MGM DCK Page 33


DBMS LAB

6 rows selected.

b) List the name ,city,and address of the suppliers who are supplying
keyboard.
SQL> select sname, scity, saddr from supplier where sno in(select sno from citems
where iname='keyboard');

SNAME SCITY SADDR


------------------------------------------
microtech bangalore mysore
electrotech mangalore udupi

c) List the supplier name, items supplied by the suppliers ‘cats’ and
‘electrotech’.
SQL> select s.sname, i.iname from supplier s, citems i where s.sno=i.sno and s.sname in
('cats','electrotech');

SNAME INAME
---------------------------
cats mouse
electrotech printer
electrotech keyboard

MGM DCK Page 34


DBMS LAB

d) Find the items having quantity less than 5 and insert the details of
supplier and item of these, into another table neworder.
SQL> create table neworder as select s.sno,s.sname,s.saddr,s.scity,i.ino,i.iname,i.iqty
from supplier s, citems i where s.sno=i.sno and i.iqty<5;
Table created.

SQL> select*from neworder;

SNO SNAME SADDR SCITY INO INAME IQTY


------------------------------------------------------------------------------------------
s01 microtech mysore bangalore i01 keyboard 4

MGM DCK Page 35


DBMS LAB

2. Create the following tables by identifying primary and foreign keys,


specify the not null property for mandatory keys.

SQL>create table product(pno varchar2(7) primary key,


pname varchar2(10) not null,
qtyavail number(6),
price number(6),
profit number(6));
Table created.

SQL> insert into product values ('p0001','monitor',10,3000,20);


1 row created.

SQL> insert into product values ('p0002','pendrive',50,650,5);


1 row created.

MGM DCK Page 36


DBMS LAB

SQL> insert into product values ('p0003','cd drive',100,10,3);


1 row created.

SQL> insert into product values ('p0004','keyboard',25,600,10);


1 row created.

SQL> create table purchased(cno varchar2(6) not null,


pno varchar2(6) references product (pno),
qtysold number(5));
Table created.

SQL> insert into purchased values ('c1','p0003',2);


1 row created.

SQL> insert into purchased values ('c2','p0002',4);


1 row created.

SQL> insert into purchased values ('c3','p0002',10);


1 row created.

SQL> insert into purchased values ('c4','p0001',3);


1 row created.

SQL> insert into purchased values ('c1','p0004',2);


1 row created.

SQL> insert into purchased values ('c2','p0003',2);


1 row created.

MGM DCK Page 37


DBMS LAB

SQL> insert into purchased values ('c4','p0004',1);


1 row created.

Write the following sql queries:


a) Display total amount spent by c2.
SQL> select sum(pu.qtysold*pr.price)total_amt from product pr,
purchased pu where pr.pno=pu.pno and pu.cno='c2';

TOTAL_AMT
-----------------
2620

b) Display the names of product for which either qtyavailable is less


than 30 or total qtysold is less than 5 (use union).
SQL> select pname from product where qtyavail<30
union
select pname from product where pno in(select pno from purchased group by pno
having sum(qtysold)<5);

PNAME
----------
cd drive
keyboard
monitor

MGM DCK Page 38


DBMS LAB

c) Display the name of products and quantity purchased by c4.


SQL> select pr.pname,pu.qtysold from product pr,purchased pu where pr.pno=pu.pno
and pu.cno='c4';

PNAME QTYSOLD
-------------------------------
monitor 3
keyboard 1

d) How much profit does the shopkeeper gets on c1’s purchase?


SQL> select sum(pu.qtysold*pr.price*pr.profit/100)profit_of_c1 from product
pr,purchased pu where pr.pno=pu.pno and pu.cno='c1';

PROFIT_OF_C1
-------------------
120.6

e) How many ‘pen drives’ have been sold?


SQL> select sum(qtysold) from purchased where pno in(select pno from product
where pname='pendrive');

SUM(QTYSOLD)
---------------------
14

MGM DCK Page 39


DBMS LAB

3. Create table student_profile includes rollno, name, class, eccc


(extra-co curricular he belongs to such as sports, nss etc). And
another table marks_report includes rollno, internal_test, marks1,
marks2, marks3 and eccc_marks.
Constraints
• Internal _test can be either 1 or 2.
• Each mark can be 0-100. absence in the test can be entered as -1.
• Consider atleast 3 classes.
Apply suitable data type and constraints to each column.
Insert 5 students marks report in the both the tests.

SQL> create table student(regno varchar2(4) primary key,name varchar2(4),class


varchar2(10),eccc varchar2(10));
Table created.

SQL> insert into student values('s1','a','I bca','sports');


1 row created.

SQL> insert into student values('s2','b','II bca','nss');


1 row created.

SQL> insert into student values('s3','c','III bca','ncc');


1 row created.

SQL> insert into student values('s4','d','I bca','ncc');


1 row created.

SQL> insert into student values('s5','e','II bca','sports');


1 row created.

MGM DCK Page 40


DBMS LAB

SQL> insert into student values('s6','f','III bca','sports');


1 row created.

SQL> select*from student;

REGN NAME CLASS ECCC


----------------------------------------
s1 a I bca sports
s2 b II bca nss
s3 c III bca ncc
s4 d I bca ncc
s5 e II bca sports
s6 f III bca sports
6 rows selected.

SQL> create table marks(regno varchar2(4) references student(regno),test


number(5),m1 number(5),m2 number(5),m3 number(5),eccmarks
number(5),check(test=1 or test=2), check((m1 between -1 and 100) and (m2
between -1 and 100) and (m3 between -1 and 100) and (eccmarks between -1 and
100)));
Table created.

SQL> insert into marks values('s1',1,46,57,89,90);


1 row created.

SQL> insert into marks values('s1',2,46,57,89,-1);


1 row created.

SQL> insert into marks values('s2',1,90,45,89,85);


1 row created.

MGM DCK Page 41


DBMS LAB

SQL> insert into marks values('s2',2,90,59,89,95);


1 row created.

SQL> insert into marks values('s3',1,70,75,89,98);


1 row created.

SQL> insert into marks values('s3',2,67,85,98,98);


1 row created.

SQL> insert into marks values('s4',1,10,85,98,98);


1 row created.

SQL> insert into marks values('s4',2,60,85,98,80);


1 row created.

SQL> insert into marks values('s5',1,60,15,98,80);


1 row created.

SQL> insert into marks values('s5',2,40,35,98,80);


1 row created.

SQL> insert into marks values('s6',1,-1,35,98,80);


1 row created.

SQL> insert into marks values('s6',2,89,35,98,80);


1 row created.

SQL> select*from marks;

MGM DCK Page 42


DBMS LAB

REGN TEST M1 M2 M3 ECCMARKS


---------------------------------------------------------------------------
s1 1 46 57 89 90
s1 2 46 57 89 -1
s2 1 90 45 89 85
s2 2 90 59 89 95
s3 1 70 75 89 98
s3 2 67 85 98 98
s4 1 10 85 98 98
s4 2 60 85 98 80
s5 1 60 15 98 80
s5 2 40 35 98 80
s6 1 -1 35 98 80
s6 2 89 35 98 80
12 rows selected.

a) Find number of students failed class- wise.


SQL> select s.class, count (m.regno) from marks m, student s where
m.regno=s.regno and (m.m1<35 or m.m2<35 or m.m3<35 or m.eccmarks<35)
group by s.class;

CLASS COUNT(M.REGNO)
-----------------------------------------
I bca 2
III bca 1
II bca 1

MGM DCK Page 43


DBMS LAB

b) Display the complete details of the students secured


distinction(percentage>=70) in i bca.
SQL> select s.regno, s.name, s.class, m.test,m.m1 ,m.m2, m.m3 ,m.eccmarks
from student s, marks m where s.regno=m.regno and not(m.m1<35 or m.m2<35
or m.m3<35 or m.eccmarks<35) and (m.m1+m.m2+m.m3+m.eccmarks) /4>70
and s.class='I bca';

REGN NAME CLASS TEST M1 M2 M3 ECCMARKS


---------------------------------------------------------------------------------------------
s1 a I bca 1 46 57 89 90
s4 d I bca 2 60 85 98 80

c) Display class and highest total marks in second internals in


each class.
SQL> select s.class, max(m.m1+m.m2+m.m3+m.eccmarks) as maxtotal from
student s, marks m where s.regno= m.regno and m.test=2 group by s.class;
CLASS MAXTOTAL
---------------------------------
I bca 323
III bca 348
II bca 333

d) Display the student name with rollno and class of those who
passed in I internals and failed in II internals. (use set operator).
SQL>select s.regno, s.name, s.class from student s, marks m
where s.regno=m.regno and m.test=1 and m.m1>35 and m.m2>35 and m.m3>35
and m.eccmarks>35
intersect

MGM DCK Page 44


DBMS LAB

select s.regno,s.name,s.class from student s, marks m where s.regno=m.regno and


m.test=2 and (m.m1<35 or m.m2<35 or m.m3<35 or m.eccmarks<35);

REGN NAME CLASS


---------------------------------
s1 a I bca

MGM DCK Page 45


DBMS LAB

4.Write a PL/SQL program to compute the selling price of books


depending on the book code and category. Use Open, Fetch and
Close. The Book_detail table contains columns: Book Code, Author,
Title, Category and Price.
Insert 10 records.
The selling price=Price-Discount.
The discount is calculated as follows:

SQL> create table book(bcode varchar2(5),


author varchar2(20),
title varchar2(20),
category varchar2(20),
price number(10,2));
Table created.

SQL> insert into book values('A','ram','c++','novels',500);


1 row created.

SQL> insert into book values('A','raj','c#','technology',900);


1 row created.

MGM DCK Page 46


DBMS LAB

SQL> insert into book values ('B','raghu','phython','commerce',700);


1 row created.

SQL> insert into book values ('B','roshan','sci','science',800);


1 row created.

SQL> insert into book values('C','rishab','java','songs',970);


1 row created.

SQL> insert into book values('C','rohan','vb','sports',300);


1 row created.

SQL> insert into book values('D','raju','ds','all',400);


1 row created.

SQL> set serveroutput on;


SQL> declare
bc varchar2(5);
athr varchar2(10);
name varchar2(20);
ctgry varchar2(20);
cost number(10,2);
disc number(10,2);
sp number(10,2);
cursor bk is select bcode,author,title,category,price from book;
begin
open bk;
dbms_output.put_line('BOOK CODE'||' '||'AUTHOR'||' '||'TITLE'||'
'||'CATEGORY'||' '||'PRICE'||'DISCOUNT'||' '||'SELLING PRICE');

MGM DCK Page 47


DBMS LAB

dbms_output.put_line('----------------------------------------------------');
loop
fetch bk into bc,athr,name,ctgry,cost;
exit
when bk%notfound;
if bc='A' and ctgry='novels' then
disc:=0.1*cost;
end if;
if bc='A' and ctgry='technology' then
disc:=0.125*cost;
end if;
if bc='B' and ctgry='commerce' then
disc:=0.18*cost;
end if;
if bc='B' and ctgry='science' then
disc:=0.19*cost;
end if;
if bc='C' and ctgry='songs' then
disc:=0.25*cost;
end if;
if bc='C' and ctgry='sports' then
disc:=0.24*cost;
end if;
if bc='D' and ctgry='all' then
disc:=0.28*cost;
end if;
sp:=cost-disc;
dbms_output.put_line(bc||' '||athr||' '||name||' '||ctgry||' '||cost||' '||disc||' '||sp);
end loop;

MGM DCK Page 48


DBMS LAB

close bk;
end;
/
BOOK CODE AUTHOR TITLE CATEGORY PRICE DISCOUNT SELLING PRICE -
-------------------------------------------------------------------------------------------------------------------

A ram c++ novels 500 50 450


A raj c# technology 900 112.5 787.5
B raghu phython commerce 700 126 574
B roshan sci science 800 152 648
C rishab java songs 970 242.5 727.5
C rohan vb sports 300 72 228
D raju ds all 400 112 288

PL/SQL procedure successfully completed.

MGM DCK Page 49


DBMS LAB

5. Write a PL/SQL program to display employee pay bill (using


Cursor For loop) Use a Procedure to receive basic pay and to compute
DA, HRA, Tax, PF, Gross Pay and Net Pay(Use OUT). Base table
contains the following columns empnum, empname, basic pay.
Insert 3 records.
Allowances are computed as follows.

SQL> create table employee(eno varchar2(5) primary key,


ename varchar2(10),
basic number(10,2));
Table created.

SQL> insert into employee values('101','A',20000);


1 row created.

SQL> insert into employee values('102','B',25000);


1 row created.

SQL> insert into employee values('103','C',30000);


1 row created.

MGM DCK Page 50


DBMS LAB

SQL> insert into employee values('104','D',40000);


1 row created.

SQL> insert into employee values('105','E',5000);


1 row created.

SQL> create or replace procedure compute(bp in number,da out number,hra out


number,pf out number,gross out number,pt out number,net out number)is
begin
if bp<=20000 then
da:=0.35*bp;
hra:=0.08*bp;
end if;
if bp>20000 and bp<=30000 then
da:=0.38*bp;
hra:=0.09*bp;
end if;
if bp>30000 and bp<=40000 then
da:=0.4*bp;
hra:=0.1*bp;
end if;
if bp>40000 then
da:=0.4*bp;
hra:=0.1*bp;
end if;
gross:=bp+da+hra;
pf:=0.12*gross;
if pf>2000 then

MGM DCK Page 51


DBMS LAB

pf:=2000;
end if;
if gross<=25000 then
pt:=100;
else
pt:=200;
end if;
net:=gross-(pf+pt);
end;
/
Procedure created.

SQL> set serveroutput on;


SQL> declare
da number(10,2);
hra number(10,2);
gross number(10,2);
pf number(10,2);
pt number(10,2);
net number(10,2);
cursor emp is select eno,ename,basic from employee;
begin
for e in emp
loop
compute(e.basic,da,hra,pf,gross,pt,net);
dbms_output.put_line('----------- PAYSLIP--------------');
dbms_output.put_line('eno:'||e.eno);
dbms_output.put_line('ename:'||e.ename);
dbms_output.put_line('basic:'||e.basic);

MGM DCK Page 52


DBMS LAB

dbms_output.put_line('da:'||da);
dbms_output.put_line('hra:'||hra);
dbms_output.put_line('gross:'||gross);
dbms_output.put_line('pf:'||pf);
dbms_output.put_line('pt:'||pt);
dbms_output.put_line('net:'||net);
dbms_output.put_line('---------------------------------------');
end loop;
end;
/

OUTPUT:

---------------------- PAYSLIP -----------------------


eno:101
ename:A
basic:20000
da:7000
hra:1600
gross:28600
pf:2000
pt:200
net:26400
----------------------------------------------------------

MGM DCK Page 53


DBMS LAB

---------------------- PAYSLIP -----------------------


eno:102
ename:B
basic:25000
da:9500
hra:2250
gross:36750
pf:2000
pt:200
net:34550
------------------------------------------------------------
---------------------- PAYSLIP -------------------------
eno:103
ename:C
basic:30000
da:11400
hra:2700
gross:44100
pf:2000
pt:200
net:41900
--------------------------------------------------------------

MGM DCK Page 54


DBMS LAB

---------------------- PAYSLIP ---------------------------


eno:104
ename:D
basic:40000
da:16000
hra:4000
gross:60000
pf:2000
pt:200
net:57800
----------------------------------------------------------------
----------------------PAYSLIP-----------------------------
eno:105
ename:E
basic:5000
da:1750
hra:400
gross:7150
pf:858
pt:100
net:6192
---------------------------------------------------------------
PL/SQL procedure successfully completed.

MGM DCK Page 55


DBMS LAB

6. Given the following tables:


ITEM_MASTER(itemno, name, stock, unit_price) [Apply the
Primary key and check constraint for stock and price as >0] [Insert 5
records]
ITEM_TRANS(itemno, quantity and trans_date)

Create a package PCK_ITEM includes a function CHK_ITEM and


a procedure PROC_ITEM.

Function CHK_ITEM gets one argument itemno and is used to


check whether the parameter itemno exists in ITEM_MASTER and
should return 1 if exist. Otherwise 0 and displays proper message.

Procedure PROC_ITEM gets two arguments itemno and quantity,


and is used to perform the following if item exists. If required
quantity is not available, give appropriate message. If available ,
insert a record of this transaction to ITEM_TRANS and modify the
stock in ITEM_MASTER.
Write a PL/SQL program to accept ITEM_NO and Quantity
needed of required item. Use Package to do the transaction process
(Transaction date can be current date).
OUTPUT to be shown as follows:

SQL> create table item_master(ino varchar2(5) primary key, iname varchar2(10),


stock number(10),price number(10), check(stock>0 and price>0));
Table created.

SQL> insert into item_master values('i101','keyboard', 100, 620);

MGM DCK Page 56


DBMS LAB

1 row created.

SQL> insert into item_master values('i102','mouse', 50, 220);

1 row created.

SQL> insert into item_master values('i104','pen drive', 75, 800);

1 row created.

SQL> insert into item_master values('i103','hard disk', 150, 2000);

1 row created.

SQL> create table item_trans(ino varchar2(5) references item_master(ino), qty


number(10), tdate date);

Table created.

SQL>create package pck_item is

function chk_item(inumber in varchar2) return number;

procedure proc_item(inumber in varchar2, qty in number);

end pck_item;

Package created.

create or replace package body pck_item is

function chk_item(inumber in varchar2) return number is

c varchar2(5);

begin

select ino into c from item_master where ino=inumber;

MGM DCK Page 57


DBMS LAB

return 1;

exception

when no_data_found then

return 0;

end;

procedure proc_item(inumber in varchar2,qty in number) is

itemno varchar2(5);

qtyavail number(10);

begin

select ino,stock into itemno,qtyavail from item_master where ino=inumber;

if qty>qtyavail then

dbms_output.put_line('low stock');

else

insert into item_trans values(itemno,qty,sysdate);

update item_master set stock=stock-qty where ino=inumber;

dbms_output.put_line('record added successfully');

end if;

end;

end pck_item;

Package body created.

declare

itemno varchar2(5);

qty number(10);

d number(5);

begin

MGM DCK Page 58


DBMS LAB

itemno:='&itemno';

qty:=&qty;

d:=pck_item.chk_item(itemno);

if d=1 then

dbms_output.put_line('item found');

pck_item.proc_item(itemno,qty);

else

dbms_output.put_line('item not found');

end if;

end;

Output 1
Enter value for itemno: i101

old 6: itemno:='&itemno';

new 6: itemno:='i101';

Enter value for qty: 5

old 7: qty:=&qty;

new 7: qty:=5;

item found

record added successfully

PL/SQL procedure successfully completed.

Output 2
Enter value for itemno: i103

old 6: itemno:='&itemno';

new 6: itemno:='i103';

Enter value for qty: 2

MGM DCK Page 59


DBMS LAB

old 7: qty:=&qty;

new 7: qty:=2;

item found

record added successfully

PL/SQL procedure successfully completed.

SQL> select * from item_trans;

INO QTY TDATE

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

i101 5 25-FEB-23

i103 2 25-FEB-23

Output 3
Enter value for itemno: i101

old 6: itemno:='&itemno';

new 6: itemno:='i101';

Enter value for qty: 200

old 7: qty:=&qty;

new 7: qty:=200;

item found

low stock

PL/SQL procedure successfully completed.

MGM DCK Page 60


DBMS LAB

Output 4
Enter value for itemno: i109

old 6: itemno:='&itemno';

new 6: itemno:='i109';

Enter value for qty: 4

old 7: qty:=&qty;

new 7: qty:=4;

item not found

PL/SQL procedure successfully completed.

MGM DCK Page 61

You might also like