Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Week3/homework/SQL-Normalization/sqlNormalization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### 1. Columns that violate 1NF

- **dinner_date**: values use different formats, so the column does not have one consistent domain.
- **food_code**: contains multiple values in a single cell (not atomic).
- **food_description**: contains multiple values in a single cell (not atomic).

### 2. Entities that can be extracted

- Member
- Dinner
- Venue
- Food

### 3. 3NF-compliant tables and columns (no junction tables)

#### Member

- member_id (PK)
- member_name
- member_address

#### Dinner

- dinner_id (PK)
- dinner_date
- venue_code (FK → Venue.venue_code)

#### Venue

- venue_code (PK)
- venue_description

#### Food

- food_code (PK)
- food_description
12 changes: 12 additions & 0 deletions Week3/homework/SQL-Transactions/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Client } from "pg";

const config = {
host: "localhost",
user: "hyfuser",
password: "hyfpass",
database: "postgres",
port: 5432,
};

const client = new Client(config);
export default client;
17 changes: 17 additions & 0 deletions Week3/homework/SQL-Transactions/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: "3.9"

services:
postgres:
image: postgres:16
container_name: postgres-db
environment:
POSTGRES_USER: hyfuser
POSTGRES_PASSWORD: hyfpass
POSTGRES_DB: postgres
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:
162 changes: 162 additions & 0 deletions Week3/homework/SQL-Transactions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Week3/homework/SQL-Transactions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "sql-transactions",
"version": "1.0.0",
"main": "client.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"pg": "^8.16.3"
}
}
40 changes: 40 additions & 0 deletions Week3/homework/SQL-Transactions/transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import client from "./client.js";

async function runTransaction() {
try {
await client.connect();
console.log("Connected to PostgreSQL database!");

await client.query("BEGIN");

//withdraw 1000 from account 101
await client.query(
`UPDATE account SET balance = balance - 1000 WHERE account_number = 101`
);

//Deposit the money to 102
await client.query(`
UPDATE account SET balance = balance + 1000 WHERE account_number = 102
`);

//log them to account changes
await client.query(`
INSERT INTO account_changes (account_number, amount, changed_date, remark)
VALUES
(101, -1000.00, NOW(), 'Transfer 1000 to 102'),
(102, 1000.00, NOW(), '1000 from 101')
`);

await client.query("COMMIT");

// if successful
console.log("Transaction executed successfully");
} catch (error) {
console.error("Error executing transaction", error);
await client.query("ROLLBACK");
} finally {
await client.end();
}
}

runTransaction();
41 changes: 41 additions & 0 deletions Week3/homework/SQL-Transactions/transactions-create-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import client from "./client.js";

async function seedDatabase() {
try {
await client.connect();
console.log("Connected to PostgreSQL database!");

await client.query("BEGIN");

await client.query("DROP TABLE IF EXISTS account_changes");

await client.query("DROP TABLE IF EXISTS account");

await client.query(`CREATE TABLE account (
account_number INTEGER NOT NULL PRIMARY KEY,
balance NUMERIC(12,2)
)`);

await client.query(`CREATE TABLE account_changes (
change_number SERIAL PRIMARY KEY,
account_number INTEGER,
amount NUMERIC(12,2),
changed_date TIMESTAMP,
remark VARCHAR(250),
FOREIGN KEY (account_number) REFERENCES account(account_number)
)`);

await client.query("COMMIT");

// if successful
console.log("Tables created successfully");
} catch (error) {
console.error("Error seeding database:", error);
await client.query("ROLLBACK");
console.log("Transaction failed");
} finally {
await client.end();
}
}

seedDatabase();
37 changes: 37 additions & 0 deletions Week3/homework/SQL-Transactions/transactions-insert-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import client from "./client.js";

async function insertValues() {
try {
await client.connect();
console.log("Connected to PostgreSQL database!");

await client.query("BEGIN");

await client.query(`INSERT INTO account (account_number, balance) VALUES
(101, 2000.00),
(102, 500.00),
(103, 300.00),
(104, 50.00),
(105, 0.00)`);

await client.query(`INSERT INTO account_changes(account_number, amount, changed_date, remark) VALUES
(101, 200.00, '2025-01-20T10:15:00Z', 'Deposit'),
(102, -50.00, '2025-01-21T14:30:00Z', 'ATM Withdrawal'),
(103, 1000.00, '2025-01-22T09:00:00Z', 'Salary Deposit'),
(101, -100.00, '2025-01-23T11:45:00Z', 'Online Purchase'),
(101, 200.00, '2025-01-24T10:15:00Z', 'ATM Withdrawal')
`);

await client.query("COMMIT");

// if successful
console.log("Tables inserted successfully");
} catch (error) {
console.error("Error inserting tables", error);
await client.query("ROLLBACK");
} finally {
await client.end();
}
}

insertValues();
Loading