Dbms Pract Sem3
Dbms Pract Sem3
dname varchar2(14),
loc varchar2(13)
);
Table Created
Records:
ename varchar2(10),
job varchar2(9),
mgr number(4),
hiredate date,
sal number(7,2),
comm number(7,2),
);
Table Created
Records:
cust_no varchar2(6) check( cust_no like 'ANA%' or cust_no like 'VVN%' or cust_no like
'BAK%'),
addressvarchar2(20),
primary key(cust_no)
);
1 row created.
Records:
bill_non varchar2(3),
prev_read number(5),
curr_read number(5),
due_date date
);
1 row created.
Records:
declare
begin
dbms_output.put_line('hello');
end;
2. Write a PL/SQL block that will take two integer numbers. Print addition, subtraction,
multiplication, division of the inputted numbers.
declare
a number(10);
b number(10);
ans number(10);
begin
a:=&number;
b:=&number;
ans:=a+b;
ans:=a-b;
ans:=a*b;
ans:=a/b;
end;
3. Write a PL/SQL block to take an input in the form of number and check whether the number is
EVEN or ODD.
declare
x number(4);
begin
x:=&x;
if mod(x,2)=0 then
dbms_output.put_line(to_char(x)||' is even');
end if;
if mod(x,2)=1 then
dbms_output.put_line(to_char(x)||' is odd');
end if;
end;
4. Write a PL/SQL block to take an input in the form of number and check whether the number is
positive, negative or zero.
declare
x number(4);
begin
X:=&X;
if x>0 then
dbms_output.put_line(to_char(x)||' is positive');
end if;
if x<0 then
dbms_output.put_line(to_char(x)||' is nagetive');
end if;
if x=0 then
dbms_output.put_line(to_char(x)||' is zero');
end if;
end;
5. Display the values value from 1 to 10 using LOOP..END LOOP, WHILE..LOOP AND FOR
LOOP.
# LOOP..END LOOP
declare
i number(3);
n number(3);
begin
i:=1;
n:=&n;
loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
# WHILE..LOOP
declare
i number(3);
n number(3);
begin
i:=&i;
n:=&n;
while i<=n
loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
/
# FOR LOOP
declare
begin
for i in 1..100
loop
if mod(i,2)=0 then
dbms_output.put_line(i);
end if;
end loop;
end;
Assignment 03 : PL/SQL Block – Implicit cursor (EMP and DEPT table) (Use
of COMMIT, ROLLBACK and SAVEPOINT)
1. Write a PL/SQL block that will take an input in the form of department number.
Increase the salaries of the employees working in the department by 5.5%. If the increment
updates the total salary of that department by 255 take back the increment else give the
increment permanently.
declare
c_dno number(2);
tot_sal number(8);
ett_sal number(8);
begin
c_dno:=&deptno;
ett_sal:=tot_sal * .055;
if ett_sal > 255 then
ROLLBACK;
dbms_output.put_line('1st ROLLBACK');
else
commite;
end if;
end;
2. Write a PL/SQL block that will update the salaries of the employees working with
SCOTT. If more than FOUR employees salaries are affected by the increment take back
the increment, else give the increment permanently.
declare
c_dno number(2);
total_rows number(3);
begin
ROLLBACK;
else
commite;
end if;
end;
3. Write a PL/SQL block that will take an input in the form of department number.
Decrease the salaries of the employees working in that department by 0.75%. If at least a
record is updated keep the change permanent else print appropriate message and
terminate the block.
declare
c_dno number(2);
tot_sal number(8);
ett_sal number(8);
begin
c_dno:=&deptno;
if
commit;
else
ROLLBACK;
end if;
end;
/
Assignment 04 : CURSOR
1. Display empno, name, salary, job and deptno of employees using cursor & %rowtype.
declare
Begin
open emp_rec;
loop
` dbms_output.put_line('');
end loop;
close emp_rec;
end;
------------------------------------------------------
Department No. : XX Department Name: XXXX
----------------------------------------------------------------------------------------------
Number Name
.. .. .. .. .. ..
---------------------------------------------------------------------------------------------- ----------------------
declare
c_dname dept.dname%type;
Begin
open dept_rec;
loop
dbms_output.put_line('deptno'||c_deptholl ||'deptname'||c_dname);
open emp_rec;
loop
dbms_output.put_line(''||c_empno||''||c_ename||''||c_job||''||c_sal);
dbms_output.put_line('');
end loop;
close emp_rec;
end loop;
close dept_rec;
end;
Electricity consumption
------------------------------------------------------------------------------------------------------------
... .. .. ..
------------------------------------------------------------------------------------------------------------
declare
cust_no ele_cust.cust_no%type;
cust_name ele_cust.cust_name%type;
cust_consum ele_cust.cust_consum%type;
cust_city ele_cust.cust_city%type;
lot_consum number(8):= 0;
cursor ele_rec is select cust_no,cust_name,cust_consum, cust_city,from ele_cust;
Begin
dbms_output.put_line( '--------------------------------');
open ele_rec;
loop
dbms_output.put_line(cust_no||''||cust_name||''||cust_consum||''||cust_city);
` dbms_output.put_line('');
dbms_output.put_line('');
dbms_output.put_line('------------------------------');
end loop;
close ele_rec;
end;
4. Write a cursor that will take input as a city and month. And display the inputted month
wise report for particular city in the following format:
Electricity Bill
Current Date:_______
Month: __________
--------------------------------------------------------------------------------------------------------------------
Readings
Customer No. Name Previous Current Consumption Unit Price Total Bill
--------------------------------------------------------------------------------------------------------------------
* ** ** ** ** ** **
--------------------------------------------------------------------------------------------------------------------
declare
c_cust_bill ele_cust.cust_bill%type;
c_prev ele_cust.prev_%type;
c_curr ele_cust.curr_%type;
c_cust_month ele_cust.month%type;
cust_no ele_cust.cust_no%type;
cust_name ele_cust.cust_name%type;
cust_city ele_cust.cust_city%type;
cust_consum ele_cust.cust_consum%type;
tot_val number(8):= 0;
Begin
open ele_rec;
loop
dbms_output.put_line(c_cust_bill ||''||c.cust_name||''||
c_prev||''||c_curr||''||c_cust_consum||''||c_unit_price||''||tot_val);
dbms_output.put_line(cust_no||''||cust_name||''||cust_consum||''||cust_city);
` dbms_output.put_line('');
dbms_output.put_line('------------------------------');
end loop;
close ele_rec;
end;
declare
no number(2);
name varchar2(15);
location varchar2(13);
begin
no:=&no;
select deptno, dname, loc into no, name, location from dept where deptno=no;
exception
end;
2. Write a PL/SQL block to take an input in the form of job. Print employee information if
the job is with one employee in the EMP table, if the job is with more than one employee
print appropriate message and terminate the block, and if the job is not present print
appropriate message and terminate the block.
declare
emp_no number(4);
ename varchar2(10);
job1 varchar2(25);
mgr1 varchar2(4);
hiredate date;
sal number(7,2);
comm number(7,2);
dept_no number(2);
begin
job1:='&job1';
select emp_no,ename,job,mgr,hiredate, sal, comm, dept_no into emp_no, ename, job1,
mgr1, hiredate, sal, comm, dept_no from emp where job=job1;
dbms_output.put_line('emp_no'||'ename'||'job'||'mgr'||'hiredate'||'salary'||;'commission'||'dept
_no');
exception
end;
3. Write a PL/SQL block to take an input in the form of department number. If the total
salary paid to that department is greater than 1900 then raise the user defined exception,
print appropriate message and terminate the block. If the total salary is less than or equal
to 1900 update the salaries of the employees working in the department by 2.5%.
declare
total_sal number(10):=0;
deptno emp.dept_no%type;
ename emp.ename%type;
new_sal emp.sal%type;
my solution exception;
cursor N is select deptno, ename, sal from emp where deptno = &deptno;
begin
savepoint s;
open N;
loop
select deptno,ename,sal into deptno, name, new_sal from emp where
deptno=deptno;
total_sal:=total_sal+new_sal;
end loop;
raise my solution;
else
where deptno=deptno;
dbms_output.put_line('salary || sdsd;'||new_sal||name);
end if;
close N
exception
rollback to S;
end;
Assignment 06 : FUNCTION
1.Create a function to accept deptno and return dname.
is
name varchar2(14);
begin
return name;
End;
2.Create a function to check salaries of two employees, and return the max out of two.
return number
is
s1 number(7,2);
s2 number(7,2);
begin
if s1>s2 then
return s1;
return s2;
else
end if;
end;
3. Create a function that will take employee number, check comm for particular empno
and return his sal, if comm > 450 else return 0 and display message accordingly.
return number
is
s number(7,2);
com number(7,2);
begin
if com>450 then
else
return 0;
end if;
end;
Assignment 07 : PROCEDURE
1. Write a procedure to display the employee name if the salary > 3500 --for a particular
empno. (supply an empno & return employee name)
s number(7,2);
name varchar2(15);
begin
if s>3500 then
dbms_output.put_line('ename='||name);
else
end if;
end;
2.Write a procedure that will take department number, if that department is having more
than four employees print that department information from the procedure, else print
appropriate message from the BLOCK which calls the procedure.
is
cn number(4);
dnm varchar2(13);
l varchar2(14);
begin
if cn>4 then
dbms_output.put_line('Department Number:'||dno);
dbms_output.put_line('Department Name:'||dnm);
dbms_output.put_line('Department Location:'||l);
else
end if;
end;
Assignment 08 : TRIGGERS
ename varchar2(10),
job varchar2(9),
mgr number(4),
hiredate date,
sal number(7,2),
comm number(7,2),
);
on emp
declare
begin
end;
2.Insert record(s) in EMP1 table as and when user updates record(s) of EMP table.
after update
on emp
declare
begin
update emp1set
empno=:new.empno,ename=:new.ename,job=:new.job,mgr=:new.mgr,hiredate=:new.hiredate,sa
l=:new.sal,comm=:new.comm,deptno=:new.deptno;
end;
3.Insert record(s) in EMP1 table when user deletes record(s) from EMP table.
after delete
on emp
begin
end;