0% found this document useful (0 votes)
20 views

DBMS LAB Practice Questions

Uploaded by

mail.ashish005ak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

DBMS LAB Practice Questions

Uploaded by

mail.ashish005ak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

1. Write a PL/SQL program to find the area of circle.

DECLARE
radius NUMBER;
area NUMBER;
BEGIN
radius := 5;
area := 3.14159 * radius * radius;
DBMS_OUTPUT.PUT_LINE('The area of the circle with radius ' || radius || ' is ' || area);
END;
/
2. Write a PL/SQL program to find sum of digits of a number.

DECLARE
num NUMBER := 12345;
sum_of_digits NUMBER := 0;
digit NUMBER;
BEGIN
WHILE num > 0 LOOP
digit := num MOD 10;
sum_of_digits := sum_of_digits + digit;
num := num / 10;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The sum of the digits is ' || sum_of_digits);
END;
/
3. Write a PL/SQL program to check whether the given number is Armstrong number or
not

DECLARE
num NUMBER := 153;
original_num NUMBER;
sum_of_powers NUMBER := 0;
digit NUMBER;
n NUMBER := 0;
BEGIN
original_num := num;
WHILE original_num > 0 LOOP
n := n + 1;
original_num := original_num / 10;
END LOOP;
original_num := num;
WHILE original_num > 0 LOOP
digit := original_num MOD 10;
sum_of_powers := sum_of_powers + POWER(digit, n);
original_num := original_num / 10;
END LOOP;
IF sum_of_powers = num THEN
DBMS_OUTPUT.PUT_LINE(num || ' is an Armstrong number.');
ELSE
DBMS_OUTPUT.PUT_LINE(num || ' is not an Armstrong number.');
END IF;
END;
/
4. Write a PL/SQL Program to Check Number is Odd or Even.
DECLARE
num NUMBER := 10;
BEGIN
IF MOD(num, 2) = 0 THEN
DBMS_OUTPUT.PUT_LINE(num || ' is even.');
ELSE
DBMS_OUTPUT.PUT_LINE(num || ' is odd.');
END IF;
END;
/
5. Write a PL/SQL recursive function to find factorial of a number using Procedures.
CREATE OR REPLACE PROCEDURE find_factorial(n IN NUMBER, result OUT
NUMBER) IS
FUNCTION factorial(num NUMBER) RETURN NUMBER IS
BEGIN
IF num = 0 THEN
RETURN 1;
ELSE
RETURN num * factorial(num - 1);
END IF;
END;
BEGIN
result := factorial(n);
END;
/
DECLARE
num NUMBER := 5;
result NUMBER;
BEGIN
find_factorial(num, result);
DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is ' || result);
END;
/
6. Write a PL/SQL program to find the factorial of number
DECLARE
num NUMBER := 5;
result NUMBER := 1;
i NUMBER;
BEGIN
FOR i IN 1..num LOOP
result := result * i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is ' || result);
END;
/
7. wirte a program to check whether the given number is even or odd.

DECLARE
num NUMBER := 10;
BEGIN
IF MOD(num, 2) = 0 THEN
DBMS_OUTPUT.PUT_LINE(num || ' is even.');
ELSE
DBMS_OUTPUT.PUT_LINE(num || ' is odd.');
END IF;
END;
/
8. Write a PL/SQL procedure to print first n Fibonacci numbers
CREATE OR REPLACE PROCEDURE print_fibonacci(n IN NUMBER) IS
a NUMBER := 0;
b NUMBER := 1;
next NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE(a);
DBMS_OUTPUT.PUT_LINE(b);
FOR i IN 3..n LOOP
next := a + b;
DBMS_OUTPUT.PUT_LINE(next);
a := b;
b := next;
END LOOP;
END;
/
BEGIN
print_fibonacci(10);
END;
/
9. Write a PL/SQL program to find the reverse of a number
DECLARE
num NUMBER := 12345;
reverse_num NUMBER := 0;
digit NUMBER;
BEGIN
WHILE num > 0 LOOP
digit := num MOD 10;
reverse_num := reverse_num * 10 + digit;
num := num / 10;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The reverse of the number is ' || reverse_num);
END;
/
10.Write a PL/SQL function to find sum of digits of a number
CREATE OR REPLACE FUNCTION sum_of_digits(num IN NUMBER) RETURN
NUMBER IS
sum_digits NUMBER := 0;
digit NUMBER;
BEGIN
WHILE num > 0 LOOP
digit := num MOD 10;
sum_digits := sum_digits + digit;
num := num / 10;
END LOOP;
RETURN sum_digits;
END;
/
DECLARE
result NUMBER;
BEGIN
result := sum_of_digits(12345);
DBMS_OUTPUT.PUT_LINE('The sum of the digits is ' || result);
END;
/
11.Write a PL/SQL program on displaying numbers from 1 to 10
BEGIN
FOR i IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
END LOOP;
END;
/
12.Write a PL/SQL procedure to find factorial of a given number
CREATE OR REPLACE PROCEDURE find_factorial(num IN NUMBER, result OUT
NUMBER) IS
BEGIN
result := 1;
FOR i IN 1..num LOOP
result := result * i;
END LOOP;
END;
/
DECLARE
result NUMBER;
BEGIN
find_factorial(5, result);
DBMS_OUTPUT.PUT_LINE('Factorial is ' || result);
END;
/
13.Write a PL/SQL program that gives reverse of a given number
DECLARE
num NUMBER := 12345;
reverse_num NUMBER := 0;
digit NUMBER;
BEGIN
WHILE num > 0 LOOP
digit := num MOD 10;
reverse_num := reverse_num * 10 + digit;
num := num / 10;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The reverse of the number is ' || reverse_num);
END;
/
14.Write a PL/SQL program to retrieve records from the sailors table using cursor
DECLARE
CURSOR sailor_cursor IS
SELECT * FROM sailors;
sailor_record sailors%ROWTYPE;
BEGIN
OPEN sailor_cursor;
LOOP
FETCH sailor_cursor INTO sailor_record;
EXIT WHEN sailor_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('ID: ' || sailor_record.sid || ', Name: ' ||
sailor_record.sname || ', Rating: ' || sailor_record.rating || ', Age: ' ||
sailor_record.age);
END LOOP;
CLOSE sailor_cursor;
END;
/
15.Write a procedure to calculate factorial of a given number
CREATE OR REPLACE PROCEDURE calculate_factorial(num IN NUMBER, result
OUT NUMBER) IS
BEGIN
result := 1;
FOR i IN 1..num LOOP
result := result * i;
END LOOP;
END;
/
DECLARE
result NUMBER;
BEGIN
calculate_factorial(5, result);
DBMS_OUTPUT.PUT_LINE('Factorial is ' || result);
END;
/
16.Write a PL/SQL program to find given no is palindrome.
DECLARE
num NUMBER := 12321;
reverse_num NUMBER := 0;
original_num NUMBER := num;
digit NUMBER;
BEGIN
WHILE num > 0 LOOP
digit := num MOD 10;
reverse_num := reverse_num * 10 + digit;
num := num / 10;
END LOOP;
IF reverse_num = original_num THEN
DBMS_OUTPUT.PUT_LINE(original_num || ' is a palindrome.');
ELSE
DBMS_OUTPUT.PUT_LINE(original_num || ' is not a palindrome.');
END IF;
END;
/
17.Write a cursor program for Explicit method
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, first_name, last_name FROM employees;
emp_record employees%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('ID: ' || emp_record.employee_id || ', Name: ' ||
emp_record.first_name || ' ' || emp_record.last_name);
END LOOP;
CLOSE emp_cursor;
END;
/
18.Write a cursor program for Implicit method
BEGIN
FOR emp_record IN (SELECT employee_id, first_name, last_name FROM employees)
LOOP
DBMS_OUTPUT.PUT_LINE('ID: ' || emp_record.employee_id || ', Name: ' ||
emp_record.first_name || ' ' || emp_record.last_name);
END LOOP;
END;
/
19.Write a PL/SQL program on displaying smallest of two numbers
DECLARE
a NUMBER := 10;
b NUMBER := 20;
BEGIN
IF a < b THEN
DBMS_OUTPUT.PUT_LINE('The smallest number is ' || a);
ELSE
DBMS_OUTPUT.PUT_LINE('The smallest number is ' || b);
END IF;
END;
/
20.Write a PL/SQL program to find the average of two numbers.
DECLARE
a NUMBER := 10;
b NUMBER := 20;
average NUMBER;
BEGIN
average := (a + b) / 2;
DBMS_OUTPUT.PUT_LINE('The average of ' || a || ' and ' || b || ' is ' || average);
END;
/
21.Write a trigger program to convert data entered to the table to upper case while inserting
data in to the table
CREATE OR REPLACE TRIGGER trg_uppercase
BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
:NEW.column_name := UPPER(:NEW.column_name);
END;
/
22.Write a PL/SQL program to find the reverse of number
DECLARE
num NUMBER := 12345;
reverse_num NUMBER := 0;
digit NUMBER;
BEGIN
WHILE num > 0 LOOP
digit := num MOD 10;
reverse_num := reverse_num * 10 + digit;
num := num / 10;
END LOOP;
DBMS_OUTPUT.PUT_LINE('The reverse of the number is ' || reverse_num);
END;
/
23.Write a PL/SQL program to find the area of rectangle
DECLARE
length NUMBER := 10;
width NUMBER := 5;
area NUMBER;
BEGIN
area := length * width;
DBMS_OUTPUT.PUT_LINE('The area of the rectangle is ' || area);
END;
/
24.Write a cursor program for Explicit method .
DECLARE
CURSOR dept_cursor IS
SELECT department_id, department_name FROM departments;
dept_record departments%ROWTYPE;
BEGIN
OPEN dept_cursor;
LOOP
FETCH dept_cursor INTO dept_record;
EXIT WHEN dept_cursor%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('ID: ' || dept_record.department_id || ', Name: ' ||
dept_record.department_name);
END LOOP;
CLOSE dept_cursor;
END;
/
25. Give an Example of PL/SQL program to find the biggest of two numbers.
DECLARE
a NUMBER := 10;
b NUMBER := 20;
BEGIN
IF a > b THEN
DBMS_OUTPUT.PUT_LINE('The biggest number is ' || a);
ELSE
DBMS_OUTPUT.PUT_LINE('The biggest number is ' || b);
END IF;
END;
/
26 .Write a PL/SQL program to find Armstrong numbers in a given range m to n
DECLARE
m NUMBER := 1;
n NUMBER := 500;
num NUMBER;
original_num NUMBER;
sum_of_powers NUMBER;
digit NUMBER;
num_digits NUMBER;
BEGIN
FOR num IN m..n LOOP
original_num := num;
sum_of_powers := 0;
num_digits := TRUNC(LOG(10, num) + 1);
WHILE original_num > 0 LOOP
digit := original_num MOD 10;
sum_of_powers := sum_of_powers + POWER(digit, num_digits);
original_num := original_num / 10;
END LOOP;
IF sum_of_powers = num THEN
DBMS_OUTPUT.PUT_LINE(num || ' is an Armstrong number.');
END IF;
END LOOP;
END;
/

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy