You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

143 lines
4.5 KiB

  1. -- PART A
  2. CREATE TABLE accounts (id SERIAL, name VARCHAR(50), credit numeric, currency VARCHAR(10));
  3. INSERT INTO accounts (name, credit, currency) VALUES('Account 1', 1000, 'RUB');
  4. INSERT INTO accounts (name, credit, currency) VALUES('Account 2', 1000, 'RUB');
  5. INSERT INTO accounts (name, credit, currency) VALUES('Account 3', 1000, 'RUB');
  6. BEGIN;
  7. -- T1
  8. UPDATE accounts SET credit=credit-500 WHERE name='Account 1';
  9. UPDATE accounts SET credit=credit+500 WHERE name='Account 3';
  10. SAVEPOINT T1;
  11. -- T2
  12. UPDATE accounts SET credit=credit-700 WHERE name='Account 2';
  13. UPDATE accounts SET credit=credit+700 WHERE name='Account 1';
  14. SAVEPOINT T2;
  15. -- T3
  16. UPDATE accounts SET credit=credit-100 WHERE name='Account 2';
  17. UPDATE accounts SET credit=credit+100 WHERE name='Account 3';
  18. SAVEPOINT T3;
  19. ROLLBACK TO T3;
  20. ROLLBACK TO T2;
  21. ROLLBACK TO T1;
  22. ROLLBACK;
  23. -- PART B
  24. ALTER TABLE accounts ADD COLUMN BankName VARCHAR(30);
  25. UPDATE accounts SET BankName='SberBank' WHERE name='Account 1' OR name='Account 3';
  26. UPDATE accounts SET BankName='Tinkoff' WHERE name='Account 2';
  27. INSERT INTO accounts (name, credit, currency) VALUES('Account 4', 1000, 'RUB');
  28. BEGIN;
  29. -- T1
  30. UPDATE accounts SET credit=credit-500 WHERE name='Account 1';
  31. UPDATE accounts SET credit=credit+500 WHERE name='Account 3';
  32. DO
  33. $do$
  34. BEGIN
  35. IF (SELECT COUNT(DISTINCT BankName) FROM accounts WHERE name='Account 1' OR name='Account 3') >= 2 THEN
  36. UPDATE accounts SET credit=credit-30 WHERE name='Account 1';
  37. UPDATE accounts SET credit=credit+30 WHERE name='Account 4';
  38. END IF;
  39. END
  40. $do$;
  41. SAVEPOINT T1;
  42. -- T2
  43. UPDATE accounts SET credit=credit-700 WHERE name='Account 2';
  44. UPDATE accounts SET credit=credit+700 WHERE name='Account 1';
  45. DO
  46. $do$
  47. BEGIN
  48. IF (SELECT COUNT(DISTINCT BankName) FROM accounts WHERE name='Account 2' OR name='Account 1') >= 2 THEN
  49. UPDATE accounts SET credit=credit-30 WHERE name='Account 2';
  50. UPDATE accounts SET credit=credit+30 WHERE name='Account 4';
  51. END IF;
  52. END
  53. $do$;
  54. SAVEPOINT T2;
  55. -- T3
  56. UPDATE accounts SET credit=credit-100 WHERE name='Account 2';
  57. UPDATE accounts SET credit=credit+100 WHERE name='Account 3';
  58. DO
  59. $do$
  60. BEGIN
  61. IF (SELECT COUNT(DISTINCT BankName) FROM accounts WHERE name='Account 2' OR name='Account 3') >= 2 THEN
  62. UPDATE accounts SET credit=credit-30 WHERE name='Account 2';
  63. UPDATE accounts SET credit=credit+30 WHERE name='Account 4';
  64. END IF;
  65. END
  66. $do$;
  67. SAVEPOINT T3;
  68. ROLLBACK TO T3;
  69. ROLLBACK TO T2;
  70. ROLLBACK TO T1;
  71. ROLLBACK;
  72. -- Part C
  73. CREATE TABLE Ledger (id Serial, fromId int, toId int, fee numeric, amount numeric, transactionDateTime timestamp);
  74. BEGIN;
  75. -- T1
  76. UPDATE accounts SET credit=credit-500 WHERE name='Account 1';
  77. UPDATE accounts SET credit=credit+500 WHERE name='Account 3';
  78. DO
  79. $do$
  80. BEGIN
  81. IF (SELECT COUNT(DISTINCT BankName) FROM accounts WHERE name='Account 1' OR name='Account 3') >= 2 THEN
  82. UPDATE accounts SET credit=credit-30 WHERE name='Account 1';
  83. UPDATE accounts SET credit=credit+30 WHERE name='Account 4';
  84. INSERT INTO Ledger (fromId, toId, fee, amount, transactionDateTime) Values(1, 3, 30, 500, CURRENT_TIMESTAMP);
  85. ELSE
  86. INSERT INTO Ledger (fromId, toId, fee, amount, transactionDateTime) Values(1, 3, 0, 500, CURRENT_TIMESTAMP);
  87. END IF;
  88. END
  89. $do$;
  90. SAVEPOINT T1;
  91. -- T2
  92. UPDATE accounts SET credit=credit-700 WHERE name='Account 2';
  93. UPDATE accounts SET credit=credit+700 WHERE name='Account 1';
  94. DO
  95. $do$
  96. BEGIN
  97. IF (SELECT COUNT(DISTINCT BankName) FROM accounts WHERE name='Account 2' OR name='Account 1') >= 2 THEN
  98. UPDATE accounts SET credit=credit-30 WHERE name='Account 2';
  99. UPDATE accounts SET credit=credit+30 WHERE name='Account 4';
  100. INSERT INTO Ledger (fromId, toId, fee, amount, transactionDateTime) Values(2, 1, 30, 700, CURRENT_TIMESTAMP);
  101. ELSE
  102. INSERT INTO Ledger (fromId, toId, fee, amount, transactionDateTime) Values(2, 1, 0, 700, CURRENT_TIMESTAMP);
  103. END IF;
  104. END
  105. $do$;
  106. SAVEPOINT T2;
  107. -- T3
  108. UPDATE accounts SET credit=credit-100 WHERE name='Account 2';
  109. UPDATE accounts SET credit=credit+100 WHERE name='Account 3';
  110. DO
  111. $do$
  112. BEGIN
  113. IF (SELECT COUNT(DISTINCT BankName) FROM accounts WHERE name='Account 2' OR name='Account 3') >= 2 THEN
  114. UPDATE accounts SET credit=credit-30 WHERE name='Account 2';
  115. UPDATE accounts SET credit=credit+30 WHERE name='Account 4';
  116. INSERT INTO Ledger (fromId, toId, fee, amount, transactionDateTime) Values(2, 4, 30, 100, CURRENT_TIMESTAMP);
  117. ELSE
  118. INSERT INTO Ledger (fromId, toId, fee, amount, transactionDateTime) Values(2, 4, 0, 100, CURRENT_TIMESTAMP);
  119. END IF;
  120. END
  121. $do$;
  122. SAVEPOINT T3;
  123. ROLLBACK TO T3;
  124. ROLLBACK TO T2;
  125. ROLLBACK TO T1;
  126. ROLLBACK;