Guest User

Untitled

a guest
Apr 8th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. % mysql --user=a --password=hoge --prompt='m1> ' test
  2.  
  3. m1> create table test(
  4. id INT(11) AUTO_INCREMENT NOT NULL PRIMARY KEY,
  5. name varchar(32) not null unique
  6. );
  7. Query OK, 0 rows affected (0.01 sec)
  8.  
  9. m1> start transaction;
  10. Query OK, 0 rows affected (0.00 sec)
  11.  
  12. -- トランザクションの分離レベル確認
  13. m1> SELECT @@tx_isolation;
  14. +-----------------+
  15. | @@tx_isolation |
  16. +-----------------+
  17. | REPEATABLE-READ |
  18. +-----------------+
  19. 1 row in set (0.01 sec)
  20.  
  21. % mysql --user=a --password=hoge --prompt='m2> ' test
  22.  
  23. m2> start transaction;
  24. Query OK, 0 rows affected (0.00 sec)
  25.  
  26. -- トランザクションの分離レベル確認
  27. m1> SELECT @@tx_isolation;
  28. +-----------------+
  29. | @@tx_isolation |
  30. +-----------------+
  31. | REPEATABLE-READ |
  32. +-----------------+
  33. 1 row in set (0.00 sec)
  34.  
  35. m1> insert into test (name) values ("aa");
  36. Query OK, 1 row affected (0.01 sec)
  37.  
  38. -- m1のトランザクションが終るまで待たされる
  39. m2> insert into test (name) values ("aa");
  40.  
  41. m1> commit;
  42. Query OK, 0 rows affected (0.01 sec)
  43.  
  44. -- エラーが返ってくる
  45. ERROR 1062 (23000): Duplicate entry 'aa' for key 'name'
  46.  
  47. m2> rollback;
  48. Query OK, 0 rows affected (0.00 sec)
  49.  
  50. m1> start transaction;
  51. Query OK, 0 rows affected (0.00 sec)
  52.  
  53. m2> start transaction;
  54. Query OK, 0 rows affected (0.00 sec)
  55.  
  56. m1> insert into test (name) values ("b");
  57. Query OK, 1 row affected (0.00 sec)
  58.  
  59. m2> insert into test (name) values ("c");
  60. Query OK, 1 row affected (0.00 sec)
  61.  
  62. m1> select * from test;
  63. +----+------+
  64. | id | name |
  65. +----+------+
  66. | 1 | aa |
  67. | 3 | b |
  68. +----+------+
  69. 2 rows in set (0.00 sec)
  70.  
  71. m2> select * from test;
  72. +----+------+
  73. | id | name |
  74. +----+------+
  75. | 1 | aa |
  76. | 4 | c |
  77. +----+------+
  78. 2 rows in set (0.00 sec)
  79.  
  80. m1> commit;
  81. Query OK, 0 rows affected (0.01 sec)
  82.  
  83. m1> select * from test;
  84. +----+------+
  85. | id | name |
  86. +----+------+
  87. | 1 | aa |
  88. | 3 | b |
  89. +----+------+
  90. 2 rows in set (0.00 sec)
  91.  
  92. m2> commit;
  93. Query OK, 0 rows affected (0.01 sec)
  94.  
  95. -- 両方コミットしたら、両方がコミットしたデータが見える
  96. m1> select * from test;
  97. +----+------+
  98. | id | name |
  99. +----+------+
  100. | 1 | aa |
  101. | 3 | b |
  102. | 4 | c |
  103. +----+------+
  104. 3 rows in set (0.00 sec)
  105.  
  106. m2> select * from test;
  107. +----+------+
  108. | id | name |
  109. +----+------+
  110. | 1 | aa |
  111. | 3 | b |
  112. | 4 | c |
  113. +----+------+
  114. 3 rows in set (0.00 sec)
  115.  
  116. test=> set PROMPT1 'p1> '
  117.  
  118. -- トランザクションの分離レベル確認
  119. p1> SHOW TRANSACTION ISOLATION LEVEL;
  120. transaction_isolation
  121. -----------------------
  122. read committed
  123. (1 row)
  124.  
  125. p1> create table test(
  126. id serial NOT NULL PRIMARY KEY,
  127. name char(32) not null unique
  128. );
  129.  
  130. p1> BEGIN;
  131. BEGIN
  132.  
  133. test=> set PROMPT1 'p2> '
  134.  
  135. -- トランザクションの分離レベル確認
  136. p2> SHOW TRANSACTION ISOLATION LEVEL;
  137. transaction_isolation
  138. -----------------------
  139. read committed
  140. (1 row)
  141.  
  142. p2> BEGIN;
  143. BEGIN
  144.  
  145. p1> insert into test (name) values ('aa');
  146. INSERT 0 1
  147.  
  148. -- p1 のトランザクションが終るまで待たされる
  149. p2> insert into test (name) values ("aa");
  150.  
  151. p1> commit;
  152. Query OK, 0 rows affected (0.01 sec)
  153.  
  154. -- エラーが返ってくる
  155. ERROR: duplicate key value violates unique constraint "test_name_key"
  156. DETAIL: Key (name)=(aa ) already exists.
  157.  
  158. p2> ABORT;
  159. ROLLBACK
  160.  
  161. p1> select * from test;
  162. id | name
  163. ----+----------------------------------
  164. 2 | aa
  165. (1 row)
  166.  
  167. p2> select * from test;
  168. id | name
  169. ----+----------------------------------
  170. 2 | aa
  171. (1 row)
  172.  
  173. p1> BEGIN;
  174. BEGIN
  175.  
  176. p2> BEGIN;
  177. BEGIN
  178.  
  179. p1> insert into test (name) values ('b');
  180. INSERT 0 1
  181.  
  182. p2> insert into test (name) values ('c');
  183. INSERT 0 1
  184.  
  185. p1> select * from test;
  186. id | name
  187. ----+----------------------------------
  188. 2 | aa
  189. 4 | b
  190.  
  191. p2> select * from test;
  192. id | name
  193. ----+----------------------------------
  194. 2 | aa
  195. 5 | c
  196. (2 rows)
  197.  
  198. p1> COMMIT;
  199. COMMIT
  200. p1> select * from test;
  201. id | name
  202. ----+----------------------------------
  203. 2 | aa
  204. 4 | b
  205. (2 rows)
  206.  
  207. p2> COMMIT;
  208. COMMIT
  209. p2> select * from test;
  210. id | name
  211. ----+----------------------------------
  212. 2 | aa
  213. 4 | b
  214. 5 | c
  215. (3 rows)
  216.  
  217. p1> select * from test;
  218. id | name
  219. ----+----------------------------------
  220. 2 | aa
  221. 4 | b
  222. 5 | c
  223. (3 rows)
  224.  
  225. INSERT INTO ... SELECT
Add Comment
Please, Sign In to add comment