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

Aranas 03-Hands-On Activity 2

Uploaded by

fr.rommel.canda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Aranas 03-Hands-On Activity 2

Uploaded by

fr.rommel.canda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Christian Aranas

BSIT- 501

1. Create a database name Currency Control and create a two (2) tables name
Accounts and Transac_History and put the following data.

Step 1: Create the Database


CREATE DATABASE ConcurrencyControl;
USE ConcurrencyControl;

Step 2: Create the Accounts Table

CREATE TABLE Accounts (


A_ID INT PRIMARY KEY,
Name VARCHAR(50),
Balance DECIMAL(10, 2)
);

Step 3: Insert Data into the Accounts Table

INSERT INTO Accounts (A_ID, Name, Balance)


VALUES
(1, 'Thor', 8000.00),
(2, 'Hulk', 4000.00),
(3, 'Thanos', 12000.00),
(4, 'Loki', 3000.00),
(5, 'Stark', 15000.00);

Step 4: Create the Transac_History Table


CREATE TABLE Transac_History (
T_ID INT,
Date DATE,
Amount DECIMAL(10, 2),
A_ID INT,
FOREIGN KEY (A_ID) REFERENCES Accounts(A_ID)
);

Step 5: Insert Data into the Transac_History Table


INSERT INTO Transac_History (T_ID, Date, Amount,
A_ID) VALUES
(1, '2020-09-02', -1500.00, 3),
(2, '2020-09-03', 3000.00, 5),
(2, '2020-09-03', -3000.00, 1);
2. Mr. and Mrs. Stark share an access to an account named Stark. On August 21, 2020
Mr. Stark wants to withdraw an amount of ₱1,000 to his account. While Mr. Stark is
working on his transaction, Mrs. Stark start a new transaction that will view the
balance of their account. She sees the balance as ₱14,000. Stark on the other hand,
did not confirm his transaction and the system automatically aborted his requested
transaction. Using READ UNCOMMITTED, write queries that will demonstrate this
concurrent transaction.

Step 1: Start the transaction for Mr. Stark (Withdraw ₱1,000)

BEGIN TRANSACTION;

-- Mr. Stark withdraws ₱1,000 from the account


UPDATE Accounts
SET Balance = Balance - 1000
WHERE A_ID = 5;

-- Check balance after withdrawal but before


committing
SELECT * FROM Accounts WHERE A_ID = 5;

Step 2: Use READ UNCOMMITTED for Mrs. Stark’s Transaction

SET TRANSACTION ISOLATION LEVEL READ


UNCOMMITTED;

-- Mrs. Stark checks the balance


SELECT * FROM Accounts WHERE A_ID = 5;

Step 3: Rollback Mr. Stark’s Transaction

ROLLBACK TRANSACTION;
OUTPUT 1

OUTPUT 2
3. On August 28, 2020, the database administrator creates a new transaction on the Accounts
table. Still, he wants to obtain a lock that will prevent any other transaction from modifying
any rows from it while he was working with his transaction. But he wants other transactions to
be able to insert new entries. Meanwhile, an application programmer inserts a new entry in
Accounts table: T_ID: 6, Name: Clint, Balance: ₱19,000. At the same time, a sophisticated user
wants to modify the balance of Thor by deducting a ₱500. Using the REPEATABLE READ
command, write queries that will demonstrate these three (3) concurrent transaction.
Step 1: Database Administrator Starts the Transaction

SET TRANSACTION ISOLATION LEVEL REPEATABLE


READ;
BEGIN TRANSACTION;

-- Admin checks the Accounts table


SELECT A_ID, Name, Balance
FROM Accounts;

Step 2: Application Programmer Inserts a New Account


INSERT INTO Accounts (A_ID, Name, Balance)
VALUES (6, 'Clint', 19000.00);

-- Check Accounts to confirm insertion


SELECT A_ID, Name, Balance
FROM Accounts;

Step 3: Sophisticated User Modifies Thor’s Balance

-- Attempt to update Thor's balance


UPDATE Accounts
SET Balance = Balance - 500
WHERE A_ID = 1;

Step 4: Admin Commits the Transaction

-- Admin commits the transaction


COMMIT;

-- Check the updated Accounts table


SELECT A_ID, Name, Balance

Step 5: Updating the Transac_History Table

-- Insert Clint into Accounts table


INSERT INTO Accounts (A_ID, Name, Balance) VALUES (6, 'Clint', 19000.00);
-- Now insert the transactions
INSERT INTO Transac_History (T_ID, Date, Amount, A_ID) VALUES
(4, '2020-08-28', -500.00, 1), -- Sophisticated user's change to Thor's balance
(5, '2020-08-28', 19000.00, 6); -- Clint's new entry by the programmer
OUTPUT 1

Output 2
Output 3

Output 4
4. On the same day, the database administrator creates a new transaction again by adding up
a ₱1000 balance to all rows in the Accounts table. This time, he wants his transaction to
obtain an exclusive lock. While the database administrator is working with his transaction, a
sophisticated user named Joe wants to add a new entry: T_ID: 7, Name: Natasha, Balance:
₱9,000. Using SERIALIZABLE command, write queries that will demonstrate these two (2)
concurrent transactions.
(Note: You can use while loop in order to update all the rows with a specific value.)

Step 1: Database Administrator Transaction (Using SERIALIZABLE)

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;


BEGIN TRANSACTION;

-- Loop through the Accounts table and update the


balances by adding 1000
UPDATE Accounts
SET Balance = Balance + 1000;

-- Check the updated balances


SELECT A_ID, Name, Balance
FROM Accounts;

-- Keep the transaction open to simulate ongoing work

Step 2: Joe Tries to Insert a New Entry

-- Joe's attempt to insert a new account


INSERT INTO Accounts (A_ID, Name, Balance) VALUES (7,
'Natasha', 9000.00);

-- Joe tries to see the updated Accounts table


SELECT A_ID, Name, Balance
FROM Accounts;

Step 3: Commit the Admin's Transaction

COMMIT;
Output 1

Output 2

You might also like