【练习】使用事务和锁定语句

1.在mysql客户机会话中,检查当前隔离级别。

2.使用PROMPT语句,将上一步打开的mysql会话中提示符改为t1,以便将其与后面的客户机会话区别开来。

3.在t1 mysql会话中启动一个新事务。

4.在t1会话中,选择city表中的ID>4070的所有行。

5.在另一个终端窗口中打开第二个mysql会话,将该mysql会话中的提示符更改为t2

6.在t2会话中,启动一个新事务,选择city表中的ID>4070的所有行。

7.在t2会话中,向city表中插入新的一行,确认新行已经加入。

1.

mysql> select @@global.tx_isolation;
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| REPEATABLE-READ       |
+-----------------------+
1 row in set (0.00 sec)

2.

mysql> prompt t1>;
PROMPT set to ‘t1>‘
t1>start transaction;
Query OK, 0 rows affected (0.00 sec)

3.

t1>use world
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

4.

t1>select * from city where id >4070;
+------+--------------+-------------+------------+------------+
| ID   | Name         | CountryCode | District   | Population |
+------+--------------+-------------+------------+------------+
| 4071 | Mount Darwin | ZWE         | Harare     |     164362 |
| 4072 | Mutare       | ZWE         | Manicaland |     131367 |
| 4073 | Gweru        | ZWE         | Midlands   |     128037 |
| 4074 | Gaza         | PSE         | Gaza       |     353632 |
| 4075 | Khan Yunis   | PSE         | Khan Yunis |     123175 |
| 4076 | Hebron       | PSE         | Hebron     |     119401 |
| 4077 | Jabaliya     | PSE         | North Gaza |     113901 |
| 4078 | Nablus       | PSE         | Nablus     |     100231 |
| 4079 | Rafah        | PSE         | Rafah      |      92020 |
+------+--------------+-------------+------------+------------+
9 rows in set (0.00 sec)

5.

t1>prompt t2>;
PROMPT set to ‘t2>‘
t2>
t2>start transaction;
Query OK, 0 rows affected (0.00 sec)

6.

t2>use world;
Database changed
t2>select * from city where id >4070;
+------+--------------+-------------+------------+------------+
| ID   | Name         | CountryCode | District   | Population |
+------+--------------+-------------+------------+------------+
| 4071 | Mount Darwin | ZWE         | Harare     |     164362 |
| 4072 | Mutare       | ZWE         | Manicaland |     131367 |
| 4073 | Gweru        | ZWE         | Midlands   |     128037 |
| 4074 | Gaza         | PSE         | Gaza       |     353632 |
| 4075 | Khan Yunis   | PSE         | Khan Yunis |     123175 |
| 4076 | Hebron       | PSE         | Hebron     |     119401 |
| 4077 | Jabaliya     | PSE         | North Gaza |     113901 |
| 4078 | Nablus       | PSE         | Nablus     |     100231 |
| 4079 | Rafah        | PSE         | Rafah      |      92020 |
+------+--------------+-------------+------------+------------+
9 rows in set (0.00 sec)

7.

t2>insert into city (name,countrycode) values (‘new city‘,‘ata‘);
Query OK, 1 row affected (0.00 sec)

t2>select * from city where id >4070;
+------+--------------+-------------+------------+------------+
| ID   | Name         | CountryCode | District   | Population |
+------+--------------+-------------+------------+------------+
| 4071 | Mount Darwin | ZWE         | Harare     |     164362 |
| 4072 | Mutare       | ZWE         | Manicaland |     131367 |
| 4073 | Gweru        | ZWE         | Midlands   |     128037 |
| 4074 | Gaza         | PSE         | Gaza       |     353632 |
| 4075 | Khan Yunis   | PSE         | Khan Yunis |     123175 |
| 4076 | Hebron       | PSE         | Hebron     |     119401 |
| 4077 | Jabaliya     | PSE         | North Gaza |     113901 |
| 4078 | Nablus       | PSE         | Nablus     |     100231 |
| 4079 | Rafah        | PSE         | Rafah      |      92020 |
| 4080 | new city     | ata         |            |          0 |
+------+--------------+-------------+------------+------------+
10 rows in set (0.00 sec)

8.

t2>prompt t1>;
PROMPT set to ‘t1>‘
t1>start transaction;
Query OK, 0 rows affected (0.01 sec)

t1>commit;
Query OK, 0 rows affected (0.00 sec)

9.

t1>select * from city where id >4070;
+------+--------------+-------------+------------+------------+
| ID   | Name         | CountryCode | District   | Population |
+------+--------------+-------------+------------+------------+
| 4071 | Mount Darwin | ZWE         | Harare     |     164362 |
| 4072 | Mutare       | ZWE         | Manicaland |     131367 |
| 4073 | Gweru        | ZWE         | Midlands   |     128037 |
| 4074 | Gaza         | PSE         | Gaza       |     353632 |
| 4075 | Khan Yunis   | PSE         | Khan Yunis |     123175 |
| 4076 | Hebron       | PSE         | Hebron     |     119401 |
| 4077 | Jabaliya     | PSE         | North Gaza |     113901 |
| 4078 | Nablus       | PSE         | Nablus     |     100231 |
| 4079 | Rafah        | PSE         | Rafah      |      92020 |
| 4080 | new city     | ata         |            |          0 |
+------+--------------+-------------+------------+------------+
10 rows in set (0.01 sec)

10.

t1>start transaction;
Query OK, 0 rows affected (0.00 sec)

t1>delete from city where id =4080;
Query OK, 1 row affected (0.00 sec)

t1>select * from city where id >4070;
+------+--------------+-------------+------------+------------+
| ID   | Name         | CountryCode | District   | Population |
+------+--------------+-------------+------------+------------+
| 4071 | Mount Darwin | ZWE         | Harare     |     164362 |
| 4072 | Mutare       | ZWE         | Manicaland |     131367 |
| 4073 | Gweru        | ZWE         | Midlands   |     128037 |
| 4074 | Gaza         | PSE         | Gaza       |     353632 |
| 4075 | Khan Yunis   | PSE         | Khan Yunis |     123175 |
| 4076 | Hebron       | PSE         | Hebron     |     119401 |
| 4077 | Jabaliya     | PSE         | North Gaza |     113901 |
| 4078 | Nablus       | PSE         | Nablus     |     100231 |
| 4079 | Rafah        | PSE         | Rafah      |      92020 |
+------+--------------+-------------+------------+------------+
9 rows in set (0.00 sec)
时间: 2024-10-14 22:15:54

【练习】使用事务和锁定语句的相关文章

事务与锁定

DML(DATA MANIPULATION LANGUAGE):INSERT,UPDATE,DELETE,MERGEDDL(DATA DEFINITION LANGUAGE):CREATE,ALTER,DROPDCL(DATA CONTROL LANGUAGE):授于或回收访问数据库的某种特权 一个事务开始于一条执行的SQL语句,结束于:    1.用户提交事务    2.用记回滚事务    3.遇到DDL语句    4.遇到DCL语句    5.用户结束会话    6.数据库关闭或系统崩溃显式

SQL2005中的事务与锁定(三)- 转载

------------------------------------------------------------------------ -- Author : HappyFlyStone  -- Date   : 2009-10-03 15:30:00 -- Version: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86)  --      Apr 14 2006 01:12:25  --           Copyright

SQL2005中的事务与锁定(二)-转载

------------------------------------------------------------------------ -- Author : HappyFlyStone  -- Date   : 2009-09-27 21:36:30 -- Version: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86)  --       Apr 14 2006 01:12:25  --       Copyright (c

深入浅出Mysql——事务控制和锁定语句

Mysql支持对MyISAM进行表级锁定,对InnoDB存储引擎支持行级锁定. LOCK TABLES可以锁定用于当前线程的表,如果表被其他线程锁定,则当前线程会等待,直到可以获取所有锁定为止. UNLOCK TBALES可以释放当前线程获得的任何锁定,当前线程执行另一个LOCK TABLES时,或当与服务器的连接被关闭时,所有由当前线程锁定的表被隐含的解锁 START TRANSACTION或BEGIN语句可以开始一项新的事务 COMMIT和ROLLBACK用来提交或者回滚事务 CHAIN和R

MySQL 事物控制和锁定语句

一.MySQL支持对MyISAM和MEMORY存储引擎的表进行表级锁定,对InnoDB存储引擎的表进行行集锁定.默认情况下是自动获得. 二.LOCK TABLES 可以用于锁定当前线程获得的表,如果表被其他线程锁定,当前线程一直等待到可以获取现有锁定为止. 三.UNLOCK TABLES 可以释放当前线程获得的任何锁定,当前线程执行另一个LOCK TABLES时,或当与服务器的连接被关闭时,所有由当前线程锁定的表被隐式地解锁. 四.start transaction 可以开启一个事务且commi

SQL2005中的事务与锁定(五)- 转载

------------------------------------------------------------------------ -- Author : HappyFlyStone -- Date   : 2009-10-05 14:00:00 -- Version: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86) --      Apr 14 2006 01:12:25 --           Copyright (c

SQL2005中的事务与锁定(八)- 转载

------------------------------------------------------------------------ -- Author : happyflystone -- Date   : 2009-10-26 -- Version: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86) --      Apr 14 2006 01:12:25 --      Copyright (c) 1988-2005 Mi

SQL2005中的事务与锁定(六) - 转载

------------------------------------------------------------------------ -- Author : HappyFlyStone -- Date   : 2009-10-12 ――2009-10-17 -- Version: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86) --      Apr 14 2006 01:12:25 --           Copyrigh

SqlServer批量刷数据执行事务回滚语句备份

企业进行对数据库执行刷数据工作,一段很长的语句希望同时成功或者失败时用到. 1.建立测试环境 /************************************************************ * Code formatted by SoftTree SQL Assistant ?v6.5.278 * Time: 2016/9/29 21:33:55 ************************************************************/