-
Notifications
You must be signed in to change notification settings - Fork 3
Ilias_Khugaev-w1-Databases #5
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import mysql from 'mysql2/promise'; | ||
|
|
||
| // Create the connection to database | ||
| const connection = await mysql.createConnection({ | ||
| host: 'localhost', | ||
| user: 'hyfuser', | ||
| password: 'hyfpassword' | ||
| }); | ||
|
|
||
| connection.connect(); | ||
|
|
||
| try { | ||
| await connection.query('CREATE DATABASE IF NOT EXISTS meetup'); | ||
| await connection.query('USE meetup'); | ||
| await connection.query('CREATE TABLE Invitee (invitee_no INT NOT NULL PRIMARY KEY, invitee_name VARCHAR(25) NOT NULL, invited_by VARCHAR(25))'); | ||
| await connection.query('CREATE TABLE Room (room_no INT NOT NULL PRIMARY KEY, room_name VARCHAR(25) NOT NULL, floor_number INT NOT NULL)'); | ||
|
||
| await connection.query('CREATE TABLE Meeting (meeting_no INT NOT NULL PRIMARY KEY, meeting_title VARCHAR(40) NOT NULL, staring_time DATETIME, ending_time DATETIME, room_no INT NOT NULL)'); | ||
|
|
||
| await connection.query('INSERT INTO Invitee VALUES (1, "Ilias Khugaev", "Ilia Bubnov")'); | ||
| await connection.query('INSERT INTO Invitee VALUES (2, "Anna Petrova", "Ilias Khugaev")'); | ||
| await connection.query('INSERT INTO Invitee VALUES (3, "Dmitry Sokolov", "Anna Petrova")'); | ||
| await connection.query('INSERT INTO Invitee VALUES (4, "Maria Ivanova", "Dmitry Sokolov")'); | ||
| await connection.query('INSERT INTO Invitee VALUES (5, "Oleg Smirnov", "Maria Ivanova")'); | ||
|
Comment on lines
+19
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you add |
||
|
|
||
| await connection.query('INSERT INTO Room VALUES (1, "Orion", 1)'); | ||
| await connection.query('INSERT INTO Room VALUES (2, "Pegasus", 1)'); | ||
| await connection.query('INSERT INTO Room VALUES (3, "Centauri", 2)'); | ||
| await connection.query('INSERT INTO Room VALUES (4, "Andromeda", 2)'); | ||
| await connection.query('INSERT INTO Room VALUES (5, "Phoenix", 3)'); | ||
|
|
||
| await connection.query('INSERT INTO Meeting VALUES (1, "Project Kickoff", "2025-05-21 09:00:00", "2025-05-21 10:00:00", 1)'); | ||
| await connection.query('INSERT INTO Meeting VALUES (2, "Design Review", "2025-05-21 10:30:00", "2025-05-21 11:30:00", 2)'); | ||
| await connection.query('INSERT INTO Meeting VALUES (3, "Sprint Planning", "2025-05-21 12:00:00", "2025-05-21 13:00:00", 3)'); | ||
| await connection.query('INSERT INTO Meeting VALUES (4, "Client Sync", "2025-05-21 14:00:00", "2025-05-21 15:00:00", 4)'); | ||
| await connection.query('INSERT INTO Meeting VALUES (5, "Retrospective", "2025-05-21 16:00:00", "2025-05-21 17:00:00", 5)'); | ||
|
|
||
| } catch (error) { | ||
| console.log(error); | ||
| } | ||
|
|
||
| connection.end(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import mysql from 'mysql2/promise'; | ||
|
|
||
| // Create the connection to database | ||
| const connection = await mysql.createConnection({ | ||
| host: 'localhost', | ||
| user: 'hyfuser', | ||
| password: 'hyfpassword' | ||
| }); | ||
|
|
||
| connection.connect(); | ||
|
|
||
| try { | ||
| await connection.query('USE new_world'); | ||
| // What are the names of countries with population greater than 8 million | ||
| await connection.query('SELECT name FROM country WHERE population > 8000000'); | ||
| // What are the names of countries that have “land” in their names? | ||
| await connection.query('SELECT name FROM country WHERE name LIKE "%land%"'); | ||
| // What are the names of the cities with population in between 500,000 and 1 million? | ||
| await connection.query('SELECT name FROM city WHERE population BETWEEN 500000 AND 1000000'); | ||
| // What's the name of all the countries on the continent ‘Europe’? | ||
| await connection.query('SELECT name FROM country WHERE Continent = "Europe"'); | ||
| // List all the countries in the descending order of their surface areas. | ||
| await connection.query('SELECT name FROM country ORDER BY SurfaceArea DESC'); | ||
| // What are the names of all the cities in the Netherlands? | ||
| await connection.query('SELECT name FROM city WHERE CountryCode = "NLD"'); | ||
| // What is the population of Rotterdam? | ||
| await connection.query('SELECT name, Population FROM city WHERE name = "Rotterdam"'); | ||
| // What's the top 10 countries by Surface Area? | ||
| await connection.query('SELECT name FROM country ORDER BY SurfaceArea LIMIT DESC 10'); | ||
|
||
| // What's the top 10 most populated cities? | ||
| await connection.query('SELECT name, Population FROM city ORDER BY population DESC LIMIT 10'); | ||
| // What is the population number of the world? | ||
| await connection.query('SELECT SUM(Population) FROM country'); | ||
|
|
||
| } catch (error) { | ||
| console.error(error); | ||
| } | ||
|
|
||
| connection.end(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: when you define a column as
PRIMARY KEYyou don't need to addNOT NULLrestriction, as the primary key needs to be not null.And you can also add
AUTO_INCREMENTforinvitee_no, as it is a int. This way you won't need to specifyinvitee_nowhen you insert new rows into the table.