Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
75 changes: 75 additions & 0 deletions Week3/assignment3/answers ex1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
✅ 3.1 – Normalization Exercise
Which columns violate 1NF?

1NF requires:

No repeating groups

No multivalued attributes

Atomic values

Violations:

food_code → contains multiple codes (e.g., C1, C2)

food_description → contains multiple values (e.g., Curry, Cake)

dinner_date → inconsistent date formats (not strictly 1NF violation, but bad practice)

2. Extracted Entities

Member

Dinner

Venue

Food

Member–Dinner (many-to-many)

Dinner–Food (many-to-many)

3. 3NF Tables
Member

member_id (PK)

member_name

member_address

Venue

venue_code (PK)

venue_description

Dinner

dinner_id (PK)

dinner_date

venue_code (FK → Venue)

Food

food_code (PK)

food_description

MemberDinner

member_id (FK)

dinner_id (FK)
PK: (member_id, dinner_id)

DinnerFood

dinner_id (FK)

food_code (FK)
PK: (dinner_id, food_code)
41 changes: 41 additions & 0 deletions Week3/assignment3/exercise2/transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const mysql = require("mysql2/promise");

async function main() {
const conn = await mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "bankdb",
});

try {
await conn.beginTransaction();

// debit sender
await conn.execute(`
UPDATE account SET balance = balance - 1000 WHERE account_number = 101
`);

// credit receiver
await conn.execute(`
UPDATE account SET balance = balance + 1000 WHERE account_number = 102
`);

// log changes
await conn.execute(
`INSERT INTO account_changes (account_number, amount, remark)
VALUES (101, -1000, 'Money transferred to 102'),
(102, 1000, 'Money received from 101')`
);

await conn.commit();
console.log("Transaction completed.");
} catch (err) {
await conn.rollback();
console.error("Transaction rolled back:", err);
}

conn.end();
}

main();
33 changes: 33 additions & 0 deletions Week3/assignment3/exercise2/transactions-create-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const mysql = require("mysql2/promise");

async function main() {
const conn = await mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "bankdb",
});

await conn.execute(`
CREATE TABLE IF NOT EXISTS account (
account_number INT PRIMARY KEY,
balance DECIMAL(10,2) NOT NULL
)
`);

await conn.execute(`
CREATE TABLE IF NOT EXISTS account_changes (
change_number INT AUTO_INCREMENT PRIMARY KEY,
account_number INT,
amount DECIMAL(10,2),
changed_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
remark VARCHAR(255),
FOREIGN KEY (account_number) REFERENCES account(account_number)
)
`);

console.log("Tables created.");
conn.end();
}

main();
20 changes: 20 additions & 0 deletions Week3/assignment3/exercise2/transactions-insert-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const mysql = require("mysql2/promise");

async function main() {
const conn = await mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "bankdb",
});

await conn.execute(
`INSERT INTO account (account_number, balance)
VALUES (101, 5000), (102, 2000)`
);

console.log("Sample data inserted.");
conn.end();
}

main();
3 changes: 3 additions & 0 deletions Week3/assignment3/exercise4/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MONGO_URI="your mongodb connection string"
DB_NAME="databaseWeek3"
COLLECTION="bob_ross_episodes"
40 changes: 40 additions & 0 deletions Week3/assignment3/exercise4/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require("dotenv").config();
const { MongoClient } = require("mongodb");
const seedDatabase = require("./seedDatabase");

const uri = process.env.MONGO_URI;
const dbName = process.env.DB_NAME;
const collectionName = process.env.COLLECTION;

async function main() {
const client = new MongoClient(uri);
await client.connect();

const db = client.db(dbName);
const episodes = db.collection(collectionName);

await seedDatabase(db);

console.log("All episodes:");
console.log(await episodes.find().toArray());

// CREATE
await episodes.insertOne({ title: "New Painting", elements: ["tree", "lake"] });

// READ
const ep = await episodes.findOne({ title: "New Painting" });
console.log("Created:", ep);

// UPDATE
await episodes.updateOne(
{ title: "New Painting" },
{ $set: { elements: ["tree", "cloud"] } }
);

// DELETE
await episodes.deleteOne({ title: "New Painting" });
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you may have misunderstood the exercise requirements. The goal of the exercise is to look at the existing functions then create queries (using mongo db methods) to fill in the TODO sections of the console logs. It should lead to console logs that look like the examples at the bottom of the file:

Created season 9 episode 13 and the document got the id 625e9addd11e82a59aa9ff93


client.close();
}

main();
172 changes: 172 additions & 0 deletions Week3/assignment3/exercise4/package-lock.json

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

6 changes: 6 additions & 0 deletions Week3/assignment3/exercise4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"dotenv": "^17.2.3",
"mongodb": "^6.21.0"
}
}
Loading