forked from HackYourFuture/databases
-
Notifications
You must be signed in to change notification settings - Fork 9
DMYTRO_DORONIN-w3-database #27
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
Dmytro-Doronin
wants to merge
1
commit into
HackYourAssignment:main
Choose a base branch
from
Dmytro-Doronin:DMYTRO_DORONIN-w3-database
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 all commits
Commits
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,136 @@ | ||
| # Exercise 3.1 - SQL Normalization | ||
|
|
||
| Original table: | ||
|
|
||
| member_id | member_name | member_address | dinner_id | dinner_date | venue_code | venue_description | food_code | food_description | ||
| --------- | ----------- | -------------- | --------- | ----------- | ---------- | ----------------- | ---------- | ---------------- | ||
| ... | ... | ... | ... | ... | ... | ... | C1, C2 | Curry, Cake | ||
| ... | ... | ... | ... | ... | ... | ... | P1, T1, M1 | Pie, Tea, Mousse | ||
| etc. | ||
|
|
||
| --- | ||
|
|
||
| ## 1. Which columns violate 1NF? | ||
|
|
||
| 1NF (First Normal Form) requires: | ||
|
|
||
| - Each field contains **atomic** (indivisible) values. | ||
| - No repeating groups or lists in a single column. | ||
| - All values in a column are of the same type. | ||
|
|
||
| In this table, 1NF is violated by: | ||
|
|
||
| - **food_code** - contains multiple values separated by commas, e.g. `C1, C2`, `P1, T1, M1`. | ||
| - **food_description** - also contains multiple values separated by commas, e.g. `Curry, Cake`, `Pie, Tea, Mousse`. | ||
|
|
||
| Additionally, **dinner_date** is stored in multiple different formats (`2020-03-15`, `20-03-2020`, `Mar 25 '20`), which is bad practice, but they are still single scalar values. The main 1NF violation is the multi-valued food columns. | ||
|
|
||
| --- | ||
|
|
||
| ## 2. Which entities can be extracted? | ||
|
|
||
| From the table we can identify the following entities: | ||
|
|
||
| 1. **Member** | ||
| - member_id | ||
| - member_name | ||
| - member_address | ||
|
|
||
| 2. **Dinner (Event)** | ||
| - dinner_id | ||
| - dinner_date | ||
| - venue_code (the place where the dinner is held) | ||
|
|
||
| 3. **Venue** | ||
| - venue_code | ||
| - venue_description | ||
|
|
||
| 4. **Food** | ||
| - food_code | ||
| - food_description | ||
|
|
||
| 5. **MemberDinner / Attendance** | ||
| - which member attended which dinner | ||
| (many-to-many relationship between Member and Dinner) | ||
|
|
||
| 6. **DinnerFood / Menu** | ||
| - which food items were served at a given dinner | ||
| (many-to-many relationship between Dinner and Food) | ||
|
|
||
| --- | ||
|
|
||
| ## 3. Tables and columns for a 3NF-compliant solution | ||
|
|
||
| Below is a 3NF schema. | ||
|
|
||
| ### 1. Table `members` | ||
|
|
||
| - **member_id** (PK) | ||
| - member_name | ||
| - member_address | ||
|
|
||
| Functional dependency: | ||
| `member_id → member_name, member_address` | ||
| No transitive dependencies. | ||
|
|
||
| --- | ||
|
|
||
| ### 2. Table `venues` | ||
|
|
||
| - **venue_code** (PK) | ||
| - venue_description | ||
|
|
||
| Functional dependency: | ||
| `venue_code → venue_description` | ||
|
|
||
| --- | ||
|
|
||
| ### 3. Table `dinners` | ||
|
|
||
| - **dinner_id** (PK) | ||
| - dinner_date (DATE type) | ||
| - venue_code (FK → venues.venue_code) | ||
|
|
||
| Functional dependency: | ||
| `dinner_id → dinner_date, venue_code` | ||
|
|
||
| --- | ||
|
|
||
| ### 4. Table `foods` | ||
|
|
||
| - **food_code** (PK) | ||
| - food_description | ||
|
|
||
| Functional dependency: | ||
| `food_code → food_description` | ||
|
|
||
| --- | ||
|
|
||
| ### 5. Table `member_dinners` (attendance) | ||
|
|
||
| - **member_id** (FK → members.member_id) | ||
| - **dinner_id** (FK → dinners.dinner_id) | ||
| - (PK is the combination (member_id, dinner_id)) | ||
|
|
||
| This table represents which member attended which dinner (many-to-many). | ||
|
|
||
| --- | ||
|
|
||
| ### 6. Table `dinner_foods` (menu per dinner) | ||
|
|
||
| - **dinner_id** (FK → dinners.dinner_id) | ||
| - **food_code** (FK → foods.food_code) | ||
| - (PK is the combination (dinner_id, food_code)) | ||
|
|
||
| This table replaces the multi-valued fields `food_code` and `food_description` by storing one row per dinner–food combination. | ||
|
|
||
| --- | ||
|
|
||
| ## Final 3NF schema | ||
|
|
||
| - `members(member_id PK, member_name, member_address)` | ||
| - `venues(venue_code PK, venue_description)` | ||
| - `dinners(dinner_id PK, dinner_date, venue_code FK)` | ||
| - `foods(food_code PK, food_description)` | ||
| - `member_dinners(member_id FK, dinner_id FK, PK(member_id, dinner_id))` | ||
| - `dinner_foods(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,71 @@ | ||
| require("dotenv").config() | ||
| const { MongoClient, ObjectId } = require("mongodb") | ||
|
|
||
| const uri = process.env.MONGODB_URI | ||
|
|
||
| if (!uri) { | ||
| throw new Error("Please set MONGODB_URI in your .env file") | ||
| } | ||
|
|
||
| async function main() { | ||
| const client = new MongoClient(uri) | ||
|
|
||
| try { | ||
| await client.connect() | ||
| console.log("Connected to MongoDB Atlas") | ||
|
|
||
| const db = client.db("databaseWeek3") | ||
| const episodes = db.collection("bob_ross_episodes") | ||
|
|
||
| const newEpisode = { | ||
| title: "My Custom Joy of Painting Episode", | ||
| season: 100, | ||
| episode: 1, | ||
| elements: ["happy little trees", "mountain", "lake"], | ||
| aired_on: new Date("2025-01-01"), | ||
| } | ||
|
|
||
| const insertResult = await episodes.insertOne(newEpisode) | ||
| console.log("Inserted new episode with _id:", insertResult.insertedId) | ||
|
|
||
|
|
||
| const insertedId = insertResult.insertedId | ||
| const foundById = await episodes.findOne({ _id: insertedId }) | ||
|
|
||
| const cursor = episodes.find({ elements: "happy little trees" }) | ||
| const episodesWithTrees = await cursor.toArray() | ||
|
|
||
| console.log( | ||
| `Episodes with "happy little trees": ${episodesWithTrees.length}` | ||
| ) | ||
|
|
||
| const updateResult = await episodes.updateOne( | ||
| { _id: insertedId }, | ||
| { | ||
| $set: { | ||
| title: "Updated Custom Joy of Painting Episode", | ||
| updated_at: new Date(), | ||
| }, | ||
| } | ||
| ) | ||
|
|
||
| console.log( | ||
| `Updated episodes: matched ${updateResult.matchedCount}, modified ${updateResult.modifiedCount}` | ||
| ) | ||
|
|
||
| const updatedDoc = await episodes.findOne({ _id: insertedId }) | ||
|
|
||
| const deleteResult = await episodes.deleteOne({ _id: insertedId }) | ||
| console.log(`Deleted documents: ${deleteResult.deletedCount}`) | ||
| } catch (err) { | ||
| console.error("MongoDB error:", err) | ||
| } finally { | ||
| await client.close() | ||
| console.log("Connection to MongoDB closed") | ||
| } | ||
| } | ||
|
|
||
| main().catch((err) => { | ||
| console.error(err) | ||
| process.exit(1) | ||
| }) |
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.
Very clear explanation! Good job!