Activity 8-2

advertisement
ACTIVITY 8-2
Your grades table may not have the exact data (records) as the one used in this
activity. You goal in this activity is to understand the concept of transactions and
see the difference in your grades table when using transactions, “commit”,
“rollback” ectc.
 To start a transaction type “begin work;”
You could also type “begin” or “start transaction”.
Any one of the three will work.
mysql> begin work;
Query OK, 0 rows affected (0.00 sec)

Then run any insert, update or delete command which
changes data but does not change structure.
mysql> update grades set gradeletter = 'F';
Query OK, 40 rows affected (0.00 sec)
Rows matched: 53 Changed: 40 Warnings: 0
mysql> select * from grades;
+--------+----------+--------+-------+-----------+-------------+
| Code
| Semester | StuNum | grade | RecordNum | gradeletter |
+--------+----------+--------+-------+-----------+-------------+
| DAT702 | 043
| 12345 | 85.0 |
1 | F
|
| OPS400 | 043
| 12345 | 75.0 |
2 | F
|
| INT213 | 051
| 12345 | 35.0 |
3 | F
|
| OPS400 | 043
| 12346 | 65.0 |
4 | F
|
| OPS440 | 051
| 12346 | 65.0 |
5 | F
|
| OPS400 | 052
| 12346 | 85.0 |
6 | F
|
| OPS440 | 042
| 21435 | 65.0 |
7 | F
|
| OPS400 | 051
| 21435 | 35.0 |
8 | F
|
| INT213 | 052
| 21435 | 65.0 |
9 | F
|
| HWD101 | 062
| 22222 | 95.0 |
10 | F
|
And more!!!!!
 Make a decision now to “commit” or “rollback”
If you “rollback” then any change to data will be
reversed.
mysql> rollback;
Query OK, 0 rows affected (0.06 sec)
 Rollback will reverse the change in data.
mysql> select * from grades;
+--------+----------+--------+-------+-----------+-------------+
| Code
| Semester | StuNum | grade | RecordNum | gradeletter |
+--------+----------+--------+-------+-----------+-------------+
| DAT702 | 043
| 12345 | 85.0 |
1 | A
|
| OPS400 | 043
| 12345 | 75.0 |
2 | B+
|
| INT213 | 051
| 12345 | 35.0 |
3 | F
|
| OPS400 | 043
| 12346 | 65.0 |
4 | C+
|
| OPS440 | 051
| 12346 | 65.0 |
5 | C+
|
| OPS400 | 052
| 12346 | 85.0 |
6 | A
|
| OPS440 | 042
| 21435 | 65.0 |
7 | C+
|
| OPS400 | 051
| 21435 | 35.0 |
8 | F
|
| INT213 | 052
| 21435 | 65.0 |
9 | C+
|
| HWD101 | 062
| 22222 | 95.0 |
10 | A+
|
| OPS400 | 053
| 98765 | 78.0 |
11 | B+
|
| DAT702 | 053
| 97546 | 35.0 |
12 | F
|
| DAT702 | 061
| 97546 | 65.0 |
13 | C+
|
| DAT702 | 033
| 21435 | 69.0 |
15 | C+
|
| int213 | 993
| 12345 | 85.0 |
16 | A
|
| OPS400 | 991
| 12346 | 75.0 |
17 | B+
|
| Hwd101 | 043
| 55555 | 45.0 |
18 | F
|
And more!!!!!
 What happens when you change structure??
mysql> desc grades;
+-------------+--------------+------+-----+---------+----------------+
| Field
| Type
| Null | Key | Default | Extra
|
+-------------+--------------+------+-----+---------+----------------+
| Code
| varchar(50) | YES | MUL | NULL
|
|
| Semester
| varchar(50) | YES |
| NULL
|
|
| StuNum
| varchar(50) | YES | MUL | NULL
|
|
| grade
| decimal(3,1) | YES |
| NULL
|
|
| RecordNum
| int(11)
| NO
| PRI | NULL
| auto_increment |
| gradeletter | char(2)
| YES |
| F
|
|
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.02 sec)
 Start another transaction
mysql> begin work;
Query OK, 0 rows affected (0.00 sec)
mysql> alter table grades modify column grade decimal(5,1);
Query OK, 53 rows affected (0.33 sec)
Records: 53 Duplicates: 0 Warnings: 0
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
 Rollback will not reverse a change in structure.
You are stuck with it!!
mysql> desc grades;
+-------------+--------------+------+-----+---------+----------------+
| Field
| Type
| Null | Key | Default | Extra
|
+-------------+--------------+------+-----+---------+----------------+
| Code
| varchar(50) | YES | MUL | NULL
|
|
| Semester
| varchar(50) | YES |
| NULL
|
|
| StuNum
| varchar(50) | YES | MUL | NULL
|
|
| grade
| decimal(5,1) | YES |
| NULL
|
|
| RecordNum
| int(11)
| NO
| PRI | NULL
| auto_increment |
| gradeletter | char(2)
| YES |
| F
|
|
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
 “Rollback” will reverse a change in data. “Commit”
will accept and save the changes in data that you
have made during the transaction.
mysql> begin work;
Query OK, 0 rows affected (0.00 sec)
mysql> update grades set grade = (grade)*(1.1) where code <> 'HWD101';
Query OK, 46 rows affected, 5 warnings (0.00 sec)
Rows matched: 46 Changed: 46 Warnings: 5
mysql> select * from grades;
+--------+----------+--------+-------+-----------+-------------+
| Code
| Semester | StuNum | grade | RecordNum | gradeletter |
+--------+----------+--------+-------+-----------+-------------+
| DAT702 | 043
| 12345 | 93.5 |
1 | A
|
| OPS400 | 043
| 12345 | 82.5 |
2 | B+
|
| INT213 | 051
| 12345 | 38.5 |
3 | F
|
| OPS400 | 043
| 12346 | 71.5 |
4 | C+
|
| OPS440 | 051
| 12346 | 71.5 |
5 | C+
|
| OPS400 | 052
| 12346 | 93.5 |
6 | A
|
| OPS440 | 042
| 21435 | 71.5 |
7 | C+
|
| OPS400 | 051
| 21435 | 38.5 |
8 | F
|
| INT213 | 052
| 21435 | 71.5 |
9 | C+
|
| HWD101 | 062
| 22222 | 95.0 |
10 | A+
|
And more!!!!!
mysql> rollback;
Query OK, 0 rows affected (0.03 sec)
mysql> select * from grades;
+--------+----------+--------+-------+-----------+-------------+
| Code
| Semester | StuNum | grade | RecordNum | gradeletter |
+--------+----------+--------+-------+-----------+-------------+
| DAT702 | 043
| 12345 | 85.0 |
1 | A
|
| OPS400 | 043
| 12345 | 75.0 |
2 | B+
|
| INT213 | 051
| 12345 | 35.0 |
3 | F
|
| OPS400 | 043
| 12346 | 65.0 |
4 | C+
|
| OPS440 | 051
| 12346 | 65.0 |
5 | C+
|
| OPS400 | 052
| 12346 | 85.0 |
6 | A
|
| OPS440 | 042
| 21435 | 65.0 |
7 | C+
|
| OPS400 | 051
| 21435 | 35.0 |
8 | F
|
| INT213 | 052
| 21435 | 65.0 |
9 | C+
|
| HWD101 | 062
| 22222 | 95.0 |
10 | A+
|
| OPS400 | 053
| 98765 | 78.0 |
11 | B+
|
| DAT702 | 053
| 97546 | 35.0 |
12 | F
|
| DAT702 | 061
| 97546 | 65.0 |
13 | C+
|
| DAT702 | 033
| 21435 | 69.0 |
15 | C+
|
| int213 | 993
| 12345 | 85.0 |
16 | A
|
| OPS400 | 991
| 12346 | 75.0 |
17 | B+
|
| Hwd101 | 043
| 55555 | 45.0 |
18 | F
|
And more!!!!!
mysql> begin work;
Query OK, 0 rows affected (0.00 sec)
mysql> update grades set grade = (grade)*(1.1) where code <> 'HWD101';
Query OK, 46 rows affected, 5 warnings (0.02 sec)
Rows matched: 46 Changed: 46 Warnings: 5
mysql> select * from grades;
+--------+----------+--------+-------+-----------+-------------+
| Code
| Semester | StuNum | grade | RecordNum | gradeletter |
+--------+----------+--------+-------+-----------+-------------+
| DAT702 | 043
| 12345 | 93.5 |
1 | A
|
| OPS400 | 043
| 12345 | 82.5 |
2 | B+
|
| INT213 | 051
| 12345 | 38.5 |
3 | F
|
| OPS400 | 043
| 12346 | 71.5 |
4 | C+
|
| OPS440 | 051
| 12346 | 71.5 |
5 | C+
|
| OPS400 | 052
| 12346 | 93.5 |
6 | A
|
| OPS440 | 042
| 21435 | 71.5 |
7 | C+
|
| OPS400 | 051
| 21435 | 38.5 |
8 | F
|
| INT213 | 052
| 21435 | 71.5 |
9 | C+
|
| HWD101 | 062
| 22222 | 95.0 |
10 | A+
|
| OPS400 | 053
| 98765 | 85.8 |
11 | B+
|
| DAT702 | 053
| 97546 | 38.5 |
12 | F
|
| DAT702 | 061
| 97546 | 71.5 |
13 | C+
|
| DAT702 | 033
| 21435 | 75.9 |
15 | C+
|
| int213 | 993
| 12345 | 93.5 |
16 | A
|
| OPS400 | 991
| 12346 | 82.5 |
17 | B+
|
| Hwd101 | 043
| 55555 | 45.0 |
18 | F
|
And more!!!!!
mysql> commit;
Query OK, 0 rows affected (0.03 sec)
mysql> select * from grades;
+--------+----------+--------+-------+-----------+-------------+
| Code
| Semester | StuNum | grade | RecordNum | gradeletter |
+--------+----------+--------+-------+-----------+-------------+
| DAT702 | 043
| 12345 | 93.5 |
1 | A
|
| OPS400 | 043
| 12345 | 82.5 |
2 | B+
|
| INT213 | 051
| 12345 | 38.5 |
3 | F
|
| OPS400 | 043
| 12346 | 71.5 |
4 | C+
|
| OPS440 | 051
| 12346 | 71.5 |
5 | C+
|
| OPS400 | 052
| 12346 | 93.5 |
6 | A
|
| OPS440 | 042
| 21435 | 71.5 |
7 | C+
|
| OPS400 | 051
| 21435 | 38.5 |
8 | F
|
| INT213 | 052
| 21435 | 71.5 |
9 | C+
|
| HWD101 | 062
| 22222 | 95.0 |
10 | A+
|
| OPS400 | 053
| 98765 | 85.8 |
11 | B+
|
| DAT702 | 053
| 97546 | 38.5 |
12 | F
|
| DAT702 | 061
| 97546 | 71.5 |
13 | C+
|
| DAT702 | 033
| 21435 | 75.9 |
15 | C+
|
| int213 | 993
| 12345 | 93.5 |
16 | A
|
| OPS400 | 991
| 12346 | 82.5 |
17 | B+
|
| Hwd101 | 043
| 55555 | 45.0 |
18 | F
|
And more!!!!!
To be submitted:
1. Using the Lab 7 database (courses, grades, students) create a database called ‘trans’
2. Start a log file called transactiontest.txt.
3. Start a transaction.

Delete all the data from the grades table where the code is HWD101 or OPS400.

Show that the data is gone by running a select query.

Rollback the transaction.

Show that the data is returned by running a select query.
5. Start a transaction.

Delete all the data from the grades table where the code is HWD101 or OPS400.

Commit to this transaction

Show that the data is gone by running a select query.
6.
Start a transaction and modify the structure of the table grades. Try to “Rollback” this
transaction and show that this is not possible.
7.
Attach your log file to an e-mail and submit as ACT 8-2
Download