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

DBMS Practical

practical file for database mangement system

Uploaded by

Chirag Dugar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DBMS Practical

practical file for database mangement system

Uploaded by

Chirag Dugar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

EXPERIMENT NO: 5

EXPERIMENT TITLE: 3 Programs Using PL/SQL

Program 1: Calculate Factorial of a Number:

AIM: To calculate the factorial of a given number using PL/SQL.

Algorithm:

1. Declare variables for input number, factorial result, and loop counter.
2. Accept the input number.
3. Initialize the factorial result to 1.
4. Use a loop to multiply numbers from 1 to the input number.
5. Display the factorial result.

Code:

DECLARE
num NUMBER := 5;
factorial NUMBER := 1;
i NUMBER;
BEGIN
FOR i IN 1..num LOOP
factorial := factorial * i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is ' || factorial);
END;
/
Screenshot:

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Sample Input/Output:

Input: 5
Output: Factorial of 5 is 120.

Outcome:

Successfully calculated the factorial of a number using a FOR loop in PL/SQL and displayed the result.

Program 2: Check if a Number is Prime

AIM: To check if a given number is prime.

Algorithm:

1. Take the input number.


2. Initialize a flag variable to indicate whether the number is prime.
3. Use a loop to check divisors from 2 to the square root of the number.
4. If a divisor is found, set the flag to 'false'.
5. Output whether the number is prime.

Code:

DECLARE

num NUMBER := 371;

i NUMBER;

is_prime BOOLEAN := TRUE;

BEGIN

IF num <= 1 THEN

is_prime := FALSE;

ELSE

FOR i IN 2..FLOOR(SQRT(num)) LOOP

IF MOD(num, i) = 0 THEN

is_prime := FALSE;

EXIT;
NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024
REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

END IF;

END LOOP;

END IF;

IF is_prime THEN

DBMS_OUTPUT.PUT_LINE(num || ' is a prime number.');

ELSE

DBMS_OUTPUT.PUT_LINE(num || ' is not a prime number.');

END IF;

END;

Screenshot:

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Sample Input/Output:

Input: 371
Output: 371 is not a prime number.

Outcome:

Successfully determined if a number is prime by iterating over possible divisors and checking for
divisibility.

Program 3: Reverse a String

AIM: To reverse a string using PL/SQL.

Algorithm:

1. Accept the input string.

2. Use a loop to iterate through the string in reverse order.

3. Concatenate the characters into a new string.

4. Display the reversed string.

Code:

DECLARE

input_str VARCHAR2(100) := 'chirag';

reversed_str VARCHAR2(100) := '';

i NUMBER;

BEGIN

FOR i IN REVERSE 1..LENGTH(input_str) LOOP

reversed_str := reversed_str || SUBSTR(input_str, i, 1);

END LOOP;

DBMS_OUTPUT.PUT_LINE('Reversed string: ' || reversed_str);

END;

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Screenshot:

Sample Input/Output:

Input: chirag

Output: Reversed string: garihc

Outcome:

Successfully reversed a given string by iterating over its characters in reverse order and displayed the
reversed string.

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

EXPERIMENT NO: 6

EXPERIMENT TITLE: 3 Programs Using Loops and IF/ELSE in PL/SQL.

Program 1: Calculate Factorial of a Number:

AIM: Find Sum of First N Numbers

Algorithm:

1. Declare a variable to store the sum.


2. Accept the input N.
3. Use a loop to iterate from 1 to N.
4. Add each number to the sum.
5. Display the sum.

Code:

DECLARE

n NUMBER := 10;

total_sum NUMBER := 0;

BEGIN

FOR i IN 1..n LOOP

total_sum := total_sum + i;

END LOOP;

DBMS_OUTPUT.PUT_LINE('Sum of first ' || n || ' numbers is ' || total_sum);

END;

/
Screenshot:

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Sample Input/Output:

Input: 10

Output: Sum of first 10 numbers is 55

Outcome:

Successfully calculated the sum of the first N natural numbers using a FOR loop and displayed the
result.

Program 2: Display Multiplication Table

AIM: To display the multiplication table of a number.

Algorithm:

1. Accept the input number.


2. Use a loop to iterate from 1 to 10.
3. Multiply the number by the loop index.
4. Display the result.

Code:

DECLARE

num NUMBER := 5;

i NUMBER;

BEGIN

FOR i IN 1..10 LOOP

DBMS_OUTPUT.PUT_LINE(num || ' x ' || i || ' = ' || num * i);

END LOOP;

END;

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Output Screenshot:

Sample Input/Output:

Input: 5

Output: 5 x 1 = 5, 5 x 2 = 10, ..., 5 x 10 = 50

Outcome:

Successfully generated and displayed the multiplication table for a given number using a loop in
PL/SQL.

Program 3: Check Even or Odd

AIM: To check whether a number is even or odd using IF/ELSE.

Algorithm:

1. Accept the input number.


2. Use the MOD function to check if the number is divisible by 2.
3. If divisible by 2, the number is even.
4. Otherwise, the number is odd.
5. Display the result.

Code:
NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024
REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

DECLARE

num NUMBER := 5578;

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;

Screenshot:

Sample Input/Output:

Input: 5578

Output: 5578 is even.

Outcome:

Successfully checked whether a number is even or odd using an IF/ELSE statement and displayed the
appropriate result.

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

EXPERIMENT NO: 7 & 8

EXPERIMENT TITLE: 4 Programs on Stored Procedures

Program 1: Add Two Numbers (IN Parameters)

AIM: To create a procedure that adds two numbers and returns the result.

Algorithm:

1. Create a procedure with two input parameters (a and b).


2. Perform the addition.
3. Use an OUT parameter to return the result.
4. Display the result.

Code:

CREATE OR REPLACE PROCEDURE AddNumbers (a IN NUMBER, b IN NUMBER, result OUT NUMBER)


AS

BEGIN

result := a + b;

END;

DECLARE

res NUMBER;

BEGIN

AddNumbers(10, 20, res);

DBMS_OUTPUT.PUT_LINE('Sum is: ' || res);

END;

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Screenshot:

Sample Input/Output:

Input: 10, 20

Output: Sum is 30

Outcome:

Successfully created a stored procedure that accepts two input parameters, calculates their sum, and
returns the result using an OUT parameter.

Program 2: Find Maximum of Two Numbers (IN/OUT)

AIM: To create a procedure that finds the maximum of two numbers.

Algorithm:

1. Create a procedure with two input parameters (a and b).


2. Use an OUT parameter to return the maximum value.
3. Use an IF statement to compare the two numbers.
4. Return the maximum value.

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Code:

CREATE OR REPLACE PROCEDURE FindMax (a IN NUMBER, b IN NUMBER, max OUT NUMBER) AS

BEGIN

IF a > b THEN

max := a;

ELSE

max := b;

END IF;

END;

DECLARE

maxValue NUMBER;

BEGIN

FindMax(15, 10, maxValue);

DBMS_OUTPUT.PUT_LINE('Maximum is: ' || maxValue);

END;

Screenshot:

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Sample Input/Output:

Input: 15, 10

Output: Maximum is 15

Outcome:

Successfully created a stored procedure to determine the maximum of two numbers by comparing
them and returned the result using an OUT parameter.

Program 3: Greet User (IN Parameter)

AIM: To create a procedure that greets the user.

Algorithm:

1. Create a procedure with one input parameter (name).


2. Use DBMS_OUTPUT to display a greeting message.

Code:

CREATE OR REPLACE PROCEDURE GreetUser (name IN VARCHAR2) AS

BEGIN

DBMS_OUTPUT.PUT_LINE('Hello, ' || name || '!');

END;

BEGIN

GreetUser('Chirag');

END;

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Screenshot:

Sample Input/Output:

Input: Chirag

Output: Hello,Chirag

Outcome:

Successfully created a stored procedure that accepts a user’s name as input and displays a
personalized greeting message.

Program 4: Calculate Area of Circle (IN/OUT)

AIM: To create a procedure that calculates the area of a circle.

Algorithm:

1. Create a procedure with one input parameter (radius).


2. Use an OUT parameter to return the calculated area.
3. Apply the formula for area: π * radius².

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Code:

CREATE OR REPLACE PROCEDURE CircleArea (radius IN NUMBER, area OUT NUMBER) AS

BEGIN

area := 3.14 * radius * radius;

END;

DECLARE

areaValue NUMBER;

BEGIN

CircleArea(5, areaValue);

DBMS_OUTPUT.PUT_LINE('Area is: ' || areaValue);

END;

/
Screenshot:

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML
CSE3001-DBMS LABORATORY SHEET-EXERCISE SHEET

Sample Input/Output:

Input: 5

Output: Area is 78.5

Outcome:

Successfully created a stored procedure that accepts the radius as input, calculates the area of the
circle using the formula πr², and returns the result using an OUT parameter.

NAME: CHIRAG DUGAR DATE OF SUBMISSION:19/11/2024


REG NO: 22BAI10161 BRANCH: COMPUTER SCIENCE WITH SPECIALIZATION IN AI/ML

You might also like