SQL 3
SQL 3
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 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 $$
DELIMITER $$
BEFORE DELETE
BEGIN
END$$
DELIMITER ;
6. After Delete Trigger:
As the name implies, it is a trigger which is implemented after a delete statement is invoked.
DELIMITER $$
AFTER DELETE
BEGIN
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.
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.