Assignment 6
Assignment 6
Assignment No: 06
Title:
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:
Software Requirements:
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.
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.
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:
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:
Before working with cursors, we need to create the N_Roll_Call and O_Roll_Call tables.
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 //
END LOOP;
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.
After creating the stored procedure, you can execute it to perform the merge operation:
CALL MergeRollCall();
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.