Dbms Print 4 To 11-1
Dbms Print 4 To 11-1
WEEK 4A:
4. a) AIM:- Implement group functions with different operators such as aggregate operators, group by, having
and order by.
IMPLEMENTATION:
SQL> create table t8 (id int,name varchar(20),age int, salary int);
Table created.
SQL> desc t8
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NUMBER(38)
NAME VARCHAR2(20)
AGE NUMBER(38)
SALARY NUMBER(38)
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
8 rows selected.
COUNT(ID)
----------
8
COUNT(SALARY)
-------------
1
COUNT(SALARY)
-------------
6
MIN(SALARY)
-----------
4000
AVG(SALARY)
-----------
8000
ID COUNT(ID)
---------- ----------
6 1
4 1
5 1
7 1
ID NAME
---------- --------------------
1 sree
2 raj
3 navdee
4 ram
5 varu
6 pavan
7 shiny
8 rishi
8 rows selected.
ID NAME AGE
---------- -------------------- ----------
1 sree 25
2 raj 27
3 navdeep 24
4 ram 26
5 varu 26
6 pavan 26
7 shiny 26
8 rishi 28
8 rows selected.
no rows selected
ID COUNT(ID)
---------- ----------
6 1
4 1
5 1
7 1
ID
----------
4
RESULT:-
NESTED & CORRELATED NESTED QUERIES
(Using set & set comparison operators.)
WEEK 4b:
4.b) AIM:-Implement nested and correlated nested queries using set operators and set comparison operators.
IMPLEMENTATION:
SQL> create table eee(id number(2),department varchar(5));
Table created.
SQL> insert into eee values(1,'cse');
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL> select * from eee;
ID DEPAR
---------- -----
1 cse
2 ece
3 eee
4 civil
5 mech
SQL> select name from employeee where exists (select * from eee where employeee.id=eee.id and
salary>4500);
NAME
----------
aliha
kathrin
SQL> select * from employeee where exists (select * from eee where employeee.id=eee.id and salary>4500);
SQL> select * from employeee where exists (select * from eee where employeee.id=eee.id );
7 rows selected.
CREATION OF VIEWS, SYNONYMS, SEQUENCE, INDEXES,SAVEPOINT
WEEK 5A:
5. a) AIM: To execute and verify queries for creating views, synonyms, sequence, indexes and save point.
IMPLEMENTATION:
SQL> create table prod07( pod_id number(1),prd_name varchar2(15), price number(4));
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
View created.
PRD_NAME PRICE
--------------- ----------
carpet 4500
ink pen 200
ribbons 150
SEQUENCE:
SQL> CREATE SEQUENCE sequence_1
2 start with 1
3 increment by 1
4 minvalue 0
5 maxvalue 100
6 cycle;
Sequence created.
INDEX:
Index created.
SAVEPOINT:
Savepoint created.
SQL> DELETE FROM prod07 WHERE PRICE=20;
1 row deleted
.
SQL> ROLLBACK TO sp1;
Rollback complete.
VARIOUS TYPES OF JOINS
WEEK 5b:
5. b) AIM: Implement various types of joins - outer join and inner join.
SQL> select eno,ename,dname from employeef5 e join deptf5 d on (e.deptno=d.deptno);
13 rows selected.
12 rows selected.
12 rows selected.
Basic PL/SQL:
WEEK 6:
6. a)AIM:
6. Construct PL/SQL block for the following.
a) To determine whether a number is palindrome
b) To determine whether a number is an Armstrong number
c) To find greatest of three numbers
d) To display Fibonacci series
IMPLEMENTATION:
SQL> set serveroutput on;
SQL> declare
2 a integer:=10;
3 b integer:=20;
4 c integer;
5 begin
6 c:=a+b;
7 dbms_output.put_line('value of c:'||c);
8 end;
9 /
value of c:30
PL/SQL procedure successfully completed.
1)To determine whether a number is an Armstrong number
SQL> declare
2 n number:=153;
3 s number:=0;
4 r number;
5 m number;
6 begin
7 m:=n;
8 while(n>0) loop
9 r:=mod(n,10);
10 s:=s+r*r*r;
11 n:=trunc(n/10);
12 end loop;
13 if (m=s) then
14 dbms_output.put_line('Yes');
15 else
16 dbms_output.put_line('No');
17 end if;
18 end;
19 /
Yes
PL/SQL procedure successfully completed.
2)To determine whether a number is palindrome
SQL> declare
2 n number;
3 m number;
4 rev number:=0;
5 rem number;
6 begin
7 n:=1221;
8 m:=n;
9 while(n>0) loop
10 rem:=mod(n,10);
11 rev:=(rev*10)+rem;
12 n:=trunc(n/10);
13 end loop;
14 if(m=rev) then
15 dbms_output.put_line('true');
16 else
17 dbms_output.put_line('false');
18 end if;
19 end;
20 /
true
PL/SQL procedure successfully completed.
3)To find greatest of three numbers
SQL> declare
2 a number:=46;
3 b number:=67;
4 c number:=109;
5 begin
6 if(a>b and a>c) then
7 dbms_output.put_line('Greatest number is'||a);
8 elsif(b>a and b>c) then
9 dbms_output.put_line('Greatest number is'||b);
10 else
11 dbms_output.put_line('Greatest number is'||c);
12 end if;
13 end;
14 /
Greatest number is109
PL/SQL procedure successfully completed.
4)To display Fibonacci series
SQL> declare
2 first number:=0;
3 second number:=1;
4 temp number;
5 n number:=8;
6 i number;
7 begin
8 dbms_output.put_line('Series:');
9 dbms_output.put_line(first);
10 dbms_output.put_line(second);
11 for i in 2..n loop
12 temp:=first+second;
13 first:=second;
14 second:=temp;
15 dbms_output.put_line(temp);
16 end loop;
17 end;
18 /
Series:
0
1
1
2
3
5
8
13
21
PL/SQL procedure successfully completed.
(Control Structures:)
SALARY OF SPECIFIC EMPLOYEE
WEEK 7A:
7.a)AIM: Write a program in PL/SQL to update the salary of a specific employee by 8% if the salary exceeds
the mid-range of the salary against this job and update up to mid-range if the salary is less than the mid-range
of the salary, and display a suitable message.
IMPLEMENTATION:
CREATE TABLE staff(employee_id number,e_name varchar(20),salary number);
INSERT INTO staff values(1,venky,16950);
INSERT INTO staff values(2,ram,15000);
INSERT INTO staff values(3,shyam,17000);
INSERT INTO staff values(4,raju,18200);
DECLARE
mid_range staff.salary%type;
eid staff.employee_id%type:=&eid;
e_salary staff.salary%type;
BEGIN
select median(salary) into mid_range from staff;
select salary into e_salary from staff where employee_id=eid;
if(e_salary>mid_range) then
update staff set salary=salary+(salary*0.08) where employee_id=eid;
select salary into e_salary from staff where employee_id=eid;
dbms_output.put_line('Employee with id='||eid||'salary is increased by 8% i.e., '||e_salary);
else
update staff set salary=mid_range where employee_id=eid;
dbms_output.put_line('Employee with id='||eid||'salary is updated to midrange('||mid_range||')');
end if;
commit;
end;
/
OUTPUT:
Enter value for eid: 2
old 3: eid staff.employee_id%type:=&eid;
new 3: eid staff.employee_id%type:=2;
Employee with id=2 salary is updated to midrange(17500)
STUDENT’S GRADE
(USING CASE STATEMENT)
WEEK 7b:
7. b) AIM: Write a PL/SQL program to display the description against a student‘s grade using CASE statement.
IMPLEMENTATION:
SQL> declare
2 grade varchar2(3):='A';
3 begin
4 case grade
5 when 'A' then dbms_output.put_line('Distinction');
6 when 'B' then dbms_output.put_line('First Class');
7 when 'C' then dbms_output.put_line('Second Class');
8 when 'D' then dbms_output.put_line('pass');
9 when 'E' then dbms_output.put_line('Fail');
10 else
11 dbms_output.put_line('invalid');
12 end case;
13 end;
14 /
Distinction
WEEK 8b:
8.b) AIM: Construct the user-defined exceptions to get the salary of an employee and check it with the job‘s
salary range. If the salary is below the range, raise an exception BELOW_SALARY_RANGE. If the salary is above
the range, raise the exception ABOVE_SALARY_RANGE.
IMPLEMENTATION:
DECLARE
min_salary employees.salary%type;
max_salary min_salary%type;
e_id employees.id%type:=&e_id;
e_salary max_salary%type;
BELOW_SALARY_RANGE EXCEPTION;
ABOVE_SALARY_RANGE EXCEPTION;
BEGIN
select minsalary,maxsalary into min_salary,max_salary from jobs where id=e_id;
select SALARY into e_salary from employees where id=e_id;
CASE
when e_salary<min_salary then raise BELOW_SALARY_RANGE;
when e_salary>max_salary then raise ABOVE_SALARY_RANGE;
else dbms_output.put_line('Employee salary is in the range '
||min_salary||' '||max_salary);
end CASE;
EXCEPTION
when BELOW_SALARY_RANGE then dbms_output.put_line('Employee salary is below the salary range');
when ABOVE_SALARY_RANGE then dbms_output.put_line('Employee salary is above the salary range');
END;
/
Functions:
WEEK 9A:
9. a) AIM: Write a function that accepts two numbers A and B and performs the following operations.
o Addition
o Subtraction
o Multiplication
o Division
IMPLEMENTATION:
declare
a number:=&a;
b number:=&b;
c number;
d number;
e number;
f number;
function add(x number,y number)
return number
is
z number;
begin
z:=x+y;
return z;
end;
function sub(x number,y number)
return number
is
z number;
begin
z:=x-y;
return z;
end;
function mul(x number,y number)
return number
is
z number;
begin
z:=x*y;
return z;
end;
function div(x number,y number)
return number
is
z number;
begin
z:=x/y;
return z;
end;
begin
c:=add(a,b);
d:=sub(a,b);
e:=mul(a,b);
f:=div(a,b);
dbms_output.put_line('sum is '||c);
dbms_output.put_line('difference is '||d);
dbms_output.put_line('multiplication is '||e);
dbms_output.put_line('division is '||f);
end;
/
UPDATES SALARY OF AN EMPLOYEE
(Using incr function)
WEEK 9B:
9.b)AIM: Write a PL/SQL block that updates salary of an employee in Employee table by using incr function
which takes employee number as argument and calculates increment and returns increment based on the
following criteria. If salary <= 3000, increment = 30% of salary If salary > 3000 and <= 6000, increment = 20%
of salary else increment = 10% of salary.
IMPLEMENTATION:
create table emp(empno number,sal number);
insert into emp values (1,3000);
insert into emp values (2,4500);
insert into emp values (3,6000);
insert into emp values (4,2800);
DECLARE
e_id emp.empno%type:=&e_id;
e_sal emp.sal%type;
e_incr emp.sal%type;
e_values number;
function incr(e_id in number,e_incr out number) return number
is e_salary emp.sal%type:=0;
begin
select sal into e_salary from emp where empno=e_id;
if(e_salary<=3000) then
e_salary:=e_salary*0.3;
e_incr:=30;
elsif(e_salary>3000 and e_salary<=6000) then
e_salary:=e_salary*0.2;
e_incr:=20;
else
e_salary:=e_salary*0.1;
e_incr:=10;
end if;
return e_salary;
end;
BEGIN
select sal into e_sal from emp where empno=e_id;
dbms_output.put_line('Employee salary before incrementing'||e_sal);
e_values:=incr(e_id,e_incr);
update emp set sal=sal +e_values where empno=e_id;
commit;
select sal into e_sal from emp where empno=e_id;
dbms_output.put_line('Employess salary after incrementing by;'||e_incr||'% is'||e_sal);
EXCEPTION
WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('EMPLOYEE DATA NOT FOUND');
when others then dbms_output.put_line('ERROR');
END;
/
Procedures:
(DISPLAYS THEIR SUM)
WEEK 10A:
10. a)AIM: Write a procedure that accepts two numbers and displays their sum.
IMPLEMENTATION:
SQL> set serveroutput on;
SQL> create procedure f5(a in number,b in number)
2 as
3 begin
4 dbms_output.put_line('result is '||(a+b));
5 end;
6 /
Procedure created.
321
Starred
Snoozed
Sent
Drafts
PROCEDURES WITH PARAMETERS
WEEK 10b:
10.b)AIM: Write procedures to demonstrate IN, IN OUT and OUT parameters.
IMPLEMENTATION:
SQL> set serveroutput on;
SQL> declare
2 a number:=&a;
3 b number:=&b;
4 c number;
5 procedure p5(a in number,b in out number,c out number)
6 as
7 begin
8 c:=a+b;
9 b:=a-b;
10 end;
11 begin
12 p5(a,b,c);
13 dbms_output.put_line('The value of c is '||c||'The value of b is '||b);
14 end;
15 /
Enter value for a: 15
old 2: a number:=&a;
new 2: a number:=15;
Enter value for b: 24
old 3: b number:=&b;
new 3: b number:=24;
The value of c is 39 The value of b is -9
loop
OUTPUT:
Employee david has done 4job(s) in the past
Employee valli has done 8job(s) in the past
Employee diana has done 3job(s) in the past
Employee Alexander's done 2job(s) in the past
Employee mukesh has done 10job(s) in the past
DISPLAY THE NAME & SALARY OF EACH EMPLOYEE
(Using cursors)
WEEK 11b:
11.b)AIM: Write a program in PL/SQL to create a cursor to display the name and salary of each employee in the
EMPLOYEES table whose salary is less than that specified by a passed-in parameter value.
IMPLEMENTATION:
-- creating employee table with name emp
--ename is the name of the employee and sal is the salary of employee
--cursor key is used to create cursor .refer google to know more about cursor
--%type is used to keep same datatype of a column of a table to the variable
BEGIN
open c_emp(salary);
--open property is used to specify that we intend to use for read only but not updating