forked from HackYourFuture/databases
-
Notifications
You must be signed in to change notification settings - Fork 9
Ivan week3 databases #26
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
Sky-A-Fox
wants to merge
6
commits into
HackYourAssignment:main
Choose a base branch
from
Sky-A-Fox:IVAN-week3-databases
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f64a26c
w2 prep
Sky-A-Fox 70a3bf1
diagramma added
Sky-A-Fox f6b0dc7
diagramma export added
Sky-A-Fox 0872e29
cascade added
Sky-A-Fox 06b6357
some small experiment with cascade and conflict I hope it will not b…
Sky-A-Fox b3c77a0
only 3 exercises done
Sky-A-Fox 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 |
|---|---|---|
|
|
@@ -4,4 +4,5 @@ node_modules/ | |
| **/*-secret.json | ||
| **/*.sh | ||
| .idea | ||
| .env | ||
| .env | ||
| test.js | ||
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,96 @@ | ||
| -- Очищаем существующие таблицы (для повторного запуска) | ||
| DROP TABLE IF EXISTS recipe_methods; | ||
| DROP TABLE IF EXISTS recipe_ingredients; | ||
| DROP TABLE IF EXISTS recipe_categories; | ||
| DROP TABLE IF EXISTS recipes; | ||
| DROP TABLE IF EXISTS cooking_methods; | ||
| DROP TABLE IF EXISTS ingredients; | ||
| DROP TABLE IF EXISTS main_ingredients; | ||
| DROP TABLE IF EXISTS categories; | ||
| DROP TABLE IF EXISTS cuisines; | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 1. Таблица кухонь (Italian, Chinese, Japanese…) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE cuisines ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 2. Таблица основных ингредиентов (мясо, рыба, овощи) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE main_ingredients ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(150) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 3. Таблица всех возможных ингредиентов (морковь, масло, соль) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE ingredients ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(150) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 4. Таблица категорий (суп, салат, десерт, завтрак) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE categories ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 5. Таблица методов приготовления (жарка, запекание, варка) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE cooking_methods ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 6. Главная таблица рецептов | ||
| --------------------------------------------------------- | ||
| CREATE TABLE recipes ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL, | ||
| cuisine_id INT, | ||
| main_ingredient_id INT, | ||
| FOREIGN KEY (cuisine_id) REFERENCES cuisines(id), | ||
| FOREIGN KEY (main_ingredient_id) REFERENCES main_ingredients(id) | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 7. Связь рецепт ↔ категории (many-to-many) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE recipe_categories ( | ||
| recipe_id INT NOT NULL, | ||
| category_id INT NOT NULL, | ||
| PRIMARY KEY (recipe_id, category_id), | ||
| FOREIGN KEY (recipe_id) REFERENCES recipes(id), | ||
| FOREIGN KEY (category_id) REFERENCES categories(id) | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 8. Связь рецепт ↔ ингредиенты (many-to-many) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE recipe_ingredients ( | ||
| recipe_id INT NOT NULL, | ||
| ingredient_id INT NOT NULL, | ||
| amount VARCHAR(50), -- например "200 г", "1 ст.л." | ||
| PRIMARY KEY (recipe_id, ingredient_id), | ||
| FOREIGN KEY (recipe_id) REFERENCES recipes(id), | ||
| FOREIGN KEY (ingredient_id) REFERENCES ingredients(id) | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 9. Связь рецепт ↔ методы приготовления (many-to-many) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE recipe_methods ( | ||
| recipe_id INT NOT NULL, | ||
| method_id INT NOT NULL, | ||
| PRIMARY KEY (recipe_id, method_id), | ||
| FOREIGN KEY (recipe_id) REFERENCES recipes(id), | ||
| FOREIGN KEY (method_id) REFERENCES cooking_methods(id) | ||
| ); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,111 @@ | ||
| -- Очищаем существующие таблицы (для повторного запуска) -- Drop existing tables (for re-running the script) | ||
| DROP TABLE IF EXISTS cuisines CASCADE; | ||
| DROP TABLE IF EXISTS main_ingredients CASCADE; | ||
| DROP TABLE IF EXISTS ingredients CASCADE; | ||
| DROP TABLE IF EXISTS categories CASCADE; | ||
| DROP TABLE IF EXISTS cooking_methods CASCADE; | ||
| DROP TABLE IF EXISTS recipes CASCADE; | ||
| DROP TABLE IF EXISTS recipe_categories CASCADE; | ||
| DROP TABLE IF EXISTS recipe_ingredients CASCADE; | ||
| DROP TABLE IF EXISTS recipe_methods CASCADE; | ||
| DROP TABLE IF EXISTS recipe_ingredient_amounts CASCADE; | ||
|
|
||
|
|
||
|
|
||
| --------------------------------------------------------- | ||
| -- 1. Таблица кухонь (Italian, Chinese, Japanese…) -- Table of Cuisines (Italian, Chinese, Japanese…) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE cuisines ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 2. Таблица основных ингредиентов (мясо, рыба, овощи) -- Table of Main Ingredients (meat, fish, vegetables) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE main_ingredients ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(150) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 3. Таблица всех возможных ингредиентов (морковь, масло, соль) -- Table of All Possible Ingredients (carrot, oil, salt) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE ingredients ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(150) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 4. Таблица категорий (суп, салат, десерт, завтрак) -- Table of Categories (soup, salad, dessert, breakfast) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE categories ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 5. Таблица методов приготовления (жарка, запекание, варка) -- Table of Cooking Methods (frying, baking, boiling) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE cooking_methods ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL UNIQUE | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 6. Главная таблица рецептов -- Main Recipes Table | ||
| --------------------------------------------------------- | ||
| CREATE TABLE recipes ( | ||
| id SERIAL PRIMARY KEY, | ||
| name VARCHAR(255) NOT NULL, | ||
| cuisine_id INT, | ||
| main_ingredient_id INT, | ||
| FOREIGN KEY (cuisine_id) REFERENCES cuisines(id), | ||
| FOREIGN KEY (main_ingredient_id) REFERENCES main_ingredients(id) | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 7. Связь рецепт ↔ категории (many-to-many) -- Recipe ↔ Categories Relationship (many-to-many) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE recipe_categories ( | ||
| recipe_id INT NOT NULL, | ||
| category_id INT NOT NULL, | ||
| PRIMARY KEY (recipe_id, category_id), | ||
| FOREIGN KEY (recipe_id) REFERENCES recipes(id), | ||
| FOREIGN KEY (category_id) REFERENCES categories(id) | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 8. Связь рецепт ↔ ингредиенты (many-to-many) -- Recipe ↔ Ingredients Relationship (many-to-many) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE recipe_ingredients ( | ||
| recipe_id INT NOT NULL, | ||
| ingredient_id INT NOT NULL, | ||
| amount VARCHAR(50), -- например "200 г", "1 ст.л." | ||
| PRIMARY KEY (recipe_id, ingredient_id), | ||
| FOREIGN KEY (recipe_id) REFERENCES recipes(id), | ||
| FOREIGN KEY (ingredient_id) REFERENCES ingredients(id) | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 9. Связь рецепт ↔ методы приготовления (many-to-many) -- Recipe ↔ Cooking Methods Relationship (many-to-many) | ||
| --------------------------------------------------------- | ||
| CREATE TABLE recipe_methods ( | ||
| recipe_id INT NOT NULL, | ||
| method_id INT NOT NULL, | ||
| PRIMARY KEY (recipe_id, method_id), | ||
| FOREIGN KEY (recipe_id) REFERENCES recipes(id), | ||
| FOREIGN KEY (method_id) REFERENCES cooking_methods(id) | ||
| ); | ||
|
|
||
| --------------------------------------------------------- | ||
| -- 10. кол-во ингредиентов в рецепте -- Quantity of Ingredients in Recipe | ||
| --------------------------------------------------------- | ||
| CREATE TABLE recipe_ingredient_amounts ( | ||
| recipe_id INT NOT NULL, | ||
| ingredient_id INT NOT NULL, | ||
| amount VARCHAR(50) NOT NULL, -- например "200 г", "1 ст.л." | ||
| PRIMARY KEY (recipe_id, ingredient_id), | ||
| FOREIGN KEY (recipe_id) REFERENCES recipes(id), | ||
| FOREIGN KEY (ingredient_id) REFERENCES ingredients(id) | ||
| ); |
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,98 @@ | ||
| -- ========================================== | ||
| -- Mock data inserts for Week 2 Prep Exercise | ||
| -- ========================================== | ||
|
|
||
| -- 1. Cuisines | ||
| INSERT INTO cuisines (name) VALUES | ||
| ('Japanese') | ||
| ON CONFLICT (name) DO NOTHING; | ||
|
|
||
| -- 2. Main ingredients | ||
| INSERT INTO main_ingredients (name) VALUES | ||
| ('Cheese'), | ||
| ('Vegetables'), | ||
| ('Pasta'), | ||
| ('Eggs') | ||
| ON CONFLICT (name) DO NOTHING; | ||
|
|
||
| -- 3. Categories | ||
| INSERT INTO categories (name) VALUES | ||
| ('Cake'), | ||
| ('No-Bake'), | ||
| ('Vegetarian'), | ||
| ('Vegan'), | ||
| ('Gluten-Free'), | ||
| ('Japanese') | ||
| ON CONFLICT (name) DO NOTHING; | ||
|
|
||
| -- 4. Ingredients | ||
| INSERT INTO ingredients (name) VALUES | ||
| ('Cheese'), | ||
| ('Vegetables'), | ||
| ('Pasta'), | ||
| ('Eggs'), | ||
| ('Condensed milk'), | ||
| ('Cream Cheese'), | ||
| ('Lemon Juice'), | ||
| ('Pie Crust'), | ||
| ('Cherry Jam'), | ||
| ('Brussels Sprouts'), | ||
| ('Sesame seeds'), | ||
| ('Pepper'), | ||
| ('Salt'), | ||
| ('Olive oil'), | ||
| ('Macaroni'), | ||
| ('Butter'), | ||
| ('Flour'), | ||
| ('Milk'), | ||
| ('Shredded Cheddar cheese'), | ||
| ('Soy sauce'), | ||
| ('Sugar') | ||
| ON CONFLICT (name) DO NOTHING; | ||
|
|
||
| -- 5. Recipes | ||
| INSERT INTO recipes (name, cuisine_id, main_ingredient_id) VALUES | ||
| ('No-Bake Cheesecake', NULL, 1), | ||
| ('Roasted Brussels Sprouts', NULL, 2), | ||
| ('Mac & Cheese', NULL, 3), | ||
| ('Tamagoyaki Japanese Omelette', 1, 4) | ||
| ON CONFLICT DO NOTHING; | ||
|
|
||
| -- 6. Recipe ↔ Categories | ||
| INSERT INTO recipe_categories (recipe_id, category_id) VALUES | ||
| (1, 1), (1, 2), (1, 3), | ||
| (2, 4), (2, 5), | ||
| (3, 3), | ||
| (4, 3), (4, 6) | ||
| ON CONFLICT DO NOTHING; | ||
|
|
||
| -- 7. Recipe ↔ Ingredients | ||
| INSERT INTO recipe_ingredients (recipe_id, ingredient_id, amount) VALUES | ||
| -- No-Bake Cheesecake | ||
| (1, 5, '200 ml'), | ||
| (1, 6, '250 g'), | ||
| (1, 7, '1 tbsp'), | ||
| (1, 8, '1 crust'), | ||
| (1, 9, '2 tbsp'), | ||
| -- Roasted Brussels Sprouts | ||
| (2, 10, '500 g'), | ||
| (2, 7, '1 tbsp'), | ||
| (2, 11, '1 tsp'), | ||
| (2, 12, '1 tsp'), | ||
| (2, 13, '1 tsp'), | ||
| (2, 14, '2 tbsp'), | ||
| -- Mac & Cheese | ||
| (3, 15, '200 g'), | ||
| (3, 16, '50 g'), | ||
| (3, 17, '2 tbsp'), | ||
| (3, 13, '1 tsp'), | ||
| (3, 12, '1 tsp'), | ||
| (3, 18, '200 ml'), | ||
| (3, 19, '150 g'), | ||
| -- Tamagoyaki Japanese Omelette | ||
| (4, 4, '4'), | ||
| (4, 20, '1 tbsp'), | ||
| (4, 21, '1 tsp'), | ||
| (4, 13, '1 tsp'), | ||
| (4, 14, '1 tbsp') | ||
| ON CONFLICT DO NOTHING; |
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,24 @@ | ||
| -- 1. All vegetarian recipes with potatoes (но картошки нет в данных) | ||
| SELECT r.name AS recipe_name | ||
| FROM recipes r | ||
| JOIN recipe_categories rc ON r.id = rc.recipe_id | ||
| JOIN categories c ON rc.category_id = c.id | ||
| WHERE c.name = 'Vegetarian'; | ||
|
|
||
| -- 2. All cakes that do not need baking | ||
| SELECT r.name AS recipe_name | ||
| FROM recipes r | ||
| JOIN recipe_categories rc1 ON r.id = rc1.recipe_id | ||
| JOIN categories c1 ON rc1.category_id = c1.id | ||
| JOIN recipe_categories rc2 ON r.id = rc2.recipe_id | ||
| JOIN categories c2 ON rc2.category_id = c2.id | ||
| WHERE c1.name = 'Cake' AND c2.name = 'No-Bake'; | ||
|
|
||
| -- 3. All vegan and Japanese recipes | ||
| SELECT r.name AS recipe_name | ||
| FROM recipes r | ||
| JOIN recipe_categories rc1 ON r.id = rc1.recipe_id | ||
| JOIN categories c1 ON rc1.category_id = c1.id | ||
| JOIN recipe_categories rc2 ON r.id = rc2.recipe_id | ||
| JOIN categories c2 ON rc2.category_id = c2.id | ||
| WHERE c1.name = 'Vegan' AND c2.name = 'Japanese'; |
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,34 @@ | ||
| QUESTIONS: | ||
| 1 - What columns violate 1NF? | ||
| 2 - What entities do you recognize that could be extracted? | ||
| 3 - Name all the tables and columns that would make a 3NF compliant solution. | ||
|
|
||
| ANSWERS: | ||
| 1 - colums "food_code" and "food_description" - have the same problem. They have differebt data separated by comas | ||
| 2 - Members - Club members | ||
| Dinners - Dinners (events) | ||
| Venues - Venues | ||
| Foods - Foods | ||
|
|
||
| Of cource for the real entities extraction we need more tables and info. HoweverHowever, for me personally, this is a tricky question, | ||
| because in my experience (outside of programming), we can manipulate data and play with definitions. So, theoretically. | ||
| I'd classify the member address in the task as a property rather than an entity (based solely on the column names). | ||
| However, this isn't explicitly stated, so I can assume that an address is also an entity, just in a different context. | ||
|
|
||
| 3 - table: Members | ||
| columns: Member_id, Member_name, Member_address | ||
|
|
||
| tables: Venues | ||
| columns: Venue_code, Venue_description, | ||
|
|
||
| tables: Foods | ||
| columns: Food_code, Food_description | ||
|
|
||
| tables: Dinners | ||
| columns: Dinner_id, Dinner_date, venue_code | ||
|
|
||
| tables: dinner_attendees | ||
| columns: Dinner_id, member_id | ||
|
|
||
| tables: dinner_foods | ||
| columns: Dinner_id, food_code |
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.
Thanks for the explanation! Makes sense!