Oracle New List
Oracle New List
set serveroutput on – command to display of pl/sql program output (use this command before the
execution of the program)
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.
* 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> create table emp_table(
2 empno number,
3 DOB date);
*****
Comparison and Logical operators
SQL> select * from employee where salary >= 20000;
SQL> select * from employee where age <= 30 and age > 20;
Group Commands
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;
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.
* Use the above command to insert as many rows as needed (Accno should be available in
lib_master table)
*****
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.
Table created.
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.
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;
/
Table altered.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
Table created.
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.
Table created.
Table created.
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.
Table created.
Table created.
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.
1 row created.
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.
Table created.
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;
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;
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;