0% found this document useful (0 votes)
10 views

dbms_lab

The document outlines the creation of triggers and cursors in MySQL, detailing their definitions, syntax, and implementation steps. Triggers are automatically invoked during specific events on a table, while cursors allow for row-by-row processing of query results. Example implementations for both triggers and cursors are provided, demonstrating their usage in a database context.
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)
10 views

dbms_lab

The document outlines the creation of triggers and cursors in MySQL, detailing their definitions, syntax, and implementation steps. Triggers are automatically invoked during specific events on a table, while cursors allow for row-by-row processing of query results. Example implementations for both triggers and cursors are provided, demonstrating their usage in a database context.
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/ 4

EXPERIMENT – 7

Objective – Creating triggers in mysql.

Triggers in MySQL are special types of stored procedures that are automatically invoked (or fired)
when a certain event occurs on a table, such as INSERT, UPDATE, or DELETE. These events can
be defined to occur either befor e or after the event takes place.

Here’s how you can create triggers in MySQL:

Gener al Syntax for Cr eating a Tr igger

sql code
CREATE TRIGGER trigger_name
trigger_time trigger_event
ON table_name FOR EACH ROW
trigger_body;

 trigger_name: The name of the trigger.


 trigger_time: When the trigger should fire. It can be either BEFORE or AFTER.
 trigger_event: The event that causes the trigger to fire. It can be one of INSERT, UPDATE,
or DELETE.
 table_name: The table the trigger is associated with.
 trigger_body: The set of actions (SQL statements) to be performed when the trigger fires.

Implementation

use glbajaj336;
create table main(id int,salary int);
create table backup(id int,salary int);
insert into main values(110,10000);
insert into main values(119,1866);
delimiter //
create trigger t1 before delete on main for each row
begin
insert into backup (id,salary) values(old.id,old.salary);
end //
delimiter ;

set sql_safe_updates=0;
delete from main where id=110;
SELECT * FROM Glbajaj336.main;
SELECT * FROM Glbajaj336.backup;
Output befor e apply tr igger

Output after apply tr igger

main

backup
EXPERIMENT – 8

Objective – Creating cursors in mysql.

A cur sor in MySQL is a database object that allows you to retrieve rows from a result set one at a
time and process them row by row. Cursors are typically used when you need to iterate over the
result of a query and perform an action for each row, such as updating or inserting data based on the
query results.

Cursors are often used in stor ed pr ocedur es or stor ed functions in MySQL, and they are
particularly useful when you need to perform operations that require row-by-row processing.

Here’s the general flow for creating and using a cursor in MySQL:

1. Declar e the Cur sor : Define the SQL query whose result you want to iterate over.
2. Open the Cur sor : Open the cursor to execute the query.
3. Fetch Data: Retrieve each row from the cursor, one at a time.
4. Close the Cur sor : Close the cursor after finishing the processing.

Basic Syntax for Declar ing and Using a Cur sor

DECLARE cursor_name CURSOR FOR select_statement;

Implementation

use Glbajaj336;
create table coustomer(id int,cname varchar(20));
insert into coustomer values(110,'yash');
insert into coustomer values(115,'uttu');

call processcursors();

cr eate stor ed pr ocedur es

CREATE DEFINER=`root`@`localhost` PROCEDURE `processcursors`()


BEGIN
declare done int default 0;
declare cid int;
declare ename varchar(225);

declare cur1 cursor for


select id,cname from coustomer;

declare continue handler for not found set done = 1;

open cur1;

read_loop: loop
fetch cur1 into cid,ename;
if done then
leave read_loop;
end if;

-- process each row here


select cid,ename;
end loop;
close cur1;

END

Output

You might also like