forked from HackYourFuture/databases
-
Notifications
You must be signed in to change notification settings - Fork 3
Ilias_Khugaev-w3-Databases #21
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
Narpimers
wants to merge
2
commits into
HackYourAssignment:main
Choose a base branch
from
Narpimers:Ilias_Khugaev-w3-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
2 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,44 @@ | ||
| 1. **Which columns break 1NF?** | ||
| - Each cell must have one single value (atomic). | ||
| - No lists or repeated values in one cell. | ||
| <br>**These columns break 1NF:** | ||
| <br>`food_code → example: C1, C2` | ||
| <br>`food_description → example: Curry, Cake` | ||
|
|
||
|
|
||
| 2. **What entities can we extract?** | ||
| We can create 5 entities (tables): | ||
|
|
||
| - Members → `people who join the dinner.` | ||
|
|
||
| - Dinners → `dinner events.` | ||
|
|
||
| - Venues → `places where dinners happen.` | ||
|
|
||
| - Foods → `food items served.` | ||
|
|
||
| Connections → who ate what, where, and when. | ||
|
|
||
| 3. **Name all the tables and columns that would make a 3NF compliant solution.** | ||
| - Members | ||
| `member_id Primary Key` | ||
| `member_name Text` | ||
| `member_address Text` | ||
|
|
||
| - Dinners | ||
| `dinner_id Primary Key` | ||
| `dinner_date Date` | ||
| `venue_code Foreign Key → Venues.venue_code` | ||
|
|
||
| - Venues | ||
| `venue_code Primary Key` | ||
| `venue_description Text` | ||
|
|
||
| - Foods | ||
| `food_code Primary Key` | ||
| `food_description Text` | ||
|
|
||
| - Member_Dinner | ||
| `member_id Foreign Key → Members.member_id` | ||
| `dinner_id Foreign Key → Dinners.dinner_id` | ||
|
Comment on lines
+41
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dinner and food also have many-to-many relationships. |
||
|
|
||
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,29 @@ | ||
| // Give an example of a value that can be passed as name and code that would take advantage of SQL-injection and ( fetch all the records in the database) | ||
| function getPopulation(Country, name, code, cb) { | ||
| // assuming that connection to the database is established and stored as conn | ||
| conn.query( | ||
| `SELECT Population FROM ${Country} WHERE Name = '${name}' and code = '${code}'`, | ||
| function (err, result) { | ||
| if (err) cb(err); | ||
| if (result.length == 0) cb(new Error("Not found")); | ||
| cb(null, result[0].name); | ||
| } | ||
| ); | ||
| } | ||
| // we can pass name as ('' OR '1'='1), or code as ('' OR '1'='1) | ||
| // SELECT Population FROM Countries WHERE Name = '' OR '1'='1' and code = '' OR '1'='1'; | ||
|
|
||
|
|
||
| // Rewrite the function so that it is no longer vulnerable to SQL injection | ||
|
|
||
| function getPopulation(Country, name, code, cb) { | ||
| conn.query( | ||
| `SELECT Population FROM ?? WHERE Name = ? AND code = ?`, | ||
| [Country, name, code], | ||
| function (err, result) { | ||
| if (err) return cb(err); | ||
| if (result.length === 0) return cb(new Error("Not found")); | ||
| cb(null, result[0].Population); // исправлено: result[0].name → result[0].Population | ||
| } | ||
| ); | ||
| } |
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,15 @@ | ||
| { | ||
| "name": "assignment", | ||
| "version": "1.0.0", | ||
| "type": "module", | ||
| "main": "transactions-create-tables.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC", | ||
| "description": "", | ||
| "dependencies": { | ||
| "mysql2": "^3.14.1" | ||
| } | ||
| } |
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,26 @@ | ||
| import mysql from 'mysql2/promise'; | ||
|
|
||
| const connection = await mysql.createConnection({ | ||
| host: 'localhost', | ||
| user: 'hyfuser', | ||
| password : 'hyfpassword', | ||
| }); | ||
|
|
||
| try { | ||
| await connection.query(`USE Transactions`) | ||
| await connection.query(`START TRANSACTION`); | ||
| await connection.query(`UPDATE account SET balance = balance - 1000 WHERE account_number = 1001`); | ||
| await connection.query(`UPDATE account SET balance = balance + 1000 WHERE account_number = 1002`); | ||
| await connection.query(` | ||
| INSERT INTO account_changes(account_number, amount, changed_date, remark) | ||
| VALUES | ||
| (1001, -1000, '2020-01-01', 'transaction'), | ||
| (1002, 1000, '2020-01-02', 'deposit') | ||
| `); | ||
| await connection.query(`COMMIT`); | ||
| } catch (err) { | ||
| console.error("Transaction failed, rolling back:", err); | ||
| await connection.query(`ROLLBACK`); | ||
| } finally { | ||
| connection.end(); | ||
| } |
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,30 @@ | ||
| import mysql from 'mysql2/promise'; | ||
|
|
||
| const connection = await mysql.createConnection({ | ||
| host: 'localhost', | ||
| user: 'hyfuser', | ||
| password : 'hyfpassword', | ||
| }); | ||
|
|
||
| try { | ||
| await connection.query(`DROP DATABASE IF EXISTS Transactions`); | ||
| await connection.query(`CREATE DATABASE IF NOT EXISTS Transactions`); | ||
| await connection.query(`USE Transactions`) | ||
| await connection.query(`CREATE TABLE IF NOT EXISTS account( | ||
| account_number INT AUTO_INCREMENT PRIMARY KEY, | ||
| balance INT NOT NULL DEFAULT 0)` | ||
| ); | ||
|
|
||
| await connection.query(`CREATE TABLE IF NOT EXISTS account_changes( | ||
| change_number INT AUTO_INCREMENT PRIMARY KEY, | ||
| account_number INT, | ||
| amount INT, | ||
| changed_date DATETIME, | ||
| remark TEXT, | ||
| FOREIGN KEY (account_number) REFERENCES account(account_number))` | ||
| ); | ||
| } catch (err) { | ||
| console.log(err); | ||
| } finally { | ||
| connection.end(); | ||
| } |
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,25 @@ | ||
| import mysql from 'mysql2/promise'; | ||
|
|
||
| const connection = await mysql.createConnection({ | ||
| host: 'localhost', | ||
| user: 'hyfuser', | ||
| password : 'hyfpassword', | ||
| }); | ||
|
|
||
| try { | ||
| await connection.query(`USE Transactions`); | ||
| await connection.query(`INSERT INTO account(account_number, balance) | ||
| VALUES (1001, 5000), | ||
| (1002, 3000), | ||
| (1003, 7000)` | ||
| ); | ||
| await connection.query(`INSERT INTO account_changes(account_number, amount, changed_date, remark) | ||
| VALUES (1001, 1000, '2020-01-01', 'Initial deposit'), | ||
| (1002, -1000, '2020-01-02', 'Initial deposit'), | ||
| (1003, 1000, '2020-01-03', 'Initial deposit')` | ||
| ); | ||
| } catch (err) { | ||
| console.log(err); | ||
| } finally { | ||
| connection.end(); | ||
| } |
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.
Your answer is correct.
dinner_datealso has a issue: its format is not uniform. Sometimes it is a ISO string but sometimes not. We also need to avoid this kind of situation.For date/time, MySQL has types for it. We should use that instead of use
varchar.