forked from HackYourFuture/databases
-
Notifications
You must be signed in to change notification settings - Fork 9
Dalia-assignment-week3 #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dalia-Saeed
wants to merge
4
commits into
HackYourAssignment:main
Choose a base branch
from
Dalia-Saeed:Dalia-assignment-w3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" }); | ||
|
|
||
| client.close(); | ||
| } | ||
|
|
||
| main(); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "dependencies": { | ||
| "dotenv": "^17.2.3", | ||
| "mongodb": "^6.21.0" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
TODOsections of the console logs. It should lead to console logs that look like the examples at the bottom of the file:databases-cohort54/Week3/homework/mongodb/index.js
Line 128 in 7d5ebd7