0% found this document useful (0 votes)
29 views6 pages

Assignment 6

DBMS practical

Uploaded by

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

Assignment 6

DBMS practical

Uploaded by

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

Group A

Assignment No: 06

Title:

Cursors in MySQL: Using Implicit, Explicit, and Parameterized Cursors

Aim:

To write a MySQL stored procedure using cursors to merge data from the N_Roll_Call table
into the O_Roll_Call table. If the data in N_Roll_Call already exists in O_Roll_Call, that data
will be skipped.

Problem Statement:

You are tasked with merging student attendance data from the N_Roll_Call table into the
O_Roll_Call table. For each record, if the student_id and date_of_attendance already exist
in O_Roll_Call, the record should be skipped. You will write a MySQL stored procedure to use
cursors for row-by-row data insertion, ensuring duplicates are avoided.

Hardware Requirements:

 Processor: Intel Core i3 or higher.


 RAM: 4GB minimum.
 Storage: 50GB of free space.
 Database Server: MySQL (locally or on a remote server).
 Network Adapter: For database connectivity.

Software Requirements:

 Operating System: Ubuntu 18.04 or higher.


 Database: MySQL 8.0 or higher.
Theory:

Cursors in MySQL:

A cursor in MySQL is a database object used to retrieve, process, and traverse records from a
result set row by row. Unlike traditional SQL operations that deal with the entire result set at
once, a cursor allows for row-level processing, which is essential in situations where row-by-row
handling is required, such as in complex data transformations or decision-making processes.
Cursors are most commonly used within stored procedures or functions in MySQL.

In MySQL, cursors are mainly used in stored procedures for iterative row processing. This gives
developers more control over how individual rows are handled, rather than operating on all rows
simultaneously with SQL commands like UPDATE, DELETE, or INSERT.

Types of Cursors in MySQL:

1. Implicit Cursors:
o MySQL does not explicitly support the term "implicit cursors" like Oracle, but
every INSERT, UPDATE, DELETE, and SELECT INTO statement internally operates
using implicit cursors.
o These are automatically managed by MySQL, and the database engine processes
the query result as a single set of rows in the background without any explicit
control from the developer.
o Example: If you run a SELECT query to fetch data, MySQL automatically handles
the process of retrieving the result set in memory. As a user, you don’t manually
fetch rows, making it an implicit operation.
2. Explicit Cursors:

 An explicit cursor is a cursor that you define explicitly in a stored procedure, allowing
you to fetch individual rows from the result set manually.
 These cursors provide more control to developers when dealing with row-by-row
operations. They are declared in a stored procedure, opened to retrieve rows from a query
result, fetched to retrieve one row at a time, and finally closed after the operations are
done.
 Use Case: Explicit cursors are useful when you need to perform specific actions for each
row in the result set, like conditional checks or aggregating values based on individual
rows.

Key Components of an Explicit Cursor:

 Declare Cursor: Define a cursor to hold the result set of a query.


 Open Cursor: Open the cursor to start fetching rows.
 Fetch Cursor: Fetch each row into a set of variables.
 Close Cursor: After all rows are processed, close the cursor to free up resources.
Cursor FOR Loop:

 MySQL does not support FOR LOOP in the same way as PL/SQL does. However, the
LOOP construct in MySQL can be used in combination with cursors to replicate similar
functionality.
 A cursor loop continuously fetches records from the cursor until all rows have been
processed. This loop typically checks whether more rows exist using a handler for the
NOT FOUND condition.
 Use Case: This is useful for repetitive tasks where every row needs to be processed, such
as updating records or inserting data into another table based on specific conditions.

Key Components:

 Declare the cursor.


 Use a LOOP construct to iterate over the result set.
 Fetch the row data and perform necessary actions within the loop.
 Use an exit condition when the cursor has no more rows.

Parameterized Cursors:

 While MySQL does not natively support parameterized cursors (i.e., cursors that accept
parameters), you can achieve a similar effect by passing parameters to a stored procedure
and using them inside the cursor’s SELECT statement.
 A parameterized cursor in other databases like Oracle allows passing arguments at
runtime, making the cursor's result set dynamic based on the input values. In MySQL,
however, this is implemented by using the input parameters of the stored procedure
directly in the cursor’s query.
 Use Case: This is particularly helpful when you need to execute the same cursor logic for
different sets of data, based on conditions passed at runtime.
Example:

1. Creating the Tables:

Before working with cursors, we need to create the N_Roll_Call and O_Roll_Call tables.

CREATE TABLE N_Roll_Call (


student_id INT,
name VARCHAR(50),
date_of_attendance DATE
);

CREATE TABLE O_Roll_Call (


student_id INT,
name VARCHAR(50),
date_of_attendance DATE
);

2. Insert Sample Data:

INSERT INTO N_Roll_Call (student_id, name, date_of_attendance) VALUES


(101, 'John Doe', '2024-01-01'),
(102, 'Jane Smith', '2024-01-02'),
(103, 'Michael Brown', '2024-01-03');

INSERT INTO O_Roll_Call (student_id, name, date_of_attendance) VALUES


(101, 'John Doe', '2024-01-01');

3. Writing the Stored Procedure with Cursors:

The following stored procedure uses a cursor to merge data from N_Roll_Call into
O_Roll_Call, skipping existing records based on the student_id and date_of_attendance.

DELIMITER //

CREATE PROCEDURE MergeRollCall()


BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE v_student_id INT;
DECLARE v_name VARCHAR(50);
DECLARE v_date_of_attendance DATE;

-- Declare a cursor for selecting rows from N_Roll_Call


DECLARE roll_call_cursor CURSOR FOR
SELECT student_id, name, date_of_attendance
FROM N_Roll_Call;

-- Declare a handler to handle the end of the cursor


DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

-- Open the cursor


OPEN roll_call_cursor;
read_loop: LOOP
-- Fetch the next row from the cursor
FETCH roll_call_cursor INTO v_student_id, v_name,
v_date_of_attendance;

-- Exit the loop if there are no more rows


IF done THEN
LEAVE read_loop;
END IF;

-- Insert the row into O_Roll_Call if it does not already exist


IF NOT EXISTS (
SELECT 1 FROM O_Roll_Call
WHERE student_id = v_student_id
AND date_of_attendance = v_date_of_attendance
) THEN
INSERT INTO O_Roll_Call (student_id, name, date_of_attendance)
VALUES (v_student_id, v_name, v_date_of_attendance);
END IF;

END LOOP;

-- Close the cursor


CLOSE roll_call_cursor;

END //

DELIMITER ;

Explanation:

 Cursor Declaration: We declare a cursor roll_call_cursor to select all rows from the
N_Roll_Call table.
 Handler Declaration: We use a handler for the NOT FOUND condition, which sets done =
TRUE when there are no more rows to fetch from the cursor.
 Cursor Loop: Inside the LOOP, we fetch each row from the cursor and check if the
student_id and date_of_attendance already exist in the O_Roll_Call table using an
IF NOT EXISTS condition.
 Data Insertion: If the record does not exist in O_Roll_Call, we insert it into the table.
 Loop Exit: The loop exits once the cursor has processed all rows.

4. Execute the Stored Procedure:

After creating the stored procedure, you can execute it to perform the merge operation:

CALL MergeRollCall();

5. Verify the Results:


Check if the data has been correctly merged into O_Roll_Call:

SELECT * FROM O_Roll_Call;

You should see the data from N_Roll_Call merged into O_Roll_Call, with no duplicate entries.

Conclusion:

In this practical, we explored how to use cursors in MySQL to handle row-by-row data
processing and merge records between two tables. The stored procedure ensures that duplicate
data is skipped during the merge operation, demonstrating effective use of MySQL cursors
within a real-world scenario.

You might also like