Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
33 views7 pages

Ex7, 8 and 9 Dbms Lab

The document outlines three programming examples in PL/SQL: calculating the sum of the first n natural numbers, creating a trigger to update employee salaries upon promotion, and analyzing student marks using a cursor. Each example includes a flowchart, code snippets, and sample outputs demonstrating the functionality. The document serves as a guide for implementing these PL/SQL concepts.

Uploaded by

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

Ex7, 8 and 9 Dbms Lab

The document outlines three programming examples in PL/SQL: calculating the sum of the first n natural numbers, creating a trigger to update employee salaries upon promotion, and analyzing student marks using a cursor. Each example includes a flowchart, code snippets, and sample outputs demonstrating the functionality. The document serves as a guide for implementing these PL/SQL concepts.

Uploaded by

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

Ex: 7 SUM OF SERIES

AIM

FLOWCHART

PROGRAM

ACCEPT n NUMBER PROMPT 'Enter a number: ';

DECLARE

n NUMBER := &n; -- Takes input from user


i NUMBER :=1;
sum_value NUMBER := 0;

BEGIN

WHILE i<= n
LOOP
sum_value := sum_value + i;
i := i + 1;
END LOOP;

DBMS_OUTPUT.PUT_LINE('Sum of first ' || n || ' natural numbers is: ' ||


sum_value);
END;
/

OUTPUT
SQL> edit D:/sample/series.sql

SQL> @ D:/sample/series.sql
Enter a number: 10
8
Sum of first 10 natural numbers is: 55

PL/SQL procedure successfully completed.

RESULT

EX: 8 TRIGGER
AIM
FLOWCHART

PROGRAM

Step 1: Create the Table and Insert Records


CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
employee_name VARCHAR2(100),
salary NUMBER(10, 2),
promotion_date DATE
);

INSERT INTO employees (employee_id, employee_name, salary, promotion_date)


VALUES (1, 'John Doe', 50000, NULL);

INSERT INTO employees (employee_id, employee_name, salary, promotion_date)


VALUES (2, 'Jane Smith', 60000, NULL);

Step 2: Create the Trigger


The trigger will automatically update an employee’s salary before and after promotion,
assuming a 10% salary increase after promotion.

Step 3: Execute a Query to Update the Promotion Date


UPDATE employees
SET promotion_date = SYSDATE
WHERE employee_id = 1;

Step 4: Display the Updated Record


SELECT * FROM employees;
CREATE OR REPLACE TRIGGER update_salary_on_promotion
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
-- Check if the promotion_date is not NULL (meaning the employee has been
promoted)
IF :NEW.promotion_date IS NOT NULL THEN
-- Apply a 10% raise to the salary
:NEW.salary := :OLD.salary * 1.1;
END IF;
END;
/
OUTPUT

SQL> select * from employees;

EMPLOYEE_ID EMPLOYEE_NAME SALARY


PROMOTION
----------- ----------------------------------------------------------------------------------------------------
--------
1 John Doe
50000
2 Jane Smith
60000

SQL> edit E:/oracle/trigger.sql

SQL> @ E:/oracle/trigger.sql

Trigger created.

SQL> UPDATE employees


2 SET promotion_date = SYSDATE
3 WHERE employee_id = 1;

1 row updated.

SQL> select * from employees;

EMPLOYEE_ID EMPLOYEE_NAME SALARY


PROMOTION
----------- ----------------------------------------------------------------------------------------------------
--------
1 John Doe
55000 10-FEB-25
2 Jane Smith
60000

RESULT
Ex:9 STUDENT MARK ANALYSIS USING CURSOR

AIM:
FLOWCHART:

PROGRAM:

DECLARE
-- Declare a cursor to fetch student marks
CURSOR student_cursor IS
SELECT student_id, student_name, subject1_marks, subject2_marks,
subject3_marks, subject4_marks
FROM student_marks;

-- Declare variables to hold individual student data


v_student_id student_marks.student_id%TYPE;
v_student_name student_marks.student_name%TYPE;
v_subject1_marks student_marks.subject1_marks%TYPE;
v_subject2_marks student_marks.subject2_marks%TYPE;
v_subject3_marks student_marks.subject3_marks%TYPE;
v_subject4_marks student_marks.subject4_marks%TYPE;

-- Declare variables for the analysis


v_total_marks NUMBER;
v_average_marks NUMBER;
v_status VARCHAR2(10);

BEGIN
-- Open the cursor
OPEN student_cursor;

-- Loop through each student record


LOOP
FETCH student_cursor INTO v_student_id, v_student_name,
v_subject1_marks, v_subject2_marks, v_subject3_marks, v_subject4_marks;

-- Exit the loop if no more rows are available


EXIT WHEN student_cursor%NOTFOUND;

-- Calculate total marks and average


v_total_marks := v_subject1_marks + v_subject2_marks +
v_subject3_marks + v_subject4_marks;
v_average_marks := v_total_marks / 4;
-- Determine the status based on average marks
IF v_average_marks >= 50 THEN
v_status := 'Pass';
ELSE
v_status := 'Fail';
END IF;

-- Output the student name and status on one line


DBMS_OUTPUT.PUT_LINE('Student: ' || v_student_name || ' - Status: ' ||
v_status);

-- Output the total marks, average marks, and student ID on another line
DBMS_OUTPUT.PUT_LINE('Total Marks: ' || v_total_marks || ', Average Marks:
' || v_average_marks || ' (Student ID: ' || v_student_id || ')');
END LOOP;

-- Close the cursor


CLOSE student_cursor;
END;
/

OUTPUT:
SQL> CREATE TABLE student_marks (
2 student_id NUMBER PRIMARY KEY,
3 student_name VARCHAR2(100),
4 subject1_marks NUMBER,
5 subject2_marks NUMBER,
6 subject3_marks NUMBER,
7 subject4_marks NUMBER
8 );

Table created.

SQL> -- Insert sample student data into the student_marks table


SQL> INSERT INTO student_marks (student_id, student_name, subject1_marks,
subject2_marks, subject3_marks, subject4_marks)
2 VALUES (101, 'John Doe', 90, 85, 88, 87);

1 row created.

SQL>
SQL> INSERT INTO student_marks (student_id, student_name, subject1_marks,
subject2_marks, subject3_marks, subject4_marks)
2 VALUES (102, 'Jane Smith', 55, 50, 60, 65);

1 row created.

SQL>
SQL> INSERT INTO student_marks (student_id, student_name, subject1_marks,
subject2_marks, subject3_marks, subject4_marks)
2 VALUES (103, 'Mark Taylor', 45, 40, 35, 30);

1 row created.

SQL>
SQL> INSERT INTO student_marks (student_id, student_name, subject1_marks,
subject2_marks, subject3_marks, subject4_marks)
2 VALUES (104, 'Alice Brown', 70, 75, 78, 80);

1 row created.

SQL>
SQL> INSERT INTO student_marks (student_id, student_name, subject1_marks,
subject2_marks, subject3_marks, subject4_marks)
2 VALUES (105, 'Bob White', 30, 35, 40, 25);

1 row created.

SQL> SELECT * FROM student_marks;


STUDENT_ID STUDENT_NAME
SUBJECT1_MARKS SUBJECT2_MARKS SUBJECT3_MARKS SUBJECT4_MARKS
---------- ----------------------------------------------------------------------------------------------------
---------
101 John Doe
90 85 88 87
102 Jane Smith
55 50 60 65
103 Mark Taylor
45 40 35 30
104 Alice Brown
70 75 78 80
105 Bob White
30 35 40 25

SQL> edit E:/oracle/cursor.sql

SQL> @ E:/oracle/cursor.sql
Student: John Doe - Status: Pass
Total Marks: 350, Average Marks: 87.5 (Student ID: 101)
Student: Jane Smith - Status: Pass
Total Marks: 230, Average Marks: 57.5 (Student ID: 102)
Student: Mark Taylor - Status: Fail
Total Marks: 150, Average Marks: 37.5 (Student ID: 103)
Student: Alice Brown - Status: Pass
Total Marks: 303, Average Marks: 75.75 (Student ID: 104)
Student: Bob White - Status: Fail
Total Marks: 130, Average Marks: 32.5 (Student ID: 105)
PL/SQL procedure successfully completed.

RESULT:

You might also like