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


    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)
  • 相关阅读:
    75张图带你了解网络设备、网络地址规划、静态路由、实战演练
    37张图详解MAC地址、以太网、二层转发、VLAN
    用Python计算最长公共子序列和最长公共子串(转)
    python多线程为什么不能利用多核cpu
    python实现leetcode算法题库-maxLengthofRepeatedSubarray-最长公共子序列(718)
    python实现leetcode算法题库-twoSum-两数之和(1)
    python字符串与列表及字典的相互转化
    python sorted函数的使用
    python 2/3重定向输出文件
    elasticsearch查询时设置最大返回数 max_result_window | 限制字段总数超1000
  • 原文地址:https://www.cnblogs.com/tomatoes-/p/6024578.html
Copyright © 2020-2023  润新知