diff --git a/Week1/Assignment/meetup.js b/Week1/Assignment/meetup.js new file mode 100644 index 000000000..aa19754d1 --- /dev/null +++ b/Week1/Assignment/meetup.js @@ -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) +} \ No newline at end of file diff --git a/Week1/Assignment/meetup.sql b/Week1/Assignment/meetup.sql new file mode 100644 index 000000000..e1c7d3996 --- /dev/null +++ b/Week1/Assignment/meetup.sql @@ -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 +); + diff --git a/Week1/Assignment/world.js b/Week1/Assignment/world.js new file mode 100644 index 000000000..41eaa350b --- /dev/null +++ b/Week1/Assignment/world.js @@ -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) +}; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 000000000..31ec5492a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,139 @@ +{ + "name": "Databases", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "mysql2": "^3.11.5" + } + }, + "node_modules/aws-ssl-profiles": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/aws-ssl-profiles/-/aws-ssl-profiles-1.1.2.tgz", + "integrity": "sha512-NZKeq9AfyQvEeNlN0zSYAaWrmBffJh3IELMZfRpJVWgrpEbtEpnjvzqBPf+mxoI287JohRDoa+/nsfqqiZmF6g==", + "license": "MIT", + "engines": { + "node": ">= 6.0.0" + } + }, + "node_modules/denque": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz", + "integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.10" + } + }, + "node_modules/generate-function": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", + "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", + "license": "MIT", + "dependencies": { + "is-property": "^1.0.2" + } + }, + "node_modules/iconv-lite": { + "version": "0.6.3", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", + "integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==", + "license": "MIT", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g==", + "license": "MIT" + }, + "node_modules/long": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/long/-/long-5.2.3.tgz", + "integrity": "sha512-lcHwpNoggQTObv5apGNCTdJrO69eHOZMi4BNC+rTLER8iHAqGrUVeLh/irVIM7zTw2bOXA8T6uNPeujwOLg/2Q==", + "license": "Apache-2.0" + }, + "node_modules/lru-cache": { + "version": "7.18.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-7.18.3.tgz", + "integrity": "sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/lru.min": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/lru.min/-/lru.min-1.1.1.tgz", + "integrity": "sha512-FbAj6lXil6t8z4z3j0E5mfRlPzxkySotzUHwRXjlpRh10vc6AI6WN62ehZj82VG7M20rqogJ0GLwar2Xa05a8Q==", + "license": "MIT", + "engines": { + "bun": ">=1.0.0", + "deno": ">=1.30.0", + "node": ">=8.0.0" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/wellwelwel" + } + }, + "node_modules/mysql2": { + "version": "3.11.5", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.11.5.tgz", + "integrity": "sha512-0XFu8rUmFN9vC0ME36iBvCUObftiMHItrYFhlCRvFWbLgpNqtC4Br/NmZX1HNCszxT0GGy5QtP+k3Q3eCJPaYA==", + "license": "MIT", + "dependencies": { + "aws-ssl-profiles": "^1.1.1", + "denque": "^2.1.0", + "generate-function": "^2.3.1", + "iconv-lite": "^0.6.3", + "long": "^5.2.1", + "lru.min": "^1.0.0", + "named-placeholders": "^1.1.3", + "seq-queue": "^0.0.5", + "sqlstring": "^2.3.2" + }, + "engines": { + "node": ">= 8.0" + } + }, + "node_modules/mysql2/node_modules/sqlstring": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.3.tgz", + "integrity": "sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/named-placeholders": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.3.tgz", + "integrity": "sha512-eLoBxg6wE/rZkJPhU/xRX1WTpkFEwDJEN96oxFrTsqBdbT5ec295Q+CoHrL9IT0DipqKhmGcaZmwOt8OON5x1w==", + "license": "MIT", + "dependencies": { + "lru-cache": "^7.14.1" + }, + "engines": { + "node": ">=12.0.0" + } + }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", + "license": "MIT" + }, + "node_modules/seq-queue": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz", + "integrity": "sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 000000000..4a6391fe5 --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "mysql2": "^3.11.5" + }, + "type": "module" +}