You are on page 1of 15

Triggers

A MySQL trigger is a stored program (with queries) which is executed automatically to respond to a specific event
such as insertion, updation or deletion occurring in a table.

❖ Trigger Structure:

● Trigger Name: A unique name to identify the trigger.

● Trigger Event: The specific event that triggers the execution of the trigger, such as INSERT, UPDATE, DELETE,
or a DDL statement.

● Trigger Timing: Specifies whether the trigger should be fired before the event (BEFORE) or after the event
(AFTER).

● Trigger Body: The code or SQL statements that define the actions to be performed when the trigger is fired.
1. Before Insert Trigger:
As the name implies, it is a trigger which is implemented before an insert is invoked.
If we write an insert statement, then the actions of the trigger will be performed before the insert is implemented.
2. After Insert Trigger:
As the name implies, it is a trigger which is implemented
after an insert is invoked.
3. After Update Trigger:
As the name implies, it is a trigger which is implemented after an
update is invoked.
4. Before Insert Trigger:
As the name implies, it is a trigger which is implemented before
an update is invoked.
DELIMITER $$
CREATE TRIGGER Updated_Sales_Data
BEFORE UPDATE
ON InitialSales
FOR EACH ROW
BEGIN
IF OLD.qty<>new.qty
THEN
INSERT INTO SalesUpdates(sales_Id, InitialQuantity, UpdatedQuantity)
VALUES(old.prodId, old.qty, new.qty);
END IF;
END $$

UPDATE InitialSales SET qty = 1000 WHERE prodId = 1; $$


View Result:
5. Before Delete Trigger:
As the name implies, it is a trigger which is implemented before delete statement is invoked.

DELIMITER $$

CREATE TRIGGER before_delete_salaries

BEFORE DELETE

ON salaries FOR EACH ROW

BEGIN

INSERT INTO salary_archives (emp_num, valid_from,


DELETE FROM salaries WHERE emp_num = 105; amount)

VALUES(OLD. emp_num, OLD.valid_from,


OLD.amount);

END$$

DELIMITER ;
6. After Delete Trigger:
As the name implies, it is a trigger which is implemented after a delete statement is invoked.

DELIMITER $$

CREATE TRIGGER after_delete_salaries

AFTER DELETE

ON salaries FOR EACH ROW

BEGIN

UPDATE total_salary_budget SET total_budget = total_budget -


old.amount;

END$$

DELIMITER ;
Indexes
● An index is a data structure that allows us to add indexes in the existing table.
● It enables you to improve the faster retrieval of records on a database table.
● It creates an entry for each value of the indexed columns.
● We use it to quickly find the record without searching each row in a database table whenever the table is accessed.
● We can create an index by using one or more columns of the table for efficient access to the records.
Cursors

● A cursor allows to iterate a set of rows returned by a query & process them accordingly.
● We create cursors inside a stored procedure to handle the result set returned by a query.
● Mysql cursors are read-only and non-scrollable.

Steps for working with Cursors:


1. Declare <cursore_name> for select statement
2. Open <cursor_name>
3. Fetch cursor_name into variable list to retrieve the next row pointed by the cursor & move the
cursor to the next row in the result set.
4. Close <cursor name> to deactivate the cursor and release the memory associated with it.
Acid Properties

In order to maintain consistency in a database, before and after the transaction, certain properties are followed. These are called
ACID properties.
1. Atomicity:

By this, we mean that either the entire transaction takes place at once or doesn’t happen at all.

If the transaction fails after completion of T1 but before completion of T2.( say, after write(X) but before
write(Y)), then the amount has been deducted from X but not added to Y. This results in an inconsistent
database state. Therefore, the transaction must be executed in its entirety in order to ensure the correctness
of the database state.
2.) Consistency:

This means that integrity constraints must be maintained so that the database is consistent before and after
the transaction. It refers to the correctness of a database. Referring to the example above,
The total amount before and after the transaction must be maintained.
Total before T occurs = 500 + 200 = 700.
Total after T occurs = 400 + 300 = 700.
Therefore, the database is consistent. Inconsistency occurs in case T1 completes but T2 fails. As a result,
T is incomplete.
3.) Isolation:

This property ensures that multiple transactions can occur concurrently without leading to the inconsistency of the
database state. Transactions occur independently without interference. Changes occurring in a particular transaction
will not be visible to any other transaction until that particular change in that transaction is written to memory or has
been committed.

Suppose T has been executed till Read (Y) and then T’’ starts. As a result, interleaving of operations takes place due to which
T’’ reads the correct value of X but the incorrect value of Y and sum computed by
T’’: (X+Y = 50, 000+500=50, 500)
is thus not consistent with the sum at end of the transaction:
T: (X+Y = 50, 000 + 450 = 50, 450).
This results in database inconsistency, due to a loss of 50 units. Hence, transactions must take place in isolation and changes
should be visible only after they have been made to the main memory.
4.) Durability:

This property ensures that once the transaction has completed execution, the updates and modifications
to the database are stored in and written to disk and they persist even if a system failure occurs. These
updates now become permanent and are stored in non-volatile memory. The effects of the transaction,
thus, are never lost.

You might also like