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

MySQL Session 13

This document provides an overview of creating, querying, and managing databases using MySQL, focusing on implementing triggers and transactions. It details the syntax and examples for creating various types of triggers, as well as the ACID properties of transactions and how to manage them in MySQL. Additionally, it outlines restrictions on triggers and the implications of using autocommit and DDL statements within transactions.

Uploaded by

osukashalom47
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

MySQL Session 13

This document provides an overview of creating, querying, and managing databases using MySQL, focusing on implementing triggers and transactions. It details the syntax and examples for creating various types of triggers, as well as the ACID properties of transactions and how to manage them in MySQL. Additionally, it outlines restrictions on triggers and the implications of using autocommit and DDL statements within transactions.

Uploaded by

osukashalom47
Copyright
© © All Rights Reserved
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 52

Creating, Querying, and Managing Databases Using

MySQL
Objectives

In this session, you will learn to:


Implement triggers
Implement transactions

Ver. 1.0 Slide 1 of 52


Creating, Querying, and Managing Databases Using
MySQL
Implementing Triggers

A trigger can be considered as a small program that is


pre-compiled and stored in the database.
It is automatically executed when a DML statement such as
UPDATE, DELETE, or INSERT is executed on the
corresponding table.

Ver. 1.0 Slide 2 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating a Trigger

The CREATE TRIGGER statement:


Is used to create triggers in a database.
Syntax:
CREATE [DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_body

Specifies
Specifies the
Contains trigger
when
the user
operation
the
statements
name account
trigger
performed
of the is
that
that
activated.
triggerneeds
are
toonexecuted
bethe
tocreated.
be
table
checked
for
that
each
activates
forrow
access
that
the
privileges
is
trigger.
affectedwhen
by the
the
triggering
trigger isevent.
activated.

Ver. 1.0 Slide 3 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating a Trigger (Contd.)

Depending on the time of activation of the trigger, it can be


classified as:
BEFORE trigger
AFTER trigger
Creating a BEFORE trigger:
A BEFORE trigger:
Is activated before the execution of the corresponding DML
statement.
Can be created for any of the INSERT, UPDATE, or DELETE event
types.

Ver. 1.0 Slide 4 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating a Trigger (Contd.)

Creating a BEFORE INSERT trigger:


A BEFORE INSERT trigger is executed before inserting each record in
the table associated with the trigger.
For example:
DELIMITER |
CREATE TRIGGER product_add BEFORE INSERT ON
Products
FOR EACH ROW
BEGIN
IF NEW.price< 0 THEN
SIGNAL SQLSTATE '45000'
SET message_text = 'Please insert positive value
for price';
END IF;
Checks and disallows insertion of records that have
END; negative value for the price column of the Products
| DELIMITER ; table and displays appropriate error message.

Ver. 1.0 Slide 5 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating a Trigger (Contd.)

Creating a BEFORE UPDATE trigger:


A BEFORE UPDATE trigger is executed before updating each record
in the table associated with the trigger.
For example:
CREATE TRIGGER check_order_quantity
BEFORE UPDATE ON Customer_Order
FOR EACH ROW
BEGIN
SELECT quantity_in_hand INTO @a from Products
WHERE product_id=new.product_id;
IF(new.quantity_ordered>@a)
THEN SIGNAL SQLSTATE '45000'
SET message_text = 'You cannot order more
products than in stock';
END IF;
END;
Checks and disallows updating of records that have order for
more quantity than what is in stock for the given product.

Ver. 1.0 Slide 6 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating a Trigger (Contd.)

Creating a BEFORE DELETE trigger:


A BEFORE DELETE trigger is executed before deletion of each
record from the table associated with the trigger.
For example:
CREATE TABLE Product_Audit(Product_ID
int(11),Audit_Action varchar(11));
Creates the product_Audit table.
CREATE TRIGGER product_delete BEFORE DELETE ON
Products
FOR EACH ROW
INSERT INTO Product_Audit VALUES(OLD.Product_ID,
'delete');
Creates
Adds a row
the to the product_audit
product_delete trigger
table
on containing
the Products
the table.
product_id and a ‘delete’ tag whenever a record is deleted
from the table.

Ver. 1.0 Slide 7 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating a Trigger (Contd.)

Creating an AFTER trigger:


An AFTER trigger:
Is activated after the execution of the corresponding DML
statement.
Can be created for any of the INSERT, UPDATE, or DELETE event
types.

Ver. 1.0 Slide 8 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating a Trigger (Contd.)

Creating an AFTER INSERT trigger:


An AFTER INSERT trigger is activated after the insertion of each
record in the associated table.
For example:
CREATE TRIGGER on_order
AFTER INSERT ON Customer_Order
FOR EACH ROW
UPDATE Products SET quantity_in_hand=
quantity_in_hand - NEW.quantity_ordered where
product_id=NEW.product_id;

Reduces the quantity on hand of a product, when an order is


made for it.

Ver. 1.0 Slide 9 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating a Trigger (Contd.)

Creating an AFTER UPDATE trigger:


An AFTER UPDATE trigger is activated after the update of each
record in the table associated with the trigger.
For example:
CREATE TRIGGER product_update AFTER UPDATE ON
Products
FOR EACH ROW
INSERT INTO Product_Audit VALUES(OLD.product_id,
'update');
Adds a row
Creates the containing the product_id
product_update trigger onand
thethe
Products tag to
update table.
the product_audit table whenever the details of a product
are updated.

Ver. 1.0 Slide 10 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating a Trigger (Contd.)

Creating an AFTER DELETE trigger:


An AFTER DELETE trigger is executed before deleting each record
from the table associated with the trigger.
For example:
CREATE TRIGGER order_del AFTER DELETE ON
Customer_Order
FOR EACH ROW
BEGIN
IF(OLD.customer_id in(SELECT customer_id FROM
Customers))
THEN
DELETE FROM Customers WHERE
customer_id=OLD.customer_id;
END IF;
END;
Deletes the customer details if the order placed by the customer
is deleted.

Ver. 1.0 Slide 11 of 52


Creating, Querying, and Managing Databases Using
MySQL
Restrictions on Triggers

There are certain restrictions on the type of statements


allowed to be used inside the trigger body in the existent
implementation of triggers in MySQL.
The following are the restrictions on the trigger
implementation in MySQL:
Any foreign key action does not activate triggers.
The CALL statement is not permitted inside the trigger body.
A TEMPORARY table or a view cannot be associated with a
trigger.
The RETURN statement is not permitted in triggers.
The mysql database does not permit any trigger creation on its
tables.

Ver. 1.0 Slide 12 of 52


Creating, Querying, and Managing Databases Using
MySQL
Restrictions on Triggers (Contd.)

Statements related to transactions such as COMMIT,


ROLLBACK, and START TRANSACTION cannot be executed
inside a trigger.
The trigger cache does not update itself dynamically if any
changes are made to the structure of a database object after
the information about its structure has been loaded in the
cache.

Ver. 1.0 Slide 13 of 52


Creating, Querying, and Managing Databases Using
MySQL
Referring to Old and New Values

Columns in the table associated with a trigger can be


referred by using the keywords, OLD and NEW.
OLD.col_name refers to a column of an existing row before
it is updated or deleted.
NEW.col_name refers to the column of a new row to be
inserted or an existing row after it is updated.
The triggering event determines whether the OLD or NEW
keywords are allowed in the corresponding trigger codes.

Ver. 1.0 Slide 14 of 52


Creating, Querying, and Managing Databases Using
MySQL
Referring to Old and New Values (Contd.)

In an INSERT trigger:
NEW.col_name indicates a column value to be inserted into a
new row.
OLD is not allowed, as the trigger operates upon each new row
inserted and not on the existing rows in the table.
In a DELETE trigger:
OLD.col_name indicates the column value to be deleted.
the use of the NEW keyword is not allowed.
In an UPDATE trigger:
OLD.col_name and NEW.col_name refer to the value of the
column in the row before and after the row is updated, respectively.

Ver. 1.0 Slide 15 of 52


Creating, Querying, and Managing Databases Using
MySQL
Dropping a Trigger

To delete a trigger, you can use the DROP TRIGGER


statement.
The DROP TRIGGER statement:
Syntax:
DROP TRIGGER [IF EXISTS]
[schema_name.]trigger_name
For example:
Is the
used
Specifies
Is tothe
name prevent
ofname an
of error condition,
the you
the trigger want to ifdrop.
database. the trigger does not
exist.
DROP TRIGGER [IF EXISTS]
production_management_system.on_order;
Drops the on_order trigger defined on the
production_management_system database.

Ver. 1.0 Slide 16 of 52


Creating, Querying, and Managing Databases Using
MySQL
Demo: Implementing Triggers

Problem Statement:
Consider the scenario of Rox Luxurious Hotels Ltd. The
management of the company needs to keep track of the bookings
made at the hotel on a daily basis. This is required to evaluate the
daily revenues. The database developer at Rox Luxurious Hotels
Ltd. needs to create a mechanism where the booking ID of every
booking is saved in another table called booking_info along with
the time and date of booking. Help the database developer in
accomplishing this task.

Ver. 1.0 Slide 17 of 52


Creating, Querying, and Managing Databases Using
MySQL
Demo: Implementing Triggers (Contd.)

Solution:
You need to create a trigger in MySQL to solve the preceding
problem. For this, you need to perform the following tasks:
Identify the table for the trigger, the trigger time, and the event.
Create the trigger on the identified table.
Verify the functionality of the created trigger.

Ver. 1.0 Slide 18 of 52


Creating, Querying, and Managing Databases Using
MySQL
Implementing Transactions

In MySQL, you can implement transactions to ensure data


integrity.
To prevent errors that could occur due to multiple
transactions accessing the same resource, you can use
locks.
Locks provide a mechanism to secure a resource until one
transaction is executed so that only one transaction can
work on a database resource at a time.

Ver. 1.0 Slide 19 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating Transactions

A transaction stands for a series of data manipulation


statements encapsulated as a single logical unit of work.
An RDBMS needs to provide four standard properties to
implement transactions.
These properties are usually referred to as the ACID
(Atomicity, Consistency, Isolation, and Durability)
properties.

Ver. 1.0 Slide 20 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating Transactions (Contd.)

The following are the ACID properties of a transaction:


Atomicity This states that either all the data-related
operations are performed or none of them is
Consistency This states that all the data is in a consistent
performed.
state after a transaction is completed
Isolation This determines whether or not any changes
successfully.
in data by an operation are made visible to a
Durability This states transaction.
concurrent that any change in data by a
completed transaction remains permanently in
effect in the system.

Ver. 1.0 Slide 21 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating Transactions (Contd.)

To fulfill the requirements of the ACID properties, MySQL


provides the following features:
Transaction management Ensures the atomicity and consistency of all
the transactions. If a transaction fails at a
Locking Preservespoint,
particular transaction durability
the MySQL andinvalidates
server isolation.
all the data modifications made since the
start of the transaction and the database
returns to its last stable state.

Ver. 1.0 Slide 22 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating Transactions (Contd.)

To support creation of a transaction, MySQL provides the


START TRANSACTION, COMMIT, and ROLLBACK
statements.
The START TRANSACTION statement begins the transaction.
The COMMIT statement commits changes to the database.
The ROLLBACK statement cancels any changes made by the
statements and returns the database to its last stable state.

Ver. 1.0 Slide 23 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating Transactions (Contd.)

The transaction process is depicted in the following figure.

Ver. 1.0 Slide 24 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating Transactions (Contd.)

Starting a transaction:
To start a transaction you need to use the following statement:
START TRANSACTION
Using the COMMIT statement:
Is used to terminate a transaction and to save all the changes
made by the transaction to the database.
For example:
START TRANSACTION; Inserts
Initiatesathe
record in the Customer table of the
transaction.
Production Management System database.
INSERT INTO Customers
VALUES(00000001,'Louie','Miller','Bin Day
23,4','Texas',9902345);
INSERT INTO Customer_Order
VALUES(000011,1,00000001,5);
COMMIT; Inserts
Submitsa the
record in the to
changes the database.
Customer _Order
table.

Ver. 1.0 Slide 25 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating Transactions (Contd.)

Using autocommit:
autocommit:
Is a session variable that must be set for each session.
Is set to 1, by default.
Can be disabled or enabled by using the following statement:
SET autocommit = {0 | 1}
Sets the value of autocommit to either 0 or 1.

Ver. 1.0 Slide 26 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating Transactions (Contd.)

The following categories of MySQL statements cause an


implicit commit:
The statements that define or modify objects in a database
such as ALTER, CREATE, and DROP statements
The statements that implicitly use or modify tables in the mysql
database such as the CREATE USER and the RENAME
USER statements
The statements that control transactions and perform locking
functions such as the BEGIN and the LOCK TABLES
statements
The LOAD DATA INFILE statement
The statements that handle administration such as ANALYZE
TABLE and CACHE INDEX statements

Ver. 1.0 Slide 27 of 52


Creating, Querying, and Managing Databases Using
MySQL
Creating Transactions (Contd.)

The DDL statements cannot be rolled back.


In case you include a DDL statement in the beginning of a
transaction and the statements following the DDL statement
fail; you cannot rollback the transaction entirely.

Ver. 1.0 Slide 28 of 52


Creating, Querying, and Managing Databases Using
MySQL
Just a minute

State whether TRUE or FALSE:


When autocommit mode is set to 1, rollback is enabled.

Answer:
False.

Ver. 1.0 Slide 29 of 52


Creating, Querying, and Managing Databases Using
MySQL
Reverting Transactions

The ROLLBACK statement rolls back an explicit or implicit


transaction to the beginning of the transaction or to a
save-point within a transaction.
A savepoint is used to divide a transaction into smaller
parts.
Savepoints allow you to discard parts of a long transaction
instead of rolling back the entire transaction.

Ver. 1.0 Slide 30 of 52


Creating, Querying, and Managing Databases Using
MySQL
Reverting Transactions (Contd.)

The ROLLBACK statement:


Rolls back an explicit or implicit transaction to the beginning of
the transaction or to a save-point within a transaction.
Syntax:
ROLLBACK [WORK] [AND [NO] CHAIN | [NO] RELEASE]
For
Is example:
Specifies
an that keyword
optional a
after
newthe
transaction
end identifies
that of thebegins
current
at transaction,
theofend
a block of in
code the
the
a current
server disconnects
transaction. from thefor
It is supported current
COMMITsession.
and ROLLBACK.
START TRANSACTION;
INSERT INTO Customers
values(00000002,'Andrew','Afoke','Bin Day -
23,4','Austin',9902376);
INSERT INTO Customer_Order
values(000012,1,00000002,5);
ROLLBACK; Is used to nullify the effect of the INSERT statements.

Ver. 1.0 Slide 31 of 52


Creating, Querying, and Managing Databases Using
MySQL
Reverting Transactions (Contd.)

Using savepoints:
The SAVEPOINT statement:
Rolls back only part of a transaction.
You can provide a name to a savepoint to mark a point in a
transaction upto which the statements can be rolled back.
Syntax:
SAVEPOINT savepoint_name;
For example:
START TRANSACTION;
INSERT INTO Customers
VALUES(00000003,'Mary','Christopher','Bin Day -
23,4','Texas',9902345);
INSERT INTO Customer_Order
VALUES(000013,1,00000003,5);
SAVEPOINT sav1;
Is used to define a savepoint after the INSERT statements.

Ver. 1.0 Slide 32 of 52


Creating, Querying, and Managing Databases Using
MySQL
Reverting Transactions (Contd.)

Multiple savepoints can be set within a transaction.


The transaction can roll back to a named savepoint.
To roll back to a given savepoint, you can use the following
statement:
ROLLBACK TO SAVEPOINT savepoint_name;
For example:
START TRANSACTION;
INSERT INTO Customers
VALUES(00000001,'Louie','Miller','Bin Day -
23,4','Texas ',9902345);
INSERT INTO Customer_Order
VALUES(000011,1,00000001,5);
SAVEPOINT sav1;

Defines a savepoint named sav1 within a transaction.

Ver. 1.0 Slide 33 of 52


Creating, Querying, and Managing Databases Using
MySQL
Reverting Transactions (Contd.)

INSERT INTO Customers


VALUES(00000002,'Ren','Dreamer', 'Green Street-
97,3','LA', 8802399);
INSERT INTO Customer_Order
VALUES(000012,1,00000002,3);
ROLLBACK TO SAVEPOINT sav1;
INSERT INTO Customers VALUES(00000002,'Jen',
'Dreamer','Green Street-97,3','LA',8802399);
INSERT INTO Customer_Order
VALUES(000012,1,00000002,3);
COMMIT;
Rolls back the transaction to the sav1 savepoint.

Ver. 1.0 Slide 34 of 52


Creating, Querying, and Managing Databases Using
MySQL
Setting Isolation Levels

Isolation levels determine how much isolated the


transactions are from each other.
MySQL supports the following isolation levels:
READ-UNCOMMITTED
READ-COMMITTED
REPEATABLE READ
SERIALIZABLE

Ver. 1.0 Slide 35 of 52


Creating, Querying, and Managing Databases Using
MySQL
Setting Isolation Levels (Contd.)

The READ-UNCOMMITTED isolation level:


Specifies that the transactions are able to see the data changes
made by other transactions that are not yet committed.
The READ-COMMITTED isolation level:
Specifies that the data changes are visible to other transactions
only when they are committed.
The REPEATABLE-READ isolation level:
Specifies that every read in the transaction will return the same set
of data.
Is the default for InnoDB.
The SERIALIZABLE isolation level:
Specifies that transactions place locks on all accessed records and
the resource.

Ver. 1.0 Slide 36 of 52


Creating, Querying, and Managing Databases Using
MySQL
Setting Isolation Levels (Contd.)

Setting an isolation level:


Syntax:
SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL
{
READ UNCOMMITTED
| READ COMMITTED
| REPEATABLE READ
| SERIALIZABLE
}
ForSpecify
example:
Specifies
the
that
level
theofdefault
isolation.
transaction level is set for
globally.
the current
SETsession.
SESSION TRANSACTION ISOLATION LEVEL REPEATABLE
READ;

Ver. 1.0 Slide 37 of 52


Creating, Querying, and Managing Databases Using
MySQL
Just a minute

Which is the default isolation level for InnoDb?

Answer:
Repeatable Read

Ver. 1.0 Slide 38 of 52


Creating, Querying, and Managing Databases Using
MySQL
Locking Tables Manually

You can achieve higher performance of your system and


prevent data manipulation by multiple programs or users by
locking your tables.
Locking tables for writing means that no other user or
programs can read or write from the same until you unlock
it.
Locking tables may sometimes lead to deadlocks.

Ver. 1.0 Slide 39 of 52


Creating, Querying, and Managing Databases Using
MySQL
Locking Tables Manually (Contd.)

You can specify the following types of locks manually:


READ lock
WRITE lock
READ lock:
Provide consistent read by blocking any writes to that resource.
are also called shared locks.
Is granted in the following way:
If there is no WRITE lock set on the resource, the READ lock is
granted immediately.
If there is a WRITE lock on the resource, the lock is put in the
READ lock queue.

Ver. 1.0 Slide 40 of 52


Creating, Querying, and Managing Databases Using
MySQL
Locking Tables Manually (Contd.)

WRITE lock:
Can be applied on a resource that needs to be modified.
Is granted in the following way:
If there is no (READ/WRITE) lock on the resource, the WRITE lock
is granted immediately.
If there is a READ/WRITE lock on the resource, the WRITE lock is
put in the WRITE lock queue.
Priority of read and write lock:
MySQL grants the lock in the following way:
If there is a request waiting in the WRITE lock queue, then the lock
is granted to it.
If there is no lock request for the resource in the WRITE lock
queue, then grant the lock to the first request in the READ lock
queue.

Ver. 1.0 Slide 41 of 52


Creating, Querying, and Managing Databases Using
MySQL
Locking Tables Manually (Contd.)

Locking in InnoDB:
InnoDB uses multiversioning.
When locks are necessary, InnoDB uses row-level locking.
A deadlock for accessing table data can occur between
transactions.
Priority of read and write lock:
MySQL grants the lock in the following way:
If there is a request waiting in the write lock queue, then the lock is
granted to it.
If there is no lock request for the resource in the write lock queue,
then grant the lock to the first request in the read lock queue.

Ver. 1.0 Slide 42 of 52


Creating, Querying, and Managing Databases Using
MySQL
Locking Tables Manually (Contd.)

Locking non-transactional tables:


You need to use the LOCK TABLES statement to lock non-
transactional tables.
After updating the tables, they must be unhooked using the
UNLOCK TABLES statement.

Ver. 1.0 Slide 43 of 52


Creating, Querying, and Managing Databases Using
MySQL
Locking Tables Manually (Contd.)

Using the LOCK TABLE statement:


Syntax:
LOCK {TABLE | TABLES}
<table name> [AS <alias>] {READ [LOCAL] |
[LOW_PRIORITY] WRITE}
[{, <table name> [AS <alias>] {READ [LOCAL] |
[LOW_PRIORITY] WRITE}}...]
Specifies whether
an
that
alternate
For example: any
the
only
all current
itconnection
is name
the oneconnection
orfor
current
connections more
can
cantheperform
execute
tables.
table.
is waiting
connection tworead
can
the for the
access
INSERT the table
WRITE
operation on
statements
for
LOCK
the andand
table other
without
performing connections
the
no any or
read conflict.
are
thecan
connection allowed
write to obtain
operation.
perform the READ locks.
write operation on
LOCK
the table. TABLE PRODUCTS READ;
Places a read lock on the Products table.

Ver. 1.0 Slide 44 of 52


Creating, Querying, and Managing Databases Using
MySQL
Locking Tables Manually (Contd.)

Using the UNLOCK TABLE statement:


Syntax:
UNLOCK {TABLE | TABLES}
For example:
UNLOCK TABLE Products;
Removes the lock from the locked Products table.

Flash Presentation: Locking

Ver. 1.0 Slide 45 of 52


Creating, Querying, and Managing Databases Using
MySQL
Demo: Implementing Transactions

Problem Statement:
Consider the scenario of Rox Luxurious Hotels Ltd. The
management of the company is receiving constant complaints
against a reservation agent and wants to dishonor all his bookings
with an immediate effect. For fulfilling the given requirement, the
relevant records of the agent must be removed from the bookings,
agent_reservation, and the agent table. The database developer
needs to create a procedure that accepts the agent_id and
removes the corresponding records from the tables. Help the
database developer in accomplishing this task.

Ver. 1.0 Slide 46 of 52


Creating, Querying, and Managing Databases Using
MySQL
Demo: Implementing Transactions (Contd.)

Solution:
You need to create a transaction in MySQL to solve the preceding
problem. For this, you need to perform the following tasks:
Identify the tables to be involved in the transaction.
Perform the transaction.
Verify the output of the transaction.

Ver. 1.0 Slide 47 of 52


Creating, Querying, and Managing Databases Using
MySQL
Summary

In this session, you learned that:


A trigger can be considered as a small program that is
pre-compiled and stored in the database.
You can use the CREATE TRIGGER statement to create
triggers in a database.
Depending upon the time of activation of the trigger, it can be
classified as:
BEFORE trigger
AFTER trigger
A BEFORE trigger is activated before the execution of the
corresponding DML statement.
A BEFORE INSERT trigger is activated before adding rows in
the table associated with the trigger.

Ver. 1.0 Slide 48 of 52


Creating, Querying, and Managing Databases Using
MySQL
Summary (Contd.)

A BEFORE UPDATE trigger is executed before updating each


record in the table associated with the trigger.
A BEFORE DELETE trigger is executed before deletion of each
record from the table associated with the trigger.
An AFTER trigger is activated after the execution of the
associated DML statement.
An AFTER INSERT trigger is activated after the insertion of each
record in the table associated with the trigger.
An AFTER UPDATE trigger is activated after the update of each
record in the table associated with the trigger.
An AFTER DELETE trigger is executed before deleting each
record from the table associated with the trigger.
Triggers serve a great purpose in maintaining the integrity of the
database and improving its performance.
In an INSERT trigger, NEW.col_name indicates a column value
to be inserted into a new row.

Ver. 1.0 Slide 49 of 52


Creating, Querying, and Managing Databases Using
MySQL
Summary (Contd.)

In a DELETE trigger, OLD.col_name indicates the column


value to be deleted.
In an UPDATE trigger, OLD.col_name and NEW.col_name
refer to the value of the column in the row before and after the
row is updated, respectively.
To delete a trigger, you can use the DROP TRIGGER statement.
A transaction stands for a series of data manipulation
statements encapsulated as a single logical unit of work.
In an RDBMS, whenever an operation is performed, it can be
treated as a transaction if it has ACID properties.
To fulfill the requirements of the ACID properties, MySQL
provides the following features:
Transaction management
Locking

Ver. 1.0 Slide 50 of 52


Creating, Querying, and Managing Databases Using
MySQL
Summary (Contd.)

To support creation of a transaction, MySQL provides the


START TRANSACTION, COMMIT, and ROLLBACK statements.
The START TRANSACTION statement begins the transaction.
The COMMIT statement commits changes to the database.
The ROLLBACK statement cancels any change made by the
statements and returns the database to its last stable state.
The DDL statements cannot be rolled back.
The ROLLBACK statement rolls back an explicit or implicit
transaction to the beginning of the transaction or to a savepoint
within a transaction.
A savepoint is used to divide a transaction into smaller parts.

Ver. 1.0 Slide 51 of 52


Creating, Querying, and Managing Databases Using
MySQL
Summary (Contd.)

MySQL supports the following isolation levels:


READ-UNCOMMITTED
READ-COMMITTED
REPEATABLE READ
SERIALIZABLE
You can specify the following types of locks manually:
READ lock
WRITE lock
You need to use the LOCK TABLE statement to lock non
transactional tables.
The UNLOCK TABLES statement unlocks all the tables that were
locked in the session, if no table name was specified.

Ver. 1.0 Slide 52 of 52

You might also like