Rdbms Practical
Rdbms Practical
declare
begin
dbms_output.put_line('---------------------------------');
dbms_output.put_line('Cust_Id Cust_Name Item_Id Qty ');
dbms_output.put_line('---------------------------------');
open cust_data;
fetch cust_data into cust_row;
loop
exit when cust_data%notfound;
fetch cust_data into cust_row;
dbms_output.put_line(cust_row.custid || ' - - ' || cust_row.custname ||
' - - ' || cust_row.itemid || ' - - ' || cust_row.qty);
end loop;
close cust_data;
dbms_output.put_line('---------------------------------');
end;
OUTPUT
--------------------------------------
Cust_Id Cust_Name Item_Id Qty
---------------------------------------
c06 - - alpa - - i03 - - 8
c07 - - Hettal - - i03 - - 12
c08 - - krupa - - i04 - - 6
c03 - -jagruti - - i09 - - 6
c03 - - vipul- - i09 - - 6
---------------------------------------
2. Write PL/SQL block that select the row in which the qty is 3
and handle no data found exception.
declare
cursor c_qty(vqty number) is select * from customers where
qty=vqty;
vcust customers%rowtype;
begin
open c_qty(&vqty);
fetch c_qty into vcust;
if c_qty%notfound then
raise no_data_found;
else
dbms_output.put_line('There Are Records with Entered Qty.');
end if;
exception
when no_data_found then
dbms_output.put_line('No Records Found With Specified
Quantity.');
when others then
dbms_output.put_line('Unknown Error.');
end;
OUTPUT
No Records Found With Specified Quantity
declare
custid varchar2(5);
begin
custid:='&custid';
Accept_customer(custid);
end;
OUTPUT
Enter value for custid: c21
old 4: custid:='&custid';
new 4: custid:='c21';
-----------------
Name Of Item
-----------------
soap
ERROR at line 1:
ORA-20021: Can Not Delete Records From Customers Table.
ORA-06512: at "EXAM21.CUST_DELETE", line 2
ORA-04088: error during execution of trigger
'EXAM21.CUST_DELETE'
5. Write a function to display the name of customer who
purchased dish.
declare
n boolean;
iname varchar2(10);
begin
iname:='&iname';
n:=display_customer(iname);
end;
ERROR at line 1:
ORA-20021: Can Not Delete Records From Customers Table.
ORA-06512: at "EXAM21.CUST_DELETE", line 2
ORA-04088: error during execution of trigger
'EXAM21.CUST_DELETE'
PL/SQL BLOCK
student (enroll no, name, bdate, English, Maths, Science, Social Sc.,
Hindi, total, status)
E_NO STUDENT2.ENROLL_NO%TYPE;
ENG STUDENT2.MARKS_ENG%TYPE;
MATH STUDENT2.MARKS_MATHS%TYPE;
SS STUDENT2.MARKS_SS%TYPE;
HINDI STUDENT2.MARKS_HINDI%TYPE;
TOT NUMBER(4);
PER NUMBER(5,2);
STAT VARCHAR2(15);
SUB_FAIL NUMBER(2) := 0;
CURSOR C1 IS SELECT
ENROLL_NO,MARKS_ENG,MARKS_MATHS,MARKS_SS,MA
RKS_HINDI
FROM STUDENT2;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO E_NO,ENG,MATH,SS,HINDI;
EXIT WHEN C1%NOTFOUND;
declare
cursor disp is select cust_id ,cust_name ,pq,sq from cust_item
where cust_id ='d21';
v_cust_id cust_item.cust_id%type ;
v_cust_name cust_item.cust_name%type ;
v_pq cust_item.pq%type ;
v_sq cust_item.sq%type ;
begin
open disp;
dbms_output.put_line ('v_cust_id' ||' '||'v_cust_name' ||' '||'v_pq' ||'
'||'v_sq');
loop
declare
v_cust_id cust_item.cust_id%type ;
v_cust_name cust_item.cust_name%type ;
v_pq cust_item.pq%type ;
v_sq cust_item.sq%type ;
begin
select cust_id ,pq into v_cust_id ,v_pq from cust_item where
cust_id = 'd03';
dbms_output.put_line ('purchase quantity');
dbms_output.put_line ('-----------------');
dbms_output.put_line (v_pq);
end;
output
purchase quantity
-----------------
50
begin
output
custid = d21
CUST_ID CUST_NAME PQ SQ
--------- ---------- ---------------------
d21 Hettal 10
d21 Hettal 20
d21 Hettal 3
5. Write a PL/SQL block to display the details of Cust-item.
declare
cursor disp is select cust_id ,cust_name ,pq,sq from cust_item;
v_cust_id cust_item.cust_id%type ;
v_cust_name cust_item.cust_name%type ;
v_pq cust_item.pq%type ;
v_sq cust_item.sq%type ;
begin
open disp;
dbms_output.put_line ('Details of customer_item table');
dbms_output.put_line ('---------------------------------------');
dbms_output.put_line ('v_cust_id' ||' '||'v_cust_name' ||' '||'v_pq' ||'
'||'v_sq');
dbms_output.put_line ('---------------------------------------');
loop
fetch disp into v_cust_id , v_cust_name ,v_pq ,v_sq;
dbms_output.put_line (v_cust_id ||' '||v_cust_name ||' '||v_pq ||'
'||v_sq);
close disp ;
end;
Output
declare
cursor disp is select cust_id ,cust_name ,pq,sq from cust_item ;
v_cust_id cust_item.cust_id%type ;
v_cust_name cust_item.cust_name%type ;
v_pq cust_item.pq%type ;
v_sq cust_item.sq%type ;
begin
open disp;
dbms_output.put_line ('customer detal with null purchase id ');
dbms_output.put_line ('-------------------------------------');
dbms_output.put_line (‘cust_id');
dbms_output.put_line ('-------------------------------------');
loop
fetch disp into v_cust_id , v_cust_name ,v_pq ,v_sq;
if v_pq is null then
dbms_output.put_line (v_cust_id );
exit when disp%notfound ;
end if ;
end loop ;
close disp;
end;
output
customer detal with null purchase id
-------------------------------------
cust_id
-------------------------------------
d02
d21
d02
d02
d02
CURSOR
1. 1. Write a cursor to update first name = Dipali where it is
„Drashti.
DECLARE
cursor c_student is
select f_name from student;
name student.f_name%type := 'Drashti;
v_first_name student.f_name%type;
flag number(1) := 0;
BEGIN
open c_student;
loop
fetch c_student into v_first_name;
if v_first_name = name then
update student set f_name = Dipali where f_name =
'Drashti;
flag := 1;
end if;
exit when c_student%notfound;
end loop;
if flag = 0 then
dbms_output.put_line('Record not found');
end if;
commit;
close c_student;
END;
/
DECLARE
TYPE t_EmployeeRecord IS RECORD
(
emp_no employee.eno%type;
emp_name employee.ename%type;
emp_add employee.eadd%type;
);
TYPE v_EmployeeRecord employee%ROWTYPE;
cursor c_Employee in select eno,ename,eadd from employee;
v_emp_no employee.eno%type;
v_emp_name employee.enname%type;
v_emp_add employee.eadd%type;
/* DECLARE A VARIABLE OF TYPE RECORD */
v_EmployeeInfo t_EmployeeRecord;
BEGIN
open c_Employee;
loop
fetch c_Employee into v_emp_no,v_emp_name,v_emp_add;
v.EmployeeRecord.emp_no := v_emp_no;
v.EmployeeRecord.emp_name := v_emp_name;
v.EmployeeRecord.emp_add := v_emp_add;
dbms_output.put_line('Emp Number' || ' ' || 'Emp Name' || '
' || 'Emp Add');
dbms_output.put_line(v_emp_no || ' ' || v_emp_name || ' '
|| v_emp_add);
exit when c_Employee%notfound;
end loop;
close c_Employee;
END;
/
DECLARE
CURSOR c_student is
select * from student;
v_cursorrec c_student%ROWTYPE;
BEGIN
OPEN c_student;
LOOP
DECLARE
vFName student.fname%type;
vLName student.lname%type;
CURSOR c_student is
SELECT FName,LName
from student;
BEGIN
OPEN c_student;
DBMS_OUTPUT.PUT_LINE('First Name'|| ' ' || 'Last Name');
LOOP
FETCH c_student INTO vFName,vLName;
EXIT WHEN c_student % NOTFOUND;
DBMS_OUTPUT.PUT_LINE(vFName || ' ' || vLName);
END LOOP;
CLOSE c_student;
END;
/
5. Example of update through cursor.
DECLARE
CURSOR c_student is
select * from student;
v_cursorrec c_student%ROWTYPE;
BEGIN
OPEN c_student;
LOOP
FETCH c_student into v_cursorrec;
DECLARE
TYPE t_student IS TABLE OF student.stud_id%type INDEX
BY BINARY_INTEGER;
CURSOR c_student IS SELECT stud_id FROM student;
v_student c_student%ROWTYPE;
v_stud t_student;
v_counter number(3) := 1;
BEGIN
open c_student;
loop
fetch c_student into v_stud(v_counter);
exit when c_student%notfound;
v_counter := v_counter + 1;
end loop;
close c_student;
v_counter := v_stud.first;
dbms_output.put_line('Roll Number');
dbms_output.put_line('===========');
loop
dbms_output.put_line(v_stud(v_counter));
exit when v_counter = v_stud.last;
v_counter := v_stud.next(v_counter);
end loop;
end;
/
DECLARE
TYPE t_student IS TABLE OF student.stud_id%type INDEX BY
BINARY_INTEGER;
CURSOR c_student IS SELECT stud_id FROM student;
v_student c_student%ROWTYPE;
v_stud t_student;
v_counter number(3) := 1;
BEGIN
open c_student;
loop
fetch c_student into v_stud(v_counter);
exit when c_student%notfound;
v_counter := v_counter + 1;
end loop;
close c_student;
v_counter := v_stud.first;
dbms_output.put_line('Roll Number');
dbms_output.put_line('===========');
loop
exit when v_counter = v_stud.last;
v_counter := v_stud.next(v_counter);
end loop;
dbms_output.put_line('The Number of Records in Table are : ' ||
v_counter);
end;
/
PL/SQL PROCEDURE
Do the following:
At the end of each month, process records of table Work to calculate
the salary amount and insert a record in Salary table for each
employee in table Work.
SOLUATION
SQL> create table emp(emp_no number(3) primary key,name
varchar2(20),
2 rate_hr number(5,2));
Table created.
Table created.
Table created.
DT
---------
21-SEP-21
08-SEP-21
15-SEP-21
20-SEP-21
22-SEP-21
29-SEP-21
02-OCT-21
06-OCT-21
13-OCT-21
15-OCT-21
20-OCT-21
27-OCT-21
EMP_NO DT HR_WORKED
--------- --------- ---------
121 21-SEP-21 7
121 08-SEP-21 9
103 15-SEP-21 2
103 30-SEP-21 4
121 02-OCT-21 1.5
121 14-OCT-21 11
103 25-OCT-21 8
103 31-OCT-21 6
102 15-SEP-21 1
102 30-SEP-21 12
102 15-OCT-21 6
102 31-OCT-21 10
104 21-SEP-21 5
104 17-SEP-21 10
104 30-SEP-21 5
104 31-OCT-21 8
16 rows selected.
SQL> ed
Wrote file afiedt.buf
tab_rate t_rate_tab;
tab_sal t_sal_tab;
sal NUMBER(6,2) := 0;
v_yr NUMBER(4);
cur_dd INTEGER;
v_hr work.hr_worked%TYPE;
v_no work.emp_no%TYPE;
BEGIN
FOR loop_var IN csr_hol LOOP
v_yr := TO_CHAR(loop_var.dt,'yy');
tab_hol(TO_CHAR(loop_var.dt,'dd')) :=
TO_CHAR(loop_var.dt,'dd');
END LOOP;
FOR loop_var IN csr_rate LOOP
tab_rate(loop_var.emp_no) := loop_var.rate_hr;
tab_sal(loop_var.emp_no) := 0;
END LOOP;
FOR loop_var IN csr_rec LOOP
cur_dd := TO_CHAR(loop_var.dt,'dd');
v_no := loop_var.emp_no;
v_hr := loop_var.hr_worked;
IF (tab_hol.exists(cur_dd)) THEN
IF (v_hr <= 2) THEN
sal := 0;
ELSIF (v_hr <= 4) THEN
sal := (v_hr * tab_rate(v_no))*0.60;
ELSIF (v_hr <= 6) THEN
sal := (v_hr * tab_rate(v_no))*0.80;
ELSE
sal := (v_hr * tab_rate(v_no));
sal := sal*1.30;
END IF;
ELSE
IF(v_hr <= 2) THEN
sal := 0;
ELSIF(v_hr <= 4) THEN
sal := (tab_rate(v_no)*v_hr)*0.60;
ELSIF(v_hr <= 6) THEN
sal := (v_hr * tab_rate(v_no))*0.80;
ELSIF (v_hr <= 8) THEN
sal := (v_hr * tab_rate(v_no));
ELSE
sal := (v_hr * tab_rate(v_no))*1.20;
END IF;
END IF;
tab_sal(v_no) := tab_sal(v_no) + sal;
END LOOP;
FOR loop_var IN csr_rate LOOP
INSERT INTO salary
VALUES(loop_var.emp_no,month,v_yr,
tab_sal(loop_var.emp_no));
END LOOP;
* END pr_cal_sal;
SQL> /
Procedure created.
Call completed.
BEGIN
select bal into amt_val from acct where pAcc_id = ac_id;
withdrawl_amt := amt_val - pAmt;
if (withdrawl_amt < 0 ) then
raise Invalid_Withdrawl;
else
update acct set bal = withdrawl_amt where ac_id = pAcc_id;
end if;
EXCEPTION
when Invalid_Withdrawl then
dbms_output.put_line ('Invalid Withdrawl');
END pAcctWithdrawl;
/
Table :
AC_ID BAL
---------- ----------
1 700
2 2000
3 5000
Output :
Ouput :
Output :
FUNCTIONS
1. Write a function block which checks if the strength of the
department is greater then 80 percent then give a message
that “Dept is full” else “Dept is vacant”.
BEGIN
select currstud,maxstud into v_currstud,v_maxstud from dept
where f_deptid = deptid and f_course = course;
if ((v_currstud/v_maxstud)*100 >= 80) then
RETURN true;
else
RETURN false;
end if;
END f_dept;
/
Table:
DECLARE
v_deptid dept.deptid%type;
v_course dept.course%type;
cursor c_dept is select deptid,course from dept;
BEGIN
open c_dept;
loop
FETCH c_dept into v_deptid,v_course;
exit when c_dept%notfound;
if f_dept(v_deptid,v_course) then
dbms_output.put_line('Dept ' || v_deptid || ' is
almost full');
else
dbms_output.put_line('Dept ' || v_deptid || ' is
vacant');
end if;
end loop;
END;
/
Output:
Function Call:
DECLARE
v_charnum varchar2(10);
BEGIN
v_charnum := '&v_charnum';
if f_numtochar(v_charnum) <> NULL then
dbms_output.put_line (f_numtochar(v_charnum));
else
dbms_output.put_line ('NULL');
end if;
END;
/
Output:
------------------------------------------------
Output : Zero Nine Eight Seven
loop
v_fdec := v_fdec || substr(v_dec,v_int,1);
end loop;
return v_fdec;
END DecToHex;
/
Output:
SQL> select dectohex(104) from dual;
DECTOHEX(104)
68
4. Write a Function to find whether the enterd number is a
prime number or not.
create or replace function IsPrime
(
f_num number
) return varchar2 as
v_ans varchar2(20);
v_int number(3);
v_temp number (3) := 0;
BEGIN
for v_int in 1..f_num
loop
if (f_num mod v_int = 0) then
v_temp := v_temp + 1;
end if;
end loop;
if v_temp = 2 then
return ('Number ' || f_num || ' is a prime number');
else
return ('Number ' || f_num || ' is not a prime number');
end if;
v_temp := 0;
END IsPrime;
/
Output:
EXCEPTION
1. Write a Exception to check the current credits of the
students.
DECLARE
e_ExcessCredits EXCEPTION;
v_CurCredits student.current_credits%type;
BEGIN
select current_credits into v_CurCredits from student
where stud_id = 'mca21';
if v_CurCredits > 20 then
e_ExcessCredits;
end if;
EXCEPTION
when e_ExcessCredits then
dbms_output.put_line ('Error Raised');
when others then
dbms_output.put_line('Other Error Raised');
end;
/
2. Write a Exception to check if current students a greater
than permitted maximum students.
DECLARE
e_ExcessStudents EXCEPTION;
v_CurrentStudent student.current_students%type;
v_MaxStudent student.max_students%type;
BEGIN
select current_students,max_students into
v_CurrentStudent,v_MaxStudent from student where
stud_id = 'mca21';
if (v_CurrentStudent > v_MaxStudent) then
raise e_ExcessStudents;
end if;
EXCEPTION
when e_ExcessStudents then
TRIGGER
Trigger created.
(iii)Write a trigger to see that the item_code starts with I
Trigger created.
10 employees
getting the highest salary.
5) WAP using parameterized cursor to display all the information of
employee living in specified city. Ask the city from user.
6) WAP which display the sum of salary department wise.
7) Create a cursor to generate different two tables from one master
table.
Student(Rno, Name, Std, B_date, Sex);
Girl_Table(Rno, Name, Std, B_date);
Boy_Table(Rno, Name, Std, B_date);
First fetch the row from Student table. If sex is ‘M’ then insert that
row in Boy_Table
and if ‘F’ then insert that row in Girl_Table.
In both table Rollno entry must be in Sequence(Using create sequence
command).
C FUNCTIONS :
1)WAF which accepts the name from user and returns the length of
that name.
2) WAF which accepts one number and return TRUE if no is prime
and return FALSE if no is not prime.
3) Write a function which accept the department no and returns
maximum salary of that
department. Handle the error if deptno does not exist or select
statement return more than one row.
EMP(Empno, deptno, salary).
4) Write a function to display whether the inputed employee no is
exists or not.
5) WAF which accepts one no and returns that no+100. Use INOUT
mode.
6) WAF which accepts the empno. If salary<10000 than give raise by
30%. If salary<20000 and salary>=10000 than give raise by 20%. If
salary>20000 than give raise by 10%. Handle the error if any.
7) WAF which accepts the empno and returns the experience in years.
Handle the error if empno does not exist.EMP(Empno, Empname,
DOJ);
D PROCEDURES:
1) Write a procedure which accepts the empno and returns the
associated empname. If empno does not exist than give proper error
message.
EMP(Empno, Empname).
PREPARED BY: DR.HETTAL H BARAD PAGE 36
SRCMBMM SANCHALIT
SMT K.S.KAPASHI BCA COLLEGE PALITANA
ORACLE USING RDBMS-II PRACTICAL
2) WAP which accepts the student rollno and returns the name,city
and marks of all the subjects of that student.
STUDENT(Stud_ID,Stud_name,m1,m2,m3).
3) WAP which accepts the name from the user. Return UPPER if
name is in uppercase,
LOWER if name is in lowercase, MIXCASE if name is entered using
both the case.
4) WAP which accepts the student rollno and returns the highest
percent and name of that
student to the calling block.
STUDENT(Stud_ID,Stud_name,percent);
5) WAP which accepts the date of joining for specific employee and
returns the years of experience along with its name. Accept the
Employee no from user.
EMP(empno, empname, DOJ);
6) WAP which accepts the student rollno and returns the result (in the
form of class: first class, second class, third class or fail).
STUDENT(Stud_ID,Stud_name,m1,m2,m3).
E TRIGGERS:
1) Write a Trigger that stores the old data table of student table in
student_backup while updating the student table.
Student_backup (Stud_ID, Stud_name, Address, Contact_no, Branch,
Operation_date)
Student (Stud_ID, Stud_name, Address, Contact_no, Branch)
2) Write a trigger, that ensures the empno of emp table is in a format
‘E00021’ (empno must start with ‘E’ and must be 6 characters long).
If not than complete empno with this format before inserting into the
employee table.
3) Write a trigger which checks the age of employee while inserting
the record in emp table. If it is negative than generate the error and
display proper message.
4) Write a trigger which converts the employee name in upper case if
it is inserted in any other case. Change should be done before the
insertion only.
5) WAT that stores the data of emp table in emp_backup table for
every delete operation and store the old data for every update
operation.
EMP(Empno, Empname, salary);
Emp_Backup(Empno,Empname,Date_of_operation,Type_of_operatio
n(i.e.update or delete));