You are on page 1of 13

cl scr – command for clear screen

ed – command to edit sql command errors and create pl/sql files

set serveroutput on – command to display of pl/sql program output (use this command before the
execution of the program)

/ - command repeat the last operation


*****

1. Create a table for Employee details with Employee Number as primary key and following
fields: Name, Designation, Gender, Age, Date of Joining and Salary. Insert at least ten
rows and perform various queries using any one Comparison, Logical, Set, Sorting and
Grouping operators.

create table employee(empno number,


emp_name varchar2(35),
design varchar2(25),
gender varchar2(1),
age number,
DOJ date,
salary number(10,2),
constraint empno_pk primary key(empno));
****
desc employee
****
insert into employee values(101,'Arun','Manager','M',45,'12-AUG-1990',28500);
****
select * from employee;
****
insert into employee values (&Emp_No, '&Emp_Name', '&Designation', '&Gender', &Age,'
&Date_of_Join', &Salary);

* Use the above command to insert as many rows as needed (add atleast 10 rows)

SQL> /
Enter value for emp_no: 104
Enter value for emp_name: Deva
Enter value for designation: Senior_Clerk
Enter value for gender: M
Enter value for age: 32
Enter value for date_of_join: 30-MAR-2000
Enter value for salary: 20100
old 1: insert into employee
values(&Emp_No,'&Emp_Name','&Designation','&Gender',&Age,'&Date_of_Joi
n',&Salary)
new 1: insert into employee
values(104,'Deva','Senior_Clerk','M',32,'30-MAR-2000',20100)

1 row created.

SQL> select * from employee;

EMPNO EMP_NAME DESIGN G AGE DOJ SALARY


----- ----------- ------------- - --- --------- -------
101 Arun Manager M 45 12-AUG-90 28500
102 Barani Asst_Manager F 38 23-JAN-99 22500
103 Chandru Clerk M 29 01-JUL-03 17560
104 Deva Senior_Clerk M 32 30-MAR-00 20100

****
SQL> create table emp_table(
2 empno number,
3 DOB date);

SQL> insert into emp_table(101,'01-MAR-1970');

SQL> insert into emp_table values(102,'11-APR-1973');

SQL> select * from emp_table;

*****
 Comparison and Logical operators
SQL> select * from employee where salary >= 20000;

SQL> select * from employee where age <= 30 and age > 20;

 Group Commands

SQL> select avg(age), sum(salary), min(empno) from employee;

SQL> select avg(salary) from employee group by gender;

 Set Commands

SQL> select empno from employee union all select empno from emp_table;

SQL> select empno from employee minus select empno from emp_table;

 Sort Commands
SQL> select * from employee order by gender;

SQL> select * from employee order by salary,age;


*******

2. Create tables for library management system which demonstrate the use of primary key
and foreign key. Master table should have the following fields: Accno, Title, Author and
Rate. Transaction table should have the following fields: User id, Accno, Date of Issue and
Date of Return. Create a Report(Select verb) with fields Accno, Title, Date of Issue for the
given Date of Return with column formats.

SQL> create table lib_master(


accno number,
title varchar2(120),
author varchar2(70),
rate number,
constraint accno_pk primary key(accno));
******

create table lib_trans(acc_no number not null,


DOI date not null,
DOR date,
foreign key(acc_no)
references lib_master(accno));
*****

SQL> desc lib_master;

SQL> desc lib_trans;

SQL> insert into lib_master values(&Acc_No,'&Title of the


Book','&AUthor',&Price);

* Use the above command to insert as many rows as needed

SQL> insert into lib_master values(1001,'ANSI C


Programming','Balagurusamy',350);

SQL> insert into lib_master values(1002,'Java


Programming','Balagurusamy',450);

SQL> insert into lib_trans values(&Acc_No,'&DOI','&DOR');

* Use the above command to insert as many rows as needed (Accno should be available in
lib_master table)

*****

SQL> column accno heading 'Access|Number';


SQL> column title heading 'Title of the Book';
SQL> column doi heading 'Date of|Issue';
SQL> column dor heading 'Date of|Return';
SQL> set pagesize 20
SQL> ttitle 'Library Book Transaction Details'
SQL> btitle '-------End of the Report -------';

SQL> select accno,title,lib_trans.doi,lib_trans.dor from


lib_master,lib_trans where accno = lib_trans.acc_no;

Output (Column Report)

Sun Aug 28 page 1


Library Book Transaction Details

Access Title of the Book Date of Date of


Number Issue Return
------- --------------------------- --------- ---------
1001 ANSI C Programming 12-AUG-16 24-AUG-16

-------End of the Report -------

3. Write a PL/SQL to update the rate field by 20% more than the current rate in inventory
table which has the following fields: Prono, ProName and Rate. After updating the table a
new field (Alter) called for Number of item and place for values for the new field without
using PL/SQL block.

SQL> create table inventory(


2 prono number,
3 proname varchar2(30),
4 rate number(10,2),
5 constraint prono_pk primary key(prono));

Table created.

SQL> insert into inventory


values(&Product_Number,'&Product_Name',&Price);
Enter value for product_number: 001
Enter value for product_name: Pen
Enter value for price: 12
old 1: insert into inventory
values(&Product_Number,'&Product_Name',&Price)
new 1: insert into inventory values(001,'Pen',12)

1 row created.

SQL> /
Enter value for product_number: 002
Enter value for product_name: Pencil
Enter value for price: 5
old 1: insert into inventory
values(&Product_Number,'&Product_Name',&Price)
new 1: insert into inventory values(002,'Pencil',5)

1 row created.

SQL> /
Enter value for product_number: 003
Enter value for product_name: Scale
Enter value for price: 8
old 1: insert into inventory
values(&Product_Number,'&Product_Name',&Price)
new 1: insert into inventory values(003,'Scale',8)

1 row created.

SQL> /
Enter value for product_number: 004
Enter value for product_name: Box
Enter value for price: 25
old 1: insert into inventory
values(&Product_Number,'&Product_Name',&Price)
new 1: insert into inventory values(004,'Box',25)

1 row created.

SQL> select * from inventory;

PRONO PRONAME RATE


---------- ------------------------------ ----------
1 Pen 12
2 Pencil 5
3 Scale 8
4 Box 25

PL/SQL (Type “ed” which open notepad, clear all the content, type the following pl/sql and
save the file with “.sql” extension, )
declare
cursor c is select prono,proname,rate from inventory;
pno inventory.prono%type;
pname inventory.proname%type;
prate inventory.rate%type;
begin
open c;
dbms_output.put_line('******* Price List after the updation of Rate*********');
dbms_output.put_line('Product No Product Name Rate');
dbms_output.put_line('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
loop
update inventory set rate = rate + ((rate*20)/100);
fetch c into pno,pname,prate;
dbms_output.put_line(to_char(pno,'999')||' '||trim(pname)||' '||to_char(prate,'9999.99'));
exit when c%notfound;
end loop;
end;
/

sql> Set serveroutput on

SQL> @ filename with full path (Example: @E:\inven.sql)

******* Price List after the updation of Rate*****


Product No Product Name Rate
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 Pen 14.40
2 Pencil 6.00
3 Scale 9.60
4 Box 37.50

PL/SQL procedure successfully completed.

SQL> alter table inventory add(NOI number);

Table altered.

SQL> update inventory set noi = 100 where prono = 001;

1 row updated.

SQL> update inventory set noi = 10 where prono = 002;

1 row updated.

SQL> update inventory set noi = 50 where prono = 003;

1 row updated.

SQL> update inventory set noi = 25 where prono = 004;

1 row updated.

SQL> select * from inventory;

PRONO PRONAME RATE NOI


---------- ------------------------------ ---------- ----------
1 Pen 14.10 100
2 Pencil 6.00 10
3 Scale 9.60 50
4 Box 37.50 25
4. Write a PL/SQL to split the student table into two tables based on result (One table for “Pass”
and another for “Fail”). Use cursor for handling records of student table. Assume necessary
fields and create a student details table.

SQL> create table student(


2 rollno number not null,
3 sname varchar2(25),
4 result varchar2(4),
5 constraint rollno_pk primary key(rollno));

Table created.

SQL> insert into student values(&Roll_No,'&Student_Name','&Result');


Enter value for roll_no: 16101
Enter value for student_name: Arun
Enter value for result: Pass
old 1: insert into student
values(&Roll_No,'&Student_Name','&Result')
new 1: insert into student values(16101,'Arun','Pass')

1 row created.

SQL> /
Enter value for roll_no: 16102
Enter value for student_name: Anand
Enter value for result: Fail
old 1: insert into student
values(&Roll_No,'&Student_Name','&Result')
new 1: insert into student values(16102,'Anand','Fail')

1 row created.

SQL> /
Enter value for roll_no: 16103
Enter value for student_name: Ananthi
Enter value for result: Pass
old 1: insert into student
values(&Roll_No,'&Student_Name','&Result')
new 1: insert into student values(16103,'Ananthi','Pass')

1 row created.

SQL> /
Enter value for roll_no: 16104
Enter value for student_name: Banu
Enter value for result: Fail
old 1: insert into student
values(&Roll_No,'&Student_Name','&Result')
new 1: insert into student values(16104,'Banu','Fail')
1 row created.

SQL> select * from student;

ROLLNO SNAME RESU


---------- ------------------------- ----
16101 Arun Pass
16102 Anand Fail
16103 Ananthi Pass
16104 Banu Fail

SQL> create table stud_pass(rno number,sname varchar2(25),res


varchar2(4));

Table created.

SQL> create table stud_fail(rno number,sname varchar2(25),res


varchar2(4));

Table created.

PL/SQL Program (Save as “stud.sql”)


declare
cursor c is select * from student;
rn student.rollno%type;
sn student.sname%type;
re student.result%type;
begin
open c;
loop
fetch c into rn,sn,re;
if re = 'Pass' then
insert into stud_pass values(rn,sn,re);
else
insert into stud_fail values(rn,sn,re);
end if;
exit when c%notfound;
end loop;
dbms_output.put_line('******* Table successfully spilted into two *****');
end;
/

SQL> set serveroutput on


SQL> @e:\stud.sql

******* Table successfully spilted into two *****

PL/SQL procedure successfully completed.


SQL> select * from student;

ROLLNO SNAME RESU


---------- ------------------------- ----
16101 Arun Pass
16102 Anand Fail
16103 Ananthi Pass
16104 Banu Fail

SQL> select * from stud_pass;

RNO SNAME RES


---------- ------------------------- ----
16101 Arun Pass
16103 Ananthi Pass

SQL> select * from stud_fail;

RNO SNAME RES


---------- ------------------------- ----
16102 Anand Fail
16104 Banu Fail

5. Create a database trigger to implement on master and transaction tables which are based on
inventory management system for checking data validity. Assume the necessary fields for
both tables.

SQL> create table master(


2 pno number primary key,
3 pname varchar2(25),
4 price number(10,2),
5 stock number(4));

Table created.

SQL> create table trans(


2 p_no number,
3 no_pro_sold number);

Table created.

SQL> insert into master values (&Product_No, '&Prodct_Name',


&Price, &Stock_in_hand);

Enter value for product_no: 001


Enter value for prodct_name: CD
Enter value for price: 12
Enter value for stock_in_hand: 50
old 1: insert into master
values(&Product_No,'&Prodct_Name',&Price,&Stock_in_hand)
new 1: insert into master values(001,'CD',12,50)

1 row created.

SQL> /
Enter value for product_no: 002
Enter value for prodct_name: DVD
Enter value for price: 18
Enter value for stock_in_hand: 100
old 1: insert into master
values(&Product_No,'&Prodct_Name',&Price,&Stock_in_hand)
new 1: insert into master values(002,'DVD',18,100)

1 row created.

SQL> /
Enter value for product_no: 003
Enter value for prodct_name: Pendrive
Enter value for price: 350
Enter value for stock_in_hand: 10
old 1: insert into master
values(&Product_No,'&Prodct_Name',&Price,&Stock_in_hand)
new 1: insert into master values(003,'Pendrive',350,10)

1 row created.

trig.sql
create or replace trigger t1 after insert or update on trans for each row
declare
cursor c1 is select * from master where pno = :new.p_no;
begin
for i in c1 loop
if i.stock < :new.no_pro_sold then
raise_application_error(-20001,'No.of Product more than stock, error');
end if;
end loop;
end;
/

SQL> @e:\trig.sql

Trigger created.

SQL> insert into trans values(001,200);


insert into trans values(001,200)
*
ERROR at line 1:
ORA-20001: No.of Product more than stock, error
ORA-06512: at "USER.T1", line 6
ORA-04088: error during execution of trigger 'USER.T1'

SQL> insert into trans values(001,20);

1 row created.

SQL> select * from trans;

P_NO NO_PRO_SOLD
---------- -----------
1 20

6. Write a PL/SQL to raise the following Exception in Bank Account Management table when
deposit amount is zero.

SQL> create table bank(


2 accno number,
3 c_name varchar2(30),
4 acc_type varchar2(10),
5 dep_amt number(12,2)
6 constraint ano_pk primary key(accno));

Table created.

SQL> desc bank;

bank.sql
declare
zero_deposit exception;
cust bank%rowtype;
begin
cust.accno := &Account_no;
cust.c_name := '&Customer_Name ';
cust.acc_type := '&Account_Type ';
cust.dep_amt := &Deposit_amount;
if cust.dep_amt <= 0 then
raise zero_deposit;
else
insert into bank values(cust.accno,cust.c_name,cust.acc_type,cust.dep_amt);
end if;
exception
when zero_deposit then
dbms_output.put_line('*******Deposit Amount cannot be zero*****');
end;
/
SQL> set serveroutput on

SQL> @e:\bank.sql
Enter value for account_no: 2
old 5: cust.accno := &Account_no;
new 5: cust.accno := 2;
Enter value for customer_name: Arun
old 6: cust.c_name := '&Customer_Name ';
new 6: cust.c_name := 'Arun ';
Enter value for account_type: Saving
old 7: cust.acc_type := '&Account_Type ';
new 7: cust.acc_type := 'Saving ';
Enter value for deposit_amount: 12000
old 8: cust.dep_amt := &Deposit_amount;
new 8: cust.dep_amt := 12000;

PL/SQL procedure successfully completed.

SQL> /
Enter value for account_no: 3
old 5: cust.accno := &Account_no;
new 5: cust.accno := 3;
Enter value for customer_name: Balu
old 6: cust.c_name := '&Customer_Name ';
new 6: cust.c_name := 'Balu ';
Enter value for account_type: current
old 7: cust.acc_type := '&Account_Type ';
new 7: cust.acc_type := 'current ';
Enter value for deposit_amount: 8500
old 8: cust.dep_amt := &Deposit_amount;
new 8: cust.dep_amt := 8500;

PL/SQL procedure successfully completed.

SQL> select * from bank;

ACCNO C_NAME ACC_TYPE DEP_AMT


---------- ------------------------------ ---------- ----------
2 Arun Saving 12000
3 Balu current 8500

SQL> @e:\bank.sql
Enter value for account_no: 4
old 5: cust.accno := &Account_no;
new 5: cust.accno := 4;
Enter value for customer_name: Chandru
old 6: cust.c_name := '&Customer_Name ';
new 6: cust.c_name := 'Chandru ';
Enter value for account_type: saving
old 7: cust.acc_type := '&Account_Type ';
new 7: cust.acc_type := 'saving ';
Enter value for deposit_amount: 0
old 8: cust.dep_amt := &Deposit_amount;
new 8: cust.dep_amt := 0;

*******Deposit Amount cannot be zero*****

PL/SQL procedure successfully completed.

SQL> select * from bank;

ACCNO C_NAME ACC_TYPE DEP_AMT


---------- ------------------------------ ---------- ----------
2 Arun Saving 12000
3 Balu current 8500

You might also like