EMySQL Transaction
Using the EMySQL API – Part 10
Foreword: In this part, I explain how to code a transaction in MySQL.
By: Chrysanthus Date Published: 16 Aug 2016
Introduction
START TRANSACTION and COMMIT
In the following transaction code, a value is read from the pet table and placed in another row of the pet table.
START TRANSACTION;
SELECT @species := species FROM pet WHERE owner='John';
UPDATE pet SET species=@species WHERE owner='Diane';
COMMIT;
The transaction begins with the statement “START TRANSACTION;” and ends with the statement “COMMIT;”. In between, the SQL statement results are saved temporarily and not permanently. They are saved to disk permanently with the “COMMIT;” statement.
There are some statements that are always committed whether or not they are in a transaction. Typical of these are the Data Definition Language (DDL) statements that define or modify database objects. These are: ALTER DATABASE ... UPGRADE DATA DIRECTORY NAME, ALTER EVENT, ALTER PROCEDURE, ALTER SERVER, ALTER TABLE, ALTER VIEW, CREATE DATABASE, CREATE EVENT, CREATE INDEX, CREATE PROCEDURE, CREATE SERVER, CREATE TABLE, CREATE TRIGGER, CREATE VIEW, DROP DATABASE, DROP EVENT, DROP INDEX, DROP PROCEDURE, DROP SERVER, DROP TABLE, DROP TRIGGER, DROP VIEW, RENAME TABLE, TRUNCATE TABLE.
The above transaction can be coded as follows (after selecting the database):
var transSt = `START TRANSACTION;
SELECT @species := species FROM pet WHERE owner='John';
UPDATE pet SET species=@species WHERE owner='Diane';
COMMIT;`;
con.query(transSt, function(err, OK)
{
if (err)
console.log(err);
else
{
console.log(OK);
}
});
Thats is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
Free Pure ECMAScript MySQL API for Node.jsMore Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course
BACK NEXT