Cursor + Trigger in MySQL

In this tutorial, we will demonstrate:

Cursor: Processing rows with a condition.

Trigger: Automatically inserting audit logs after an insert operation.

We’ll use simple examples with screenshots from an online MySQL editor.

Cursor Examp…


This content originally appeared on DEV Community and was authored by ATHEEBA PARVEEN J A 24CB002

In this tutorial, we will demonstrate:

  • Cursor: Processing rows with a condition.
  • Trigger: Automatically inserting audit logs after an insert operation.

We’ll use simple examples with screenshots from an online MySQL editor.

Cursor Example: Employees with Salary > 50,000

Step 1: Create Employee table & Insert sample data

CREATE TABLE Employee (
    emp_id INT PRIMARY KEY,
    emp_name VARCHAR(50),
    salary INT
);

INSERT INTO Employee VALUES
(1, 'Alice', 40000),
(2, 'Bob', 55000),
(3, 'Charlie', 70000),
(4, 'David', 45000);

Step 2: Use a Cursor with Condition

DELIMITER //

CREATE PROCEDURE GetHighSalaryEmployees()
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE empName VARCHAR(50);
    DECLARE cur CURSOR FOR SELECT emp_name FROM Employee WHERE salary > 50000;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cur;

    read_loop: LOOP
        FETCH cur INTO empName;
        IF done THEN
            LEAVE read_loop;
        END IF;
        SELECT empName AS High_Salary_Employee;
    END LOOP;

    CLOSE cur;
END//
DELIMITER ;

Step 3: Call the Procedure

CALL GetHighSalaryEmployees();

cursor

Trigger Example: Student Registration Audit

Step 1: Create Student & Audit tables

CREATE TABLE Students (
    student_id INT PRIMARY KEY,
    name VARCHAR(50)
);

CREATE TABLE Student_Audit (
    audit_id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT,
    name VARCHAR(50),
    registered_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Step 2: Create AFTER INSERT Trigger

DELIMITER //

CREATE TRIGGER after_student_insert
AFTER INSERT ON Students
FOR EACH ROW
BEGIN
    INSERT INTO Student_Audit (student_id, name)
    VALUES (NEW.student_id, NEW.name);
END //

DELIMITER ;

Step 3: Insert Data into Students

INSERT INTO Students VALUES (1, 'John Doe');
INSERT INTO Students VALUES (2, 'Jane Smith');

Step 4: Check Audit Table

SELECT * FROM Student_Audit;

trigger

CONCLUSION

  • Cursor helps process row-by-row results under certain conditions.
  • Trigger automates actions (like auditing) whenever an event occurs in a table.

Both are powerful features in SQL for automation and data integrity.


This content originally appeared on DEV Community and was authored by ATHEEBA PARVEEN J A 24CB002


Print Share Comment Cite Upload Translate Updates
APA

ATHEEBA PARVEEN J A 24CB002 | Sciencx (2025-10-04T17:00:24+00:00) Cursor + Trigger in MySQL. Retrieved from https://www.scien.cx/2025/10/04/cursor-trigger-in-mysql/

MLA
" » Cursor + Trigger in MySQL." ATHEEBA PARVEEN J A 24CB002 | Sciencx - Saturday October 4, 2025, https://www.scien.cx/2025/10/04/cursor-trigger-in-mysql/
HARVARD
ATHEEBA PARVEEN J A 24CB002 | Sciencx Saturday October 4, 2025 » Cursor + Trigger in MySQL., viewed ,<https://www.scien.cx/2025/10/04/cursor-trigger-in-mysql/>
VANCOUVER
ATHEEBA PARVEEN J A 24CB002 | Sciencx - » Cursor + Trigger in MySQL. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/04/cursor-trigger-in-mysql/
CHICAGO
" » Cursor + Trigger in MySQL." ATHEEBA PARVEEN J A 24CB002 | Sciencx - Accessed . https://www.scien.cx/2025/10/04/cursor-trigger-in-mysql/
IEEE
" » Cursor + Trigger in MySQL." ATHEEBA PARVEEN J A 24CB002 | Sciencx [Online]. Available: https://www.scien.cx/2025/10/04/cursor-trigger-in-mysql/. [Accessed: ]
rf:citation
» Cursor + Trigger in MySQL | ATHEEBA PARVEEN J A 24CB002 | Sciencx | https://www.scien.cx/2025/10/04/cursor-trigger-in-mysql/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.