Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Week3/assignment/answers.md
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`
Comment on lines +1 to +6

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_date also 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.



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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dinner and food also have many-to-many relationships.


29 changes: 29 additions & 0 deletions Week3/assignment/injection.js
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
}
);
}
143 changes: 143 additions & 0 deletions Week3/assignment/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Week3/assignment/package.json
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"
}
}
26 changes: 26 additions & 0 deletions Week3/assignment/transaction.js
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();
}
30 changes: 30 additions & 0 deletions Week3/assignment/transactions-create-tables.js
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();
}
25 changes: 25 additions & 0 deletions Week3/assignment/transactions-insert-values.js
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();
}
Loading