Dbms Lab 7 19IT030
Dbms Lab 7 19IT030
No: 7
PL/SQL BLOCK CREATION AND USAGE OF
VARIOUS COMPOSITE DATA TYPES
Aim:
To practice with PL/SQL block creation and various composite data types
Description:
PL/SQL is a combination of SQL along with the procedural features of programming languages. It
LOOP
//Sequence of statements;//
END LOOP;
Procedure:
Example:
program takes two inputs one for each variable and adds the result to a third variable and prints it.
Declare
Var1 integer;
Var2 integer;
Var3 integer;
Begin
Var1:=&var1;
Var2:=&var2;
Var3:=var1+var2;
Dbms_output.put_line(var3);
End;
/
2. PL/SQL Program to Find Factorial of a Number:
DECLARE
n number;
fac number:=1;
i number;
BEGIN
n:=&n;
FOR i in 1..n
LOOP
fac:=fac*i;
END LOOP;
dbms_output.put_line('factorial='||fac);
end;
/
3. PL/SQL Program for Armstrong Number:
DECLARE
n INT := 407;
s INT := 0;
r INT;
m INT;
BEGIN
m := n;
WHILE n > 0
LOOP
r := mod(n, 10);
s := s + power(r, 3);
n := trunc(n/10);
END LOOP;
IF m = s
THEN
dbms_output.put_line('Armstrong number');
ELSE
END IF;
END;
DECLARE
n INT := &n;
BEGIN
IF mod(n, 2) = 0
THEN
dbms_output.put_line('Number is even');
ELSE
dbms_output.put_line('Number is odd');
END IF;
END;
/
5. PL/SQL program for finding the maximum of two values:
DECLARE
n1 INT;
n2 INT;
result INT;
BEGIN
n1 := &input1;
n2 := &input2;
IF n1 > n2
THEN
ELSIF n1 < n2
THEN
ELSE
END IF;
END;
/
PL/SQL commands for Election Database Management System
1. PL/SQL program to find the constituency with minimum population from the Constituency
table:
DECLARE
CURSOR cursor1 IS
SELECT population FROM constituency;
pop constituency.population%type;
minimum INTEGER := 1000;
BEGIN
FOR POP IN cursor1
LOOP
IF POP.POPULATION < minimum
THEN
minimum := POP.POPULATION;
END IF;
END LOOP;
dbms_output.put_line('The minimum population of all the constituencies in the table is ' ||
minimum || ' thousands');
END;
/
2. PL/SQL program to find the constituency with maximum population from the Constituency
table:
DECLARE
CURSOR cursor1 IS
SELECT population FROM constituency;
pop constituency.population%type;
maximum INTEGER := 100;
BEGIN
FOR POP IN cursor1
LOOP
IF POP.POPULATION > maximum
THEN
maximum := POP.POPULATION;
END IF;
END LOOP;
dbms_output.put_line('The maximum population of all the constituencies in the table is ' ||
maximum || ' thousands');
END;
/
3. PL/SQL program to find the average population of all the constituencies from the Constituency
table:
DECLARE
CURSOR cursor1 IS
SELECT population FROM constituency;
pop constituency.population%type;
total int := 0.00;
average int := 0.00;
counter int := 0;
BEGIN
FOR pop IN cursor1
LOOP
total := total + POP.population;
counter := counter + 1;
END LOOP;
average := total/counter;
dbms_output.put_line( 'The average population of all the constituencies in the table is ' || average
|| ' thousands');
END;
/
4. PL/SQL program to find the Population of STATE of all the constituencies from the
Constituency table:
DECLARE
CURSOR cursor1 IS
SELECT population FROM constituency;
pop constituency.population%type;
total int:= 0;
BEGIN
FOR pop IN cursor1
LOOP
total := total + POP.population;
END LOOP;
dbms_output.put_line('The total population of all the constituencies in the table is ' || total || '
thousands');
END;
/
DECLARE
Candidate_name candidate.name%type;
Candidate_id candidate.cand_id%type;
CURSOR cursor1 IS
SELECT name,cand_id FROM candidate;
BEGIN
OPEN cursor1;
LOOP
FETCH cursor1 INTO candidate_name,candidate_id;
EXIT WHEN cursor1%notfound;
dbms_output.put_line(candidate_name|| ' ' || candidate_id);
END LOOP;
CLOSE cursor1;
END;
/
RESULT:
Thus, the PL/SQL block has been created and executed successfully.