MySQL Session 13
MySQL Session 13
MySQL
Objectives
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.
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.
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.
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.
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.
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.
Answer:
False.
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.
Answer:
Repeatable Read
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.
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.
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.
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.