From b9dd311fb56151fcb09fda8645c7f0180a64771d Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 27 Nov 2022 10:17:22 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20SQ?= =?UTF-8?q?L=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/create-tables.sql | 23 +++++++++++++++++++++++ sql/drop-tables.sql | 7 +++++++ sql/fail-transaction.sql | 6 ++++++ sql/refresh-balance.sql | 3 +++ sql/success-transaction.sql | 10 ++++++++++ 5 files changed, 49 insertions(+) create mode 100644 sql/create-tables.sql create mode 100644 sql/drop-tables.sql create mode 100644 sql/fail-transaction.sql create mode 100644 sql/refresh-balance.sql create mode 100644 sql/success-transaction.sql diff --git a/sql/create-tables.sql b/sql/create-tables.sql new file mode 100644 index 0000000..2543dce --- /dev/null +++ b/sql/create-tables.sql @@ -0,0 +1,23 @@ +CREATE SEQUENCE seq_person; +CREATE SEQUENCE seq_transaction; + +CREATE TABLE person +( + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('seq_person'), + balance BIGINT NOT NULL +); + +CREATE TABLE transaction +( + id BIGINT NOT NULL PRIMARY KEY DEFAULT nextval('seq_transaction'), + person_from BIGINT NOT NULL REFERENCES person (id), + person_to BIGINT NOT NULL REFERENCES person (id), + amount BIGINT NOT NULL + +); + +INSERT INTO person +VALUES (1, 1000); + +INSERT INTO person +VALUES (2, 1000); \ No newline at end of file diff --git a/sql/drop-tables.sql b/sql/drop-tables.sql new file mode 100644 index 0000000..6dfa609 --- /dev/null +++ b/sql/drop-tables.sql @@ -0,0 +1,7 @@ +drop table transaction cascade; + +drop table person cascade; + +drop sequence seq_person; + +drop sequence seq_transaction; \ No newline at end of file diff --git a/sql/fail-transaction.sql b/sql/fail-transaction.sql new file mode 100644 index 0000000..87b1400 --- /dev/null +++ b/sql/fail-transaction.sql @@ -0,0 +1,6 @@ +INSERT INTO person values (1, 100); +INSERT INTO person values (2, 100); + +UPDATE person SET balance = (balance - 10) WHERE id = 1; +INSERT INTO transaction(person_from, person_to, amount) values (1, 3, 10); +UPDATE person SET balance = (balance + 10) WHERE id = 2; diff --git a/sql/refresh-balance.sql b/sql/refresh-balance.sql new file mode 100644 index 0000000..01240bc --- /dev/null +++ b/sql/refresh-balance.sql @@ -0,0 +1,3 @@ +DELETE FROM person WHERE id = 3; +DELETE FROM transaction; +UPDATE person SET balance = 1000 WHERE id in (1,2) \ No newline at end of file diff --git a/sql/success-transaction.sql b/sql/success-transaction.sql new file mode 100644 index 0000000..df4d68c --- /dev/null +++ b/sql/success-transaction.sql @@ -0,0 +1,10 @@ +UPDATE person SET balance=100 WHERE id IN (1,2); +DELETE FROM transaction WHERE person_from = 1; + +BEGIN; + +UPDATE person SET balance = (balance - 10) WHERE id = 1; +INSERT INTO transaction(person_from, person_to, amount) values (1, 3, 10); +UPDATE person SET balance = (balance + 10) WHERE id = 2; + +COMMIT; \ No newline at end of file