LAB 7
Transactions
Your grades table may not have the exact data (records) as the one used in this
activity. You goal 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 schools 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.
Start a transaction and modify the structure of the table grades. Try to “Rollback” this
transaction and show that this is not possible.
6.
Triggers
1. Create a copy of the “courses” table called “OldCourses” with the same structure but
without any data in it.
Create a trigger “after update on courses” that will record the old record from the
courses into the “OldCourses” table before the record has been updated.
i.e. “OldCourses” keeps a copy of the old records when they are updated in the
courses table.
2. Start a log file called trig.txt
3. Run queries to show that the trigger you created in step 1 actually works.
4. Run other queries to show that both the triggers on the student table and freshstudents
table actually work.
Users and Permissions
1. Create a log file for all the command line queries
2. Take a screen shot of each window showing the correct changes.
Create a number of new users by assigning name,host
and password.
Note the use of the wild card and single quotes in
the host and the single quotes in the password.
mysql> create user rhonda@'142%' identified by '1234';
Query OK, 0 rows affected (0.08 sec)
mysql> create user stan@'142%' identified by '1234';
Query OK, 0 rows affected (0.00 sec)
mysql> create user bob@'142%' identified by '1234';
Query OK, 0 rows affected (0.00 sec)
mysql> create user susan@'142%' identified by '1234';
Query OK, 0 rows affected (0.00 sec)
Now grant all global privileges to user
rhonda@’142%’
mysql> grant all on *.* to rhonda@'142%';
Query OK, 0 rows affected (0.06 sec)
Remember to flush privileges to load them to the
present session.
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
Now look at this user in SQLYOG
Now revoke all privileges from frogK@’142%’ and look
again at the GUI.
mysql> revoke all privileges on *.* from rhonda@'142%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
Now we look at granting permissions to a specific
database. This is more likely the case than the
previous global permissions.
mysql> grant select on subexamples.* to stan@'142%';
Query OK, 0 rows affected (0.44 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> grant update, insert on subexample.branches to stan@'142%';
Query OK, 0 rows affected (0.03 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)
We now revoke some of the privileges from
stan@’142%”
mysql> revoke update, insert on subexamples.branches from stan@'142%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
You can guess what the GUI would look like at this
point.
Finally we can delete the user completely.
Be careful of this command as it could be big
trouble if you delete the wrong user or users.
mysql> DELETE FROM mysql.user WHERE Host='142%' AND User='stan';
Query OK, 1 row affected (0.00 sec)
NOTE: We used the mysql database and the user table to delete the user.
is the table that stores all host names, users and passwords.
This
To change a password for a user.
First look at all of the passwords for all users.
Note that they are all encrypted.
mysql> select host,user,password from user;
mysql> use mysql;
Database changed
mysql> select host,user,password from user;
+------------+--------+-------------------------------------------+
| host
| user
| password
|
+------------+--------+-------------------------------------------+
| localhost | root
|
|
| 127.0.0.1 | root
|
|
| localhost |
|
|
| localhost | first |
|
| 142%
| rhonda | *A4B6157319038724E3560894F7F932C8886EBFCF |
| localhost | dumbo | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| localhost | paul
| *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| 142%
| bob
| *A4B6157319038724E3560894F7F932C8886EBFCF |
| localhost% | rhonda | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| 142%
| susan | *A4B6157319038724E3560894F7F932C8886EBFCF |
+------------+--------+-------------------------------------------+
10 rows in set (0.00 sec)
We will change the password for user ‘susan’
Now update the user table in the mysql database.
Be careful. This is an update and the wrong move
can be very upsetting for you.
mysql> UPDATE mysql.user SET Password= password('jjkkii45') WHERE User='susan';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host,user,password from user;
+------------+--------+-------------------------------------------+
| host
| user
| password
|
+------------+--------+-------------------------------------------+
| localhost | root
|
|
| 127.0.0.1 | root
|
|
| localhost |
|
|
| localhost | first |
|
| 142%
| rhonda | *A4B6157319038724E3560894F7F932C8886EBFCF |
| localhost | dumbo | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| localhost | paul
| *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| 142%
| bob
| *A4B6157319038724E3560894F7F932C8886EBFCF |
| localhost% | rhonda | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| 142%
| susan | *B5EF79F6AF51054A17579A07ED5560D35794156A |
+------------+--------+-------------------------------------------+
10 rows in set (0.00 sec)
Obviously changed but still encrypted…………
Views
The following information is located at this link.
http://www.w3schools.com/sql/sql_view.asp
What is a View?
In SQL, a VIEW is a virtual table based on the result-set of a SELECT statement.
A view contains rows and columns, just like a real table. The fields in a view are fields from one or more
real tables in the database. You can add SQL functions, WHERE, and JOIN statements to a view and present
the data as if the data were coming from a single table.
Note: The database design and structure will NOT be affected by the functions, where, or join statements
in a view.
Syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
Note: The database does not store the view data! The database engine recreates the data, using the view's
SELECT statement, every time a user queries a view
Using Views
A view could be used from inside a query, a stored procedure, or from inside another view. By adding
functions, joins, etc., to a view, it allows you to present exactly the data you want to the user.
What to Do.
Open a logfile called Lab_07_Views.txt and run queries to illustrate that your views work.
Use the command line to create the following views on your students courses and grades database. (you can
use the GUI to create it and then copy and paste it to the command line.)
The user ‘bob’ that you created in the last activity should be given permission to see these views and these
views only.
1. Create a view called “gradesonly” which shows only the following:
student first name
student last name
course codes
course description
grade for all students.
2. Create a view called “gradeswithHWD101” which shows only the following:
student first name
student last name
course codes
course description
grade
for all students and all the courses that they have taken but only if they have taken HWD101 and order them
by student last name.
3.
Create a view called “highestHWD101”
Which shows only the following:
Course code
Course description
Highest grade
4.
Create a view called “studenthighgradeHWD101”
Which shows only the following:
Student firstname
Student lastname
Course code
Grade
Run the appropriate Select statements to show these view tables.
Close your log file.
Add to your log file a screen shot of the permissions you gave to ‘bob’ in SQLyog.
© Copyright 2026 Paperzz