forked from HackYourFuture/databases
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Moayad mohammed databases week3 #27
Open
TheYodash
wants to merge
12
commits into
HackYourAssignment:main
Choose a base branch
from
TheYodash:moayad-mohammed-databases-week3
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
12 commits
Select commit
Hold shift + click to select a range
1bc268f
submitting db w1 assignment
TheYodash 1ac3e5c
removed comments
TheYodash e99f813
submitting w2 assignment
TheYodash b7ca63e
Delete Week1/meetupDatabase.js
TheYodash c0a38a2
Delete Week1/worldDatabase.js
TheYodash 1f4634b
Update aggregate.js
TheYodash 2ae1043
Submitting week3 assignment
TheYodash 894f55f
Delete Week2/Assignment/aggregate.js
TheYodash 6ca913d
Delete Week2/Assignment/dbConnect.js
TheYodash 89e55e4
Delete Week2/Assignment/joins.js
TheYodash 76b83ad
Delete Week2/Assignment/keys.js
TheYodash 6320b44
Delete Week2/Assignment/relationships.js
TheYodash 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 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 | ||
/Week3/.vscode |
This file contains 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,17 @@ | ||
{ | ||
"sqltools.connections": [ | ||
{ | ||
"mysqlOptions": { | ||
"authProtocol": "default", | ||
"enableSsl": "Disabled" | ||
}, | ||
"previewLimit": 50, | ||
"server": "localhost", | ||
"port": 3306, | ||
"driver": "MySQL", | ||
"name": "hyfDvConnection", | ||
"database": "assignmentDatabase", | ||
"username": "hyfuser" | ||
} | ||
] | ||
} |
38 changes: 38 additions & 0 deletions
38
Week3/Assignment/Exercise 1 : SQL Normalization/Normalization.md
This file contains 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,38 @@ | ||
# Normalization | ||
## 1. What columns violate 1NF? | ||
`food_code` and `food_description` | ||
___ | ||
## 2. What entities do you recognize that could be extracted? | ||
The following entities could be extracted: | ||
1. Members | ||
2. Dinner | ||
3. Venues | ||
4. Food | ||
___ | ||
## 3. Name all the tables and columns that would make a 3NF compliant solution. | ||
|
||
1. Members: | ||
- member_id | ||
- member_name | ||
- member_address | ||
|
||
2. Dinners: | ||
- dinner_id | ||
- dinner_date | ||
- venue_code | ||
|
||
3. Venues: | ||
- venue_code | ||
- venue_description | ||
|
||
4. Foods Table: | ||
- food_code | ||
- food_description | ||
|
||
5. Member_Dinner (junction table): | ||
- member_id | ||
- dinner_id | ||
|
||
6. Dinner_Food Table (junction table): | ||
- dinner_id | ||
- food_code |
96 changes: 96 additions & 0 deletions
96
Week3/Assignment/Exercise 2 : SQL Transactions/transaction.js
This file contains 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 @@ | ||
|
||
const { createConnection } = require('mysql2'); | ||
|
||
|
||
// Create a connection to the database | ||
const connection = createConnection({ | ||
host: 'localhost', | ||
user: 'hyfuser', | ||
password: 'hyfpassword', | ||
database: 'Transactions', | ||
}); | ||
|
||
// Connect to the database | ||
connection.connect((err) => { | ||
if (err) { | ||
console.error('Error connecting to the database: ' + err.stack); | ||
return; | ||
} | ||
console.log('Connected to the database.'); | ||
}); | ||
|
||
connection.beginTransaction((err) => { | ||
if (err) { | ||
console.error('Error starting transaction: ' + err.stack); | ||
return; | ||
} | ||
|
||
const senderAccountNumber = 1001; | ||
const receiverAccountNumber = 1002; | ||
const amount = 1000; | ||
|
||
// Deduct the amount from the sender's account | ||
|
||
// Declaring queries as constants | ||
const deductAmountFromSenderAccount = `UPDATE account SET balance = balance - ${amount} WHERE account_number = ${senderAccountNumber}`; | ||
const addAmountToReceiverAccount = `UPDATE account SET balance = balance + ${amount} WHERE account_number = ${receiverAccountNumber}`; | ||
const logSenderTransaction = `INSERT INTO account_changes (account_number, amount, changed_date, remark) VALUES (${senderAccountNumber}, -${amount}, CURDATE(), 'Transfer to account ${receiverAccountNumber}')`; | ||
const logReceiverTransaction = `INSERT INTO account_changes (account_number, amount, changed_date, remark) VALUES (${receiverAccountNumber}, ${amount}, CURDATE(), 'Transfer from account ${senderAccountNumber}')`; | ||
|
||
connection.query(deductAmountFromSenderAccount, [amount, senderAccountNumber], (err, result) => { | ||
if (err) { | ||
connection.rollback(() => { | ||
console.error('Error deducting amount from sender account: ' + err.stack); | ||
return; | ||
}); | ||
} | ||
|
||
// Add the amount to the receiver's account | ||
connection.query(addAmountToReceiverAccount, (err, result) => { | ||
if (err) { | ||
connection.rollback(() => { | ||
console.error('Error adding amount to receiver account: ' + err.stack); | ||
return; | ||
}); | ||
} | ||
|
||
// Log the transaction in the account_changes table for sender | ||
connection.query(logSenderTransaction, (err, result) => { | ||
if (err) { | ||
connection.rollback(() => { | ||
console.error('Error logging transaction: ' + err.stack); | ||
return; | ||
}); | ||
} | ||
// Log the transaction in the account_changes table for receiver | ||
connection.query(logReceiverTransaction, (err, result) => { | ||
if (err) { | ||
connection.rollback(() => { | ||
console.error('Error logging transaction: ' + err.stack); | ||
return; | ||
}); | ||
} | ||
|
||
// Commit the transaction | ||
connection.commit((err) => { | ||
if (err) { | ||
connection.rollback(() => { | ||
console.error('Error committing transaction: ' + err.stack); | ||
return; | ||
}); | ||
} | ||
|
||
console.log('Transaction completed successfully.'); | ||
connection.end((err) => { | ||
if (err) { | ||
console.error('Error closing the database connection: ' + err.stack); | ||
return; | ||
} | ||
console.log('Connection closed.'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
Week3/Assignment/Exercise 2 : SQL Transactions/transactions-create-tables.js
This file contains 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,66 @@ | ||
|
||
const createConnection = require('mysql2').createConnection; | ||
|
||
|
||
// Create a connection to the database | ||
const connection = createConnection({ | ||
host: 'localhost', | ||
user: 'hyfuser', | ||
password: 'hyfpassword', | ||
database: 'Transactions', | ||
}); | ||
|
||
// Connect to the database | ||
connection.connect((err) => { | ||
if (err) { | ||
console.error('Error connecting to the database: ' + err.stack); | ||
return; | ||
} | ||
console.log('Connected to the database.'); | ||
}); | ||
|
||
// Create the account table | ||
const createAccountTable = ` | ||
CREATE TABLE IF NOT EXISTS account ( | ||
account_number INT PRIMARY KEY, | ||
balance DECIMAL(10, 2) | ||
); | ||
`; | ||
|
||
// Create the account_changes table | ||
const createAccountChangesTable = ` | ||
CREATE TABLE IF NOT EXISTS account_changes ( | ||
change_number INT PRIMARY KEY AUTO_INCREMENT, | ||
account_number INT, | ||
amount DECIMAL(10, 2), | ||
changed_date DATE, | ||
remark VARCHAR(255), | ||
FOREIGN KEY (account_number) REFERENCES account(account_number) | ||
); | ||
`; | ||
|
||
// Execute the queries to create the tables | ||
connection.query(createAccountTable, (err) => { | ||
if (err) { | ||
console.error('Error creating account table: ' + err.stack); | ||
return; | ||
} | ||
console.log('Account table created successfully.'); | ||
}); | ||
|
||
connection.query(createAccountChangesTable, (err) => { | ||
if (err) { | ||
console.error('Error creating account_changes table: ' + err.stack); | ||
return; | ||
} | ||
console.log('Account_changes table created successfully.'); | ||
}); | ||
|
||
// Close the database connection | ||
connection.end((err) => { | ||
if (err) { | ||
console.error('Error closing the database connection: ' + err.stack); | ||
return; | ||
} | ||
console.log('Database connection closed.'); | ||
}); |
68 changes: 68 additions & 0 deletions
68
Week3/Assignment/Exercise 2 : SQL Transactions/transactions-insert-values.js
This file contains 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,68 @@ | ||
|
||
const createConnection = require('mysql2').createConnection; | ||
|
||
|
||
// Create a connection to the database | ||
const connection = createConnection({ | ||
host: 'localhost', | ||
user: 'hyfuser', | ||
password: 'hyfpassword', | ||
database: 'Transactions', | ||
}); | ||
|
||
// Connect to the database | ||
connection.connect((err) => { | ||
if (err) { | ||
console.error('Error connecting to the database: ' + err.stack); | ||
return; | ||
} | ||
console.log('Connected to the database.'); | ||
}); | ||
|
||
// Insert values into the account table | ||
|
||
const insertAccountValues = ` | ||
INSERT INTO account (account_number, balance) VALUES | ||
(1001, 5000.00), | ||
(1002, 10000.00), | ||
(1003, 15000.00), | ||
(1004, 20000.00), | ||
(1005, 25000.00); | ||
`; | ||
|
||
connection.query(insertAccountValues, (err) => { | ||
if (err) { | ||
console.error('Error inserting values into account table: ' + err.stack); | ||
return; | ||
} | ||
console.log('Values inserted into account table successfully.'); | ||
}); | ||
|
||
// Insert values into the account_changes table | ||
|
||
const insertAccountChangesValues = ` | ||
INSERT INTO account_changes (account_number, amount, changed_date, remark) VALUES | ||
(1001, 5000.00, '2021-01-01', 'Initial deposit'), | ||
(1002, 10000.00, '2021-01-01', 'Initial deposit'), | ||
(1003, 15000.00, '2021-01-01', 'Initial deposit'), | ||
(1004, 20000.00, '2021-01-01', 'Initial deposit'), | ||
(1005, 25000.00, '2021-01-01', 'Initial deposit'); | ||
`; | ||
|
||
connection.query(insertAccountChangesValues, (err) => { | ||
if (err) { | ||
console.error('Error inserting values into account_changes table: ' + err.stack); | ||
return; | ||
} | ||
console.log('Values inserted into account_changes table successfully.'); | ||
}); | ||
|
||
// Close the database connection | ||
|
||
connection.end((err) => { | ||
if (err) { | ||
console.error('Error closing the database connection: ' + err.stack); | ||
return; | ||
} | ||
console.log('Database connection closed.'); | ||
}); |
This file contains 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 @@ | ||
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); | ||
} | ||
); | ||
} | ||
|
||
/* | ||
A value for the variables: name and code can be manipulated to inject SQL code into the query. Like the following values: | ||
- Name: ' OR '1'='1 | ||
- code: ' OR '1'='1 | ||
will result in the following query: | ||
SELECT Population FROM ${Country} WHERE Name = '' OR '1'='1' and code = '' OR '1'='1' | ||
This query will return the population of all countries in the database, which is not the intended behavior. | ||
*/ | ||
|
||
// To prevent SQL injection, you should use parameterized queries or prepared statements. Here's an example using parameterized queries: | ||
function getPopulation(Country, name, code, cb) { | ||
conn.query( | ||
"SELECT Population FROM ?? WHERE Name = ? and code = ?", | ||
[Country, name, code], | ||
function (err, result) { | ||
if (err) cb(err); | ||
if (result.length == 0) cb(new Error("Not found")); | ||
cb(null, result[0].name); | ||
} | ||
); | ||
} |
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.
nice !