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

Mahtab mardani week3 database #21

Open
wants to merge 5 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
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"cSpell.words": [
"hyfpassword",
"hyfuser",
"recipestep",
"Tamagoyaki"
]
}
244 changes: 244 additions & 0 deletions Week1/database_diagram.drawio

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions Week1/meetup_DataBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
const util = require("util");
const mysql = require("mysql");

const connection = mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: 'meetup'
});

const executeQuery = util.promisify(connection.query.bind(connection));

async function seedDatabase() {
const CREATE_DATABASE = `CREATE DATABASE IF NOT EXISTS meetup`;
const USE_DATABASE = `USE meetup`;

const CREATE_INVITEE_TABLE = `
CREATE TABLE IF NOT EXISTS Invitee
(
invitee_no INT AUTO_INCREMENT ,
invitee_name VARCHAR(50),
invited_by VARCHAR(50),
PRIMARY KEY(invitee_no)
);`;

const CREATE_ROOM_TABLE = `
CREATE TABLE IF NOT EXISTS Room
(
room_no INT AUTO_INCREMENT ,
room_name VARCHAR(50),
floor_number INT,
PRIMARY KEY(room_no)
);`;

const CREATE_MEETING_TABLE = `
CREATE TABLE IF NOT EXISTS Meeting
(
meeting_no INT AUTO_INCREMENT,
meeting_title VARCHAR(50),
starting_time DATETIME,
ending_time DATETIME,
room_no INT,
PRIMARY KEY(meeting_no),
FOREIGN KEY(room_no) REFERENCES Room(room_no)
);`;

const invitees = [
{ invitee_name: "Alice", invited_by: "Bob" },
{ invitee_name: "Bob", invited_by: "Charlie" },
{ invitee_name: "Charlie", invited_by: "Alice" },
{ invitee_name: "Dave", invited_by: "Eve" },
{ invitee_name: "Eve", invited_by: "Dave" }
];

const rooms = [
{ room_name: "Conference Room A", floor_number: 1 },
{ room_name: "Conference Room B", floor_number: 2 },
{ room_name: "Conference Room C", floor_number: 3 },
{ room_name: "Conference Room D", floor_number: 4 },
{ room_name: "Conference Room E", floor_number: 5 }
];

const meetings = [
{
meeting_title: "Project Kickoff",
starting_time: "2024-01-01 10:00:00",
ending_time: "2024-01-01 11:00:00",
room_no: 1
},
{
meeting_title: "Design Review",
starting_time: "2024-01-02 14:00:00",
ending_time: "2024-01-02 15:00:00",
room_no: 2
},
{
meeting_title: "Sprint Planning",
starting_time: "2024-01-03 09:00:00",
ending_time: "2024-01-03 10:00:00",
room_no: 3
},
{
meeting_title: "Team Building",
starting_time: "2024-01-04 16:00:00",
ending_time: "2024-01-04 17:00:00",
room_no: 4
},
{
meeting_title: "Retrospective",
starting_time: "2024-01-05 11:00:00",
ending_time: "2024-01-05 12:00:00",
room_no: 5
}
];

connection.connect();
try {
await executeQuery(CREATE_DATABASE);
await executeQuery(USE_DATABASE);
await Promise.all([
executeQuery(CREATE_INVITEE_TABLE),
executeQuery(CREATE_ROOM_TABLE),
executeQuery(CREATE_MEETING_TABLE)
]);
await Promise.all(
invitees.map((invitee) =>
executeQuery("INSERT INTO Invitee SET ?", invitee)
)
);
await Promise.all(
rooms.map((room) => executeQuery("INSERT INTO Room SET ?", room))
);
await Promise.all(
meetings.map((meeting) =>
executeQuery("INSERT INTO Meeting SET ?", meeting)
)
);
} catch (error) {
console.error(error);
}

connection.end();
}
seedDatabase();
41 changes: 41 additions & 0 deletions Week1/recipe_DataBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const mysql = require("mysql");
const connection = mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "recipe_db",
port: 3306
});
connection.connect();

const createDatabaseAndTables = () => {
const create_dataBase_query = "CREATE DATABASE IF NOT EXISTS recipe_db";
connection.query(create_dataBase_query, function (error, results, fields) {
if (error) throw error;
console.log("Database 'recipe_db' created or already exists.");

const use_database_query = "USE recipe_db";
connection.query(use_database_query, function (error, results, fields) {
console.log("Using 'recipe_db' database.");

const CREATE_TABLES = [
"CREATE TABLE IF NOT EXISTS Recipe (recipe_id INT AUTO_INCREMENT, recipe_name VARCHAR(255) NOT NULL, PRIMARY KEY(recipe_id))",
"CREATE TABLE IF NOT EXISTS Category (category_id INT AUTO_INCREMENT, category_name VARCHAR(255) NOT NULL, PRIMARY KEY(category_id))",
"CREATE TABLE IF NOT EXISTS Ingredient (ingredient_id INT AUTO_INCREMENT, ingredient_name VARCHAR(255) NOT NULL, PRIMARY KEY(ingredient_id))",
"CREATE TABLE IF NOT EXISTS Step (step_id INT AUTO_INCREMENT, step_description TEXT NOT NULL, PRIMARY KEY(step_id))",
"CREATE TABLE IF NOT EXISTS RecipeCategory (recipe_id INT, category_id INT, PRIMARY KEY(recipe_id, category_id), FOREIGN KEY(recipe_id) REFERENCES Recipe(recipe_id), FOREIGN KEY(category_id) REFERENCES Category(category_id))",
"CREATE TABLE IF NOT EXISTS RecipeIngredient (recipe_id INT, ingredient_id INT, PRIMARY KEY(recipe_id, ingredient_id), FOREIGN KEY(recipe_id) REFERENCES Recipe(recipe_id), FOREIGN KEY(ingredient_id) REFERENCES Ingredient(ingredient_id))",
"CREATE TABLE IF NOT EXISTS RecipeStep (recipe_id INT, step_id INT, step_order INT, PRIMARY KEY(recipe_id, step_id), FOREIGN KEY(recipe_id) REFERENCES Recipe(recipe_id), FOREIGN KEY(step_id) REFERENCES Step(step_id))"
];

CREATE_TABLES.forEach((query) => {
connection.query(query, function (error, results, fields) {
if (error) throw error;
console.log(`Table created or already exists.`);
});
});
connection.end();
});
});
};
module.exports = {createDatabaseAndTables , connection};
44 changes: 44 additions & 0 deletions Week1/world_queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const mysql = require('mysql');
const util = require('util');

const connection = mysql.createConnection({
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
database: 'world',
});

const execute_Query = util.promisify(connection.query.bind(connection));

async function executeQueries(){
try{
const countriesPopulation = await execute_Query('SELECT name FROM country WHERE population > 8000000' );
const countriesLand = await execute_Query("SELECT name FROM country WHERE name LIKE '%land%'");
const citiesPopulation = await execute_Query('SELECT name FROM city WHERE population BETWEEN 500000 AND 1000000');
const europeCountries = await execute_Query("SELECT name FROM country WHERE continent = 'Europe'");
const countriesBySurfaceArea = await execute_Query('SELECT name FROM country ORDER BY surfaceArea DESC');
const citiesNetherlands = await execute_Query("SELECT name FROM city WHERE countryCode = 'NLD'");
const populationRotterdam = await execute_Query("SELECT population FROM city WHERE name = 'Rotterdam'");
const top10CountriesBySurfaceArea = await execute_Query('SELECT name FROM country ORDER BY surfaceArea DESC LIMIT 10');
const top10MostPopulatedCities = await execute_Query('SELECT name FROM city ORDER BY population DESC LIMIT 10');
const worldPopulation = await execute_Query('SELECT SUM(population) AS world_population FROM country');

console.log('Countries with population greater than 8 million:', countriesPopulation);
console.log('Countries with "land" in their names:', countriesLand);
console.log('Cities with population between 500,000 and 1 million:', citiesPopulation);
console.log('All countries in Europe:', europeCountries);
console.log('All countries ordered by surface area:', countriesBySurfaceArea);
console.log('All cities in the Netherlands:', citiesNetherlands);
console.log('Population of Rotterdam:', populationRotterdam);
console.log('Top 10 countries by surface area:', top10CountriesBySurfaceArea);
console.log('Top 10 most populated cities:', top10MostPopulatedCities);
console.log('Total world population:', worldPopulation);
}
catch(error){
console.error(error);
} finally {
connection.end();
}
}

executeQueries();
52 changes: 52 additions & 0 deletions Week2/MYSQL_exercises/create_tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const mysql = require('mysql');

const connection = mysql.createConnection({
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
database: 'author_db',
});

connection.connect((err) => {
if (err) throw err;
console.log('Connected to mysql server.');
createTables();
});

function createTables() {
const AUTHORS = `
CREATE TABLE IF NOT EXISTS authors (
author_id INT AUTO_INCREMENT PRIMARY KEY,
author_name VARCHAR(255),
university VARCHAR(255),
date_of_birth DATE,
h_index INT,
gender ENUM('male', 'female', 'other'),
mentor INT,
FOREIGN KEY (mentor) REFERENCES authors(author_id) ON DELETE SET NULL
);
`;
connection.query(AUTHORS, (error, results, fields) => {
if (error) throw error;
console.log('Table "authors" created or already exists.');
createResearchPapersTable();
});
}

function createResearchPapersTable() {
const RESEARCH_PAPERS = `
CREATE TABLE IF NOT EXISTS research_papers (
paper_id INT AUTO_INCREMENT PRIMARY KEY,
paper_title VARCHAR(255),
conference VARCHAR(255),
publish_date DATE,
author_id INT,
FOREIGN KEY (author_id) REFERENCES authors(author_id) ON DELETE CASCADE
);
`;
connection.query(RESEARCH_PAPERS, (error, results, fields) => {
if (error) throw error;
console.log('Table "research_papers" created or already exists.');
connection.end();
});
}
64 changes: 64 additions & 0 deletions Week2/MYSQL_exercises/insert_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const mysql = require('mysql');

const connection = mysql.createConnection({
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
database: 'author_db',
});

connection.connect((err) => {
if (err) throw err;
console.log('Connected to mysql server.');
insertSampleData();
});

function insertSampleData() {
let authors = [];
let papers = [];

for (let i = 1; i <= 15; i++) {
const year = 1980 + i;
const author = [
`Author ${i}`,
`University ${String.fromCharCode(64 + (i % 3) + 1)}`,
`${year}-01-01`,
Math.floor(Math.random() * 20) + 1,
i % 2 === 0 ? 'male' : 'female',
i > 1 ? Math.floor(Math.random() * (i - 1)) + 1 : null
];
authors.push(author);
}

for (let i = 1; i <= 30; i++) {
const paper = [
`Paper ${i}`,
`Conference ${String.fromCharCode(64 + (i % 3) + 1)}`,
`2022-01-${(i % 30) + 1}`,
Math.floor(Math.random() * 15) + 1
];
papers.push(paper);
}

const INSERT_AUTHORS = `
INSERT INTO authors (author_name, university, date_of_birth, h_index, gender, mentor)
VALUES ?;
`;
connection.query(INSERT_AUTHORS, [authors], (error, results, fields) => {
if (error) throw error;
console.log('Sample data inserted into "authors" table.');
insertResearchPapers(papers);
});
}

function insertResearchPapers(papers) {
const INSERT_RESEARCH_PAPERS = `
INSERT INTO research_papers (paper_title, conference, publish_date, author_id)
VALUES ?;
`;
connection.query(INSERT_RESEARCH_PAPERS, [papers], (error, results, fields) => {
if (error) throw error;
console.log('Sample data inserted into "research_papers" table.');
connection.end();
});
}
Loading