Skip to content

Commit

Permalink
bank
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Aug 16, 2024
1 parent 4bd7c89 commit 4ff5d12
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 5 deletions.
20 changes: 20 additions & 0 deletions sqlite/examples/bank/out.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Jane|0
Mary|1000
Ann|1000
Total|2000
-----
Jane|100
Mary|900
Ann|1000
Total|2000
-----
Jane|100
Mary|800
Ann|1000
Total|1900
-----
Jane|100
Mary|800
Ann|1000
Total|1900
-----
10 changes: 10 additions & 0 deletions sqlite/examples/bank/setup_bank.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE bank (
name TEXT PRIMARY KEY,
balance INTEGER NOT NULL
);

INSERT INTO bank (name, balance) VALUES ("Jane", 0);
INSERT INTO bank (name, balance) VALUES ("Mary", 1000);
INSERT INTO bank (name, balance) VALUES ("Ann", 1000);


3 changes: 3 additions & 0 deletions sqlite/examples/bank/show.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT * FROM bank;
SELECT "Total", SUM(balance) FROM bank;
SELECT "-----";
13 changes: 13 additions & 0 deletions sqlite/examples/bank/steps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sqlite3 bank.db < setup_bank.sql
sqlite3 bank.db < show.sql

sqlite3 bank.db < transfer.sql
sqlite3 bank.db < show.sql

sqlite3 bank.db < without_transaction.sql
sqlite3 bank.db < show.sql

sqlite3 bank.db < with_transaction.sql
sqlite3 bank.db < show.sql

rm -f bank.db
3 changes: 3 additions & 0 deletions sqlite/examples/bank/transfer.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
UPDATE bank SET balance = (SELECT balance FROM bank WHERE name = "Mary") - 100 WHERE name = "Mary";
UPDATE bank SET balance = (SELECT balance FROM bank WHERE name = "Jane") + 100 WHERE name = "Jane";

5 changes: 5 additions & 0 deletions sqlite/examples/bank/with_transaction.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BEGIN TRANSACTION;
UPDATE bank SET balance = (SELECT balance FROM bank WHERE name = "Mary") - 100 WHERE name = "Mary";
.exit
UPDATE bank SET balance = (SELECT balance FROM bank WHERE name = "Jane") + 100 WHERE name = "Jane";
COMMIT;
4 changes: 4 additions & 0 deletions sqlite/examples/bank/without_transaction.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
UPDATE bank SET balance = (SELECT balance FROM bank WHERE name = "Mary") - 100 WHERE name = "Mary";
.exit
UPDATE bank SET balance = (SELECT balance FROM bank WHERE name = "Jane") + 100 WHERE name = "Jane";

25 changes: 20 additions & 5 deletions sqlite/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ Meaning of life?|42

![](examples/foreign-key.sql)

## Transactions
{id: transactions}

![](examples/transaction.sql)

## PRAGMA
{id: pragma}
Expand All @@ -179,10 +175,29 @@ Meaning of life?|42
![](examples/update.sql)


## SQLite Transaction - in a bank
{id: sqlite-transactions-in-a-bank}

* Setup Bank accounts with some initial money.
* Move some money from one account to another - two separate steps. Worked.
* Move some money from one account to another - two separate steps - stop in the middle. Failed.
* Move some money from one account to another - Transaction - stop in the middle. Worked. (money stayed where it was)
* Remove bank

![](examples/bank/setup_bank.sql)
![](examples/bank/show.sql)
![](examples/bank/transfer.sql)
![](examples/bank/without_transaction.sql)
![](examples/bank/with_transaction.sql)

![](examples/bank/steps.sh)

![](examples/bank/out.out)



* TODO: loading a large CSV file into the database and running queries.
* TODO: creating a multi-tabe databas, dumping it and then loading it and running queries against it.
* TODO: creating a multi-tabe database, dumping it and then loading it and running queries against it.
* TODO: FOREIGN KEY - cascading deletition?


0 comments on commit 4ff5d12

Please sign in to comment.