MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
A MySQL cursor is a database object that allows you to retrieve rows from a result set one at a time. Cursors are used when you need to update records in a database table in a singleton fashion, i.e., a row at a time.
Here is a simple tutorial illustrating the use of MySQL cursors:
Declare the Cursor:
The declaration must happen inside a BEGIN...END block. Usually, this is within a stored procedure. The cursor declaration must always be associated with a SELECT statement.
DECLARE cursor_name CURSOR FOR select_statement;
Open the Cursor:
You can open the cursor to establish the result set.
OPEN cursor_name;
Fetch the Cursor:
Retrieve the next row pointed by the cursor. If a row is fetched, its values are stored into the variables.
FETCH cursor_name INTO variables_list;
Close the Cursor:
Closing the cursor when done with it.
CLOSE cursor_name;
Here is an example of a stored procedure that uses a cursor:
DELIMITER $$ CREATE PROCEDURE `list_employees`() BEGIN DECLARE done INT DEFAULT FALSE; DECLARE a,b CHAR(16); DECLARE cur CURSOR FOR SELECT first_name, last_name FROM employees; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; OPEN cur; read_loop: LOOP FETCH cur INTO a, b; IF done THEN LEAVE read_loop; END IF; -- Display result SELECT a, b; END LOOP; CLOSE cur; END$$ DELIMITER ;
In this example, the cursor cur
is defined and associated with a SELECT
statement that fetches the first_name
and last_name
from the employees
table. The cursor is then opened and a loop is started (read_loop
). Within the loop, rows are fetched from the cursor one at a time and the values are displayed using the SELECT
statement. The loop continues until all rows have been fetched. The done
variable is used as a flag to indicate when all rows have been fetched (NOT FOUND
sets done = TRUE
), at which point the loop is exited.
To use this stored procedure, you would use the CALL
statement:
CALL list_employees();
This will execute the stored procedure, which will fetch and display the first name and last name of each employee, one employee at a time.
How to use cursors in MySQL:
DECLARE
, OPEN
, FETCH
, and CLOSE
statements to work with cursors in MySQL.-- Declare a cursor DECLARE cursor_name CURSOR FOR SELECT column1, column2 FROM my_table; -- Open the cursor OPEN cursor_name; -- Fetch and process data FETCH cursor_name INTO var1, var2; -- Process data here -- Close the cursor CLOSE cursor_name;
Iterating through results with MySQL cursor:
DECLARE my_cursor CURSOR FOR SELECT column1, column2 FROM my_table; OPEN my_cursor; my_loop: LOOP FETCH my_cursor INTO var1, var2; IF done THEN LEAVE my_loop; END IF; -- Process data here END LOOP; CLOSE my_cursor;
Fetching and processing data using MySQL cursor:
FETCH
statement to retrieve values from the cursor into variables.DECLARE my_cursor CURSOR FOR SELECT column1, column2 FROM my_table; OPEN my_cursor; FETCH my_cursor INTO var1, var2; -- Process data here CLOSE my_cursor;
Cursor types in MySQL:
FORWARD_ONLY
, SCROLL
, and DYNAMIC
. Specify the cursor type during cursor declaration.DECLARE forward_cursor CURSOR FOR SELECT column1 FROM my_table; DECLARE scroll_cursor CURSOR SCROLL FOR SELECT column1 FROM my_table; DECLARE dynamic_cursor CURSOR DYNAMIC FOR SELECT column1 FROM my_table;
Dynamic cursors in MySQL:
DECLARE dynamic_cursor CURSOR DYNAMIC FOR SELECT column1 FROM my_table WHERE condition; -- Change the condition dynamically SET @new_condition = 'new_condition'; SET @dynamic_sql = CONCAT('SELECT column1 FROM my_table WHERE ', @new_condition); PREPARE stmt FROM @dynamic_sql; OPEN dynamic_cursor;
MySQL stored procedures with cursors:
DELIMITER // CREATE PROCEDURE process_data() BEGIN DECLARE done INT DEFAULT FALSE; DECLARE var1, var2 INT; DECLARE my_cursor CURSOR FOR SELECT column1, column2 FROM my_table; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; OPEN my_cursor; my_loop: LOOP FETCH my_cursor INTO var1, var2; IF done THEN LEAVE my_loop; END IF; -- Process data here END LOOP; CLOSE my_cursor; END // DELIMITER ;