Question: Please explain how the account table, the transaction table, the savepoint command, the rollback command, and the commit command are working together: CREATE TABLE account

Please explainhow the account table, the transaction table, the savepoint command, the rollback command, and the commit command are working together:
CREATE TABLE account (
account_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
avail_balance DECIMAL(10,2) NOT NULL,
last_activity_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY(account_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- account_id avail_balance last_activity_date
-- txn_id txn_date account_id txn_type_cd amount
CREATE TABLE transaction (
txn_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
txn_date DATETIME NOT NULL,
account_id SMALLINT UNSIGNED,
txn_type_cd VARCHAR(2) NOT NULL,
amount DECIMAL(10,2) NOT NULL,
PRIMARY KEY(txn_id),
KEY idx_fk_account_id (account_id),
CONSTRAINT fk_account_txn FOREIGN KEY (account_id) REFERENCES account (account_id) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO account
(account_id, avail_balance, last_activity_date)
VALUES (789,2000,"2020-07-1020:53:27");
INSERT INTO account
(account_id, avail_balance, last_activity_date)
VALUES (123,1000,"2019-07-1020:53:27");
SET AUTOCOMMIT=0;
# try savepoint *************************
START TRANSACTION;
SAVEPOINT before_update_transactions;
INSERT INTO transaction
(txn_date, account_id, txn_type_cd, amount)
VALUES
(now(),123,'D',55);
INSERT INTO transaction
(txn_date, account_id, txn_type_cd, amount)
VALUES
(now(),789,'C',55);
SAVEPOINT before_update_accounts;
UPDATE account
SET avail_balance = avail_balance -55,
last_activity_date = now()
WHERE account_id =123;
UPDATE account
SET avail_balance = avail_balance +55,
last_activity_date = now()
WHERE account_id =789;
SAVEPOINT after_update_accounts;
-- ROLLBACK TO SAVEPOINT before_update_transactions;
-- ROLLBACK TO SAVEPOINT before_update_accounts;
ROLLBACK TO SAVEPOINT after_update_accounts;
COMMIT;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!