DB Mangmnt Examples
DB Mangmnt Examples
6. ABC Markets sell products to customers. The relational diagram shown in Figure P10.6 represents the main
entities for ABC’s database.
FIGURE P10.6 The ABC Markets Relational Diagram
Note: Not all entities and attributes are represented in this example. Use only the attributes indicated.
Using this database, write the SQL code to represent each one of the following transactions. Use BEGIN
TRANSACTION and COMMIT to group the SQL statements in logical transactions.
Chapter 10 Exercises
a. On May 11, 2012, customer ‘10010’ makes a credit purchase (30 days) of one unit of product ‘11QER/31’
with a unit price of $110.00; the tax rate is 8 percent. The invoice number is 10983, and this invoice has
only one product line.
a. BEGIN TRANSACTION
b. INSERT INTO INVOICE
i. VALUES (10983, ‘10010’, ‘11-May-2012’, 118.80, ‘30’, ‘OPEN’);
c. INSERT INTO LINE
i. VALUES (10983, 1, ‘11QER/31’, 1, 110.00);
d. UPDATE PRODUCT
i. SET P_QTYOH = P_QTYOH – 1
ii. WHERE P_CODE = ‘11QER/31’;
e. UPDATE CUSTOMER
f. SET CUS_DATELSTPUR = ‘11-May-2012’, CUS_BALANCE = CUS_BALANCE +118.80
g. WHERE CUS_CODE = ‘10010’;
h. COMMIT;
b. On June 3, 2012, customer ‘10010’ makes a payment of $100 in cash. The payment ID is 3428.
a. BEGIN TRANSACTION
b. INSERT INTO PAYMENTS
VALUES (3428, ‘03-Jun-2012’, ‘10010’, 100.00, ‘CASH’, 'None');
UPDATE CUSTOMER;
SET CUS_DATELSTPMT = ‘03-Jun-2012’, CUS_BALANCE = CUS_BALANCE -100.00
WHERE CUS_CODE = ‘10010’;
COMMIT
7. Create a simple transaction log (using the format shown in Table 10.13) to represent the actions of the two
previous transactions.
Table P10.7 The ABC Markets Transaction Log
TRL TRX PREV NEXT BEFORE AFTER
ID NUM PTR PTR OPERATION TABLE ROW ID ATTRIBUTE VALUE VALUE
987 101 Null 1023 START * Start Trx.
1023 101 987 1026 INSERT INVOICE 10983 10983, 10010,
11-May-2012,
118.80, 30, OPEN
1026 101 1023 1029 INSERT LINE 10983, 1 10983, 1,
11QER/31, 1,
110.00
1029 101 1026 1031 UPDATE PRODUCT 11QER/31 P_QTYOH 47 46
1031 101 1029 1032 UPDATE CUSTOMER 10010 CUS_BALANCE 345.67 464.47
1032 101 1031 1034 UPDATE CUSTOMER 10010 CUS_DATELSTPUR 5-May-2010 11-May-2012
1034 101 1032 Null COMMIT * End Trx. *
1089 102 Null 1091 START * Start Trx.
1091 102 1089 1095 INSERT PAYMENT 3428 3428, 3-Jun-2012,
10010, 100.00,
CASH, None
1095 102 1091 1096 UPDATE CUSTOMER 10010 CUS_BALANCE 464.47 364.47
1096 102 1095 1097 UPDATE CUSTOMER 10010 CUS_DATELSTPMT 2-May-2010 3-Jun-2012
1097 102 1096 Null COMMIT * End Trx.
Note: Because we have not shown the table contents, the "before" values in the transaction can be assumed. The "after" value must be
computed using the assumed "before" value, plus or minus the transaction value. Also, in order to save some space, we have combined
the "after" values for the INSERT statements into a single cell. Actually, each value could be entered in individual rows.
Chapter 10 Exercises
8. Assuming that pessimistic locking is being used, but the two-phase locking protocol is not, create a
chronological list of the locking, unlocking, and data manipulation activities that would occur during
the complete processing of the transaction described in Problem 6a.
Time Action
1 Lock INVOICE
2 Insert row 10983 into INVOICE
3 Unlock INVOICE
4 Lock LINE
5 Insert into row 10983, 1 into LINE
6 Unlock LINE
7 Lock PRODUCT
8 Update PRODUCT 11QER/31, P_QTYOH from 47 to 46
9 Unlock PRODUCT
10 Lock CUSTOMER
11 Update CUSTOMER 10010, CUS_BALANCE from 345.67 to 464.47
12 Update CUSTOMER 10010, CUS_DATELSTPUR from 05-May-2010 to 11-May-2012
13 Unlock CUSTOMER
9. Assuming that pessimistic locking with the two-phase locking protocol is being used, create a
chronological list of the locking, unlocking, and data manipulation activities that would occur during
the complete processing of the transaction described in Problem 6a.
Time Action
1 Lock INVOICE
2 Lock LINE
3 Lock PRODUCT
4 Lock CUSTOMER
5 Insert row 10983 into INVOICE
6 Insert into row 10983, 1 into LINE
7 Update PRODUCT 11QER/31, P_QTYOH from 47 to 46
8 Update CUSTOMER 10010, CUS_BALANCE from 345.67 to 464.47
9 Update CUSTOMER 10010, CUS_DATELSTPUR from 05-May-2010 to 11-May-2012
10 Unlock INVOICE
11 Unlock LINE
12 Unlock PRODUCT
13 Unlock CUSTOMER
Chapter 10 Exercises
10. Assuming that pessimistic locking is being used, but the two-phase locking protocol is not, create a
chronological list of the locking, unlocking, and data manipulation activities that would occur during
the complete processing of the transaction described in Problem 6b.
Time Action
1 Lock PAYMENT
2 Insert row 3428 into PAYMENT
3 Unlock PAYMENT
4 Lock CUSTOMER
5 Update CUSTOMER 10010, CUS_BALANCE from 464.47 to 364.47
6 Update CUSTOMER 10010, CUS_DATELSTPMT from 02-May-2010 to 03-Jun-2012
7 Unlock CUSTOMER
11. Assuming that pessimistic locking with the two-phase locking protocol is being used, create a
chronological list of the locking, unlocking, and data manipulation activities that would occur during
the complete processing of the transaction described in Problem 6b.
Time Action
1 Lock PAYMENT
2 Lock CUSTOMER
3 Insert row 3428 into PAYMENT
4 Update CUSTOMER 10010, CUS_BALANCE from 464.47 to 364.47
5 Update CUSTOMER 10010, CUS_DATELSTPMT from 02-May-2010 to 03-Jun-2012
6 Unlock PAYMENT
7 Unlock CUSTOMER
Chapter 10 Exercises
Additional Problems and Answers
1. Write the SQL statements that might be used in transaction management and explain how they work.
Comment
BEGIN TRANSACTION Start transaction
In SQL, the transaction begins automatically with the first SQL statement, or the user can start with the
BEGIN TRANSACTION statement. The SQL transaction ends when
• the last SQL statement is found and/or the program ends
• the user cancels the transaction
• COMMIT or ROLLBACK statements are found
The DBMS will ensure that all SQL statements are executed and completed before committing all work. If,
for any reason, one or more of the SQL statements in the transaction cannot be completed, the entire
transaction is aborted and the database is rolled back to its previous consistent state.
Chapter 10 Exercises
2. Starting with a consistent database state, trace the activities that are required to execute a set of
transactions to produce an updated consistent database state.
The following example traces the execution of problem 1's credit sale transaction. We will assume that the
transaction is based on a currently consistent database state. We will also assume that the transaction is the
only transaction being executed by the DBMS.
The following transaction log traces the database transaction explained in problem 1.