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

Oracle Practical Program - 240625 - 100611-1

Oracle basics notes

Uploaded by

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

Oracle Practical Program - 240625 - 100611-1

Oracle basics notes

Uploaded by

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

1.

Employee Details – Table Creation


Aim
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.
Algorithm
Start the process.
Create a Table for storing Employee details
Insert the values to the table with various information’s of employee
Raise the queries with comparison, logical, set, sorting and grouping operations.
Terminate the process.

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);

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_Join',&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. Inventory Details – Alter and Update Operations


Aim
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.
Algorithm
1. Start the process.
2. Create a Table for Inventory details.
3. Insert the values to the table with product information’s.
4. Alter the table by adding a field called No.of Items
5. Update the rate of each product by 20% and display the new rate
6. Terminate the process.

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
fetch c into pno,pname,prate;
prate := prate + ((prate*20)/100);
update inventory set rate = prate where prono = pno;
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

3. Inventory Management System – Trigger Handling


Aim
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.
Algorithm
1. Start the process.
2. Create Tables for master and transaction Inventory details.
3. Insert the values to the tables with product information’s and transaction.
4. Create a PL/SQL procedure to handle the trigger operations
5. Execute the procedure while insertion of row.
6. Terminate the process.

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
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
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
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
4. Bank Account Management – Exception Handling
Aim
Write a PL/SQL to raise the following Exception in Bank Account Management table
when deposit amount is zero.
Algorithm
1. Start the process.
2. Create a PL/SQL procedure to handle the Exception
3. Check the deposit amount is greater than zero
4. If yes then allow transaction
5. Else decline the transaction with Exception
6. Terminate the process.

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