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

Lidiia Starshynova w1 databases #3

Open
wants to merge 3 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
42 changes: 42 additions & 0 deletions Week1/Assignment/meetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { createConnection } from 'mysql2/promise';
import { readFile } from 'fs/promises';

const connection = await createConnection({
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
multipleStatements: true,
});

const sqlDatabase = await readFile('meetup.sql', 'utf-8');

const insertInvitee = `INSERT INTO Invitee (invitee_name, invited_by) VALUES
('Kate', 'John'),
('Kris', 'Sara'),
('Michael', 'John'),
('Dimitri', 'John'),
('Maria', 'Sara')`;

const insertRoom = `INSERT INTO Room (room_no, room_name, floor_number) VALUES
(104, 'Blue room', 1),
(215, 'Violet room', 2),
(906, 'Green room', 9),
(302, 'Orange room', 3),
(534, 'Blue room', 5)`;

const insertMeeting = `INSERT INTO Meeting (meeting_title, starting_time, ending_time, room_no) VALUES
('CatchUp', '2024-12-16 11:00:00', '2024-12-16 11:30:00', 906),
('Project 1', '2024-12-17 13:00:00', '2024-12-17 14:00:00', 302),
('Discussion of the next projects', '2024-12-17 15:00:00', '2024-12-17 18:00:00', 534),
('Discussion of new vacancies', '2024-12-18 13:00:00', '2024-12-18 14:00:00', 104),
('New hire discussion', '2024-12-18 15:00:00', '2024-12-18 16:00:00', 215)`;

try {
await connection.query(sqlDatabase);
await connection.query(insertInvitee);
await connection.query(insertRoom);
await connection.query(insertMeeting);
connection.end();
} catch (err) {
console.error('Connection error', err)
}
32 changes: 32 additions & 0 deletions Week1/Assignment/meetup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CREATE DATABASE IF NOT EXISTS meetup;
USE meetup;

DROP TABLE IF EXISTS Meeting;
DROP TABLE IF EXISTS Room;
DROP TABLE IF EXISTS Invitee;


CREATE TABLE Invitee (
invitee_no INT(6) NOT NULL AUTO_INCREMENT,
invitee_name VARCHAR(255) NOT NULL,
invited_by VARCHAR(255),
PRIMARY KEY (invitee_no)
);

CREATE TABLE Room (
room_no INT(6) NOT NULL,
room_name VARCHAR(255),
floor_number INT(3),
PRIMARY KEY (room_no)
);

CREATE TABLE Meeting (
meeting_no INT(6) AUTO_INCREMENT,
meeting_title VARCHAR(255),
starting_time DATETIME NOT NULL,
ending_time DATETIME,
room_no INT(6) NOT NULL,
PRIMARY KEY (meeting_no),
FOREIGN KEY (room_no) REFERENCES Room(room_no) ON DELETE CASCADE
);

71 changes: 71 additions & 0 deletions Week1/Assignment/world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { createConnection } from 'mysql2/promise';
import { readFile } from 'fs/promises';

const connection = await createConnection({
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
multipleStatements: true,
});

const sqlDatabase = await readFile('../databases/world.sql', 'utf-8');
const countryWithPopulationGreat8Million = `SELECT Name FROM country WHERE Population > 8000000`;
const countryWithLandInName = `SELECT Name FROM country WHERE Name LIKE '%land%'`;
const cityWithPopulationBetween500ToMillion = `SELECT Name FROM city WHERE Population BETWEEN 500000 AND 1000000`;
const countryInEUrope = `SELECT Name FROM country WHERE Continent = 'Europe'`;
const countryDescendingOrderSurfaceArea = `SELECT Name FROM country ORDER BY SurfaceArea DESC`;
const cityInNetherlands = `SELECT city.Name AS CityName, country.Name AS CountryName
FROM city JOIN country ON city.CountryCode = country.Code
WHERE country.Name = 'Netherlands'`;
const rotterdamPopulation = `SELECT Population FROM city WHERE Name = 'Rotterdam'`;
const countrySurfaceAreaTop10 = `SELECT Name FROM country ORDER BY SurfaceArea DESC LIMIT 10`;
const cityPopulationTop10 = `SELECT Name FROM city ORDER BY Population DESC LIMIT 10`;
const populationNumberOfTheWorld = `SELECT SUM(Population) AS PopulationNumberOfTheWorld FROM country`;

try {
await connection.query(sqlDatabase);

const [resultOfCountryWithPopulationGreat8Million] =
await connection.query(countryWithPopulationGreat8Million);
console.log('resultOfCountryWithPopulationGreat8Million:', resultOfCountryWithPopulationGreat8Million);

const [resultOfCountryWithLandInName] =
await connection.query(countryWithLandInName);
console.log('resultOfCountryWithLandInName:', resultOfCountryWithLandInName);

const [resultOfCityWithPopulationBetween500ToMillion] =
await connection.query(cityWithPopulationBetween500ToMillion);
console.log('resultOfCityWithPopulationBetween500ToMillion', resultOfCityWithPopulationBetween500ToMillion);

const [resultOfCountryInEUrope] =
await connection.query(countryInEUrope);
console.log('resultOfCountryInEUrope', resultOfCountryInEUrope);

const [resultOfCountryDescendingOrderSurfaceArea] =
await connection.query(countryDescendingOrderSurfaceArea);
console.log('resultOfCountryDescendingOrderSurfaceArea', resultOfCountryDescendingOrderSurfaceArea);

const [resultOfCityInNetherlands] =
await connection.query(cityInNetherlands);
console.log('resultOfCityInNetherlands', resultOfCityInNetherlands);

const [resultOfRotterdamPopulation] =
await connection.query(rotterdamPopulation);
console.log('resultOfRotterdamPopulation', resultOfRotterdamPopulation);

const [resultOfCountrySurfaceAreaTop10] =
await connection.query(countrySurfaceAreaTop10);
console.log('resultOfCountrySurfaceAreaTop10', resultOfCountrySurfaceAreaTop10);

const [resultOfCityPopulationTop10] =
await connection.query(cityPopulationTop10);
console.log('resultOfCityPopulationTop10', resultOfCityPopulationTop10);

const [resultOfPopulationNumberOfTheWorld] =
await connection.query(populationNumberOfTheWorld);
console.log('resultOfPopulationNumberOfTheWorld', resultOfPopulationNumberOfTheWorld);

connection.end();
} catch (err) {
console.error('Connection error', err)
};
139 changes: 139 additions & 0 deletions package-lock.json

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

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"mysql2": "^3.11.5"
},
"type": "module"
}