Skip to content
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

Tugbanur- Database week4 #47

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
17 changes: 17 additions & 0 deletions Week3/assignment/exercise1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
1.What columns violate 1NF?

- food_code and food_description column. Because they both contain multiple values.

2.What entities do you recognize that could be extracted?

- members, dinners, venues, food

3.Name all the tables and columns that would make a 3NF compliant solution.

- Members => member_id, member_name, member_address
- Dinners => dinner_id, dinner_date
- Venues => venue_code, venue_description
- Foods => food_code, food_description
- Dinner_Venue => dinner_id, venue_code
- Dinner_Food => dinner_id, food_code
- Member_Dinner => member_id, dinner_id
46 changes: 46 additions & 0 deletions Week3/assignment/exercise2/transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const mysql = require("mysql2/promise");

async function transferAmount() {
const connection = await mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "transaction_db",
});

try {
await connection.beginTransaction();

await connection.execute(`
UPDATE account
SET balance = balance - 1000
WHERE account_number = 101;
`);

await connection.execute(`
INSERT INTO account_changes (account_number, amount, remark)
VALUES (101, -1000, 'Transfer to account 102');
`);

await connection.execute(`
UPDATE account
SET balance = balance + 1000
WHERE account_number = 102;
`);

await connection.execute(`
INSERT INTO account_changes (account_number, amount, remark)
VALUES (102, 1000, 'Transfer from account 101');
`);

await connection.commit();
console.log("Transfer successful!");
} catch (error) {
await connection.rollback();
console.error("Transfer failed:", error.message);
} finally {
await connection.end();
}
}

transferAmount();
31 changes: 31 additions & 0 deletions Week3/assignment/exercise2/transactions-create-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const mysql = require("mysql2/promise");

async function createTables() {
const connection = await mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "transaction_db",
});
await connection.execute(`
CREATE TABLE IF NOT EXISTS account (
account_number INT PRIMARY KEY,
balance DECIMAL(15, 2) NOT NULL
);
`);

await connection.execute(`
CREATE TABLE IF NOT EXISTS account_changes (
change_number INT AUTO_INCREMENT PRIMARY KEY,
account_number INT,
amount DECIMAL(15, 2),
changed_date INTEGER,
remark VARCHAR(255),
FOREIGN KEY (account_number) REFERENCES account(account_number)
);
`);

await connection.end();
}

createTables().catch(console.error);
18 changes: 18 additions & 0 deletions Week3/assignment/exercise2/transactions-insert-values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mysql = require("mysql2/promise");

async function insertTables() {
const connection = await mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "transaction_db",
});
await connection.execute(`
INSERT INTO account (account_number, balance) VALUES
(101, 5000.00),
(102, 3000.00);
`);
await connection.end();
}

insertTables().catch(console.error);
11 changes: 11 additions & 0 deletions Week3/assignment/exercise3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function getPopulation(Country, name, code, cb) {
// assuming that connection to the database is established and stored as conn
const query = `SELECT Population FROM ?? WHERE Name = ? and code = ?`;
const values = [Country, name, code];

conn.query(query, values, (err, result) => {
if (err) return cb(err);
if (result.length === 0) return cb(new Error("Not found"));
cb(null, result[0].Population);
});
}
16 changes: 16 additions & 0 deletions Week3/assignment/exercise4/bobRossEpisode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mongoose from "mongoose";
const { Schema } = mongoose;

const episodeSchema = new Schema({
_id: {
type: Schema.Types.ObjectId,
default: () => new mongoose.Types.ObjectId(),
},
title: String,
season: Number,
episode: Number,
elements: [String],
});

const BobRossEpisode = mongoose.model("BobRossEpisode", episodeSchema);
export default BobRossEpisode;
57 changes: 57 additions & 0 deletions Week3/assignment/exercise4/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import mongoose from "mongoose";
import dotenv from "dotenv";
import BobRossEpisode from "./bobRossEpisode.js";

dotenv.config();

mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Connected to MongoDB"))
.catch((err) => console.error("Could not connect to MongoDB", err));

async function addEpisode(data) {
const episode = new BobRossEpisode(data);
await episode.save();
console.log("Episode added:", episode);
}

addEpisode({
title: "Mountain Retreat",
season: 3,
episode: 5,
elements: ["mountain", "trees", "clouds"],
});

async function findAllEpisodes() {
const episodes = await BobRossEpisode.find();
console.log("All episodes:", episodes);
}

findAllEpisodes();

async function updateEpisode(id, data) {
const episode = await BobRossEpisode.findByIdAndUpdate(id, data, {
new: true,
runValidators: true,
});
if (episode) {
console.log("Updated episode:", episode);
} else {
console.log("Updated episode:", episode);
}
}
updateEpisode("61426985e216000000477259");

async function deleteEpisode(id) {
const episode = await BobRossEpisode.findByIdAndDelete(id);
if (episode) {
console.log("Episode deleted");
} else {
console.log("Episode not found");
}
}

deleteEpisode("61426985e216000000477259");
Loading