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

Hanna melnyk w2 databases #567

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion Week1/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package.json
package-lock.json
create-table.js
create-table.js


Week1/node_modules/
76 changes: 76 additions & 0 deletions Week1/exercise_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const mysql = require('mysql');

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


/*Connect to SQL server*/
connection.connect(err => {
if (err) {
return console.error('Connection error: ' + err.stack);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to know what the error message says too rather than the stack trace

}
console.log('Connected!');
});


/*SQL queries*/
const createDatabaseAndTables =
`DROP DATABASE IF EXISTS meetup;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proposing to move meetup to a const. Like const DB_NAME = 'meetup' and use this in the template string. This way you can avoid typos.

CREATE DATABASE meetup;
USE meetup;

CREATE TABLE Invitee (
invitee_no INT AUTO_INCREMENT PRIMARY KEY,
invitee_name VARCHAR(100),
invited_by VARCHAR(100)
);

CREATE TABLE Room (
room_no INT AUTO_INCREMENT PRIMARY KEY,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small pet peeve, indentations are a bit off. Your indentations on Meeting are different than the rest

room_name VARCHAR(50),
floor_number INT
);

CREATE TABLE Meeting (
meeting_no INT AUTO_INCREMENT PRIMARY KEY,
meeting_title VARCHAR(100),
starting_time DATETIME,
ending_time DATETIME,
room_no INT,
FOREIGN KEY (room_no) REFERENCES Room(room_no)
);

INSERT INTO Room (room_name, floor_number) VALUES
('Paris', 1),
('New York', 2),
('Tokyo', 3),
('London', 4),
('Berlin', 5);

INSERT INTO Invitee (invitee_name, invited_by) VALUES
('Victor Hugo', 'Alexandre Dumas'),
('Mark Twain', 'Henry James'),
('Haruki Murakami', 'Kenzaburo Oe'),
('Charles Dickens', 'Wilkie Collins'),
('Albert Einstein', 'Niels Bohr');

INSERT INTO Meeting (meeting_title, starting_time, ending_time, room_no) VALUES
('Literary Classics Discussion', '2024-08-01 09:00:00', '2024-08-01 10:00:00', 1),
('American Literature Seminar', '2024-08-02 11:00:00', '2024-08-02 12:00:00', 2),
('Japanese Fiction Workshop', '2024-08-03 14:00:00', '2024-08-03 15:00:00', 3),
('Victorian Literature Symposium', '2024-08-04 16:00:00', '2024-08-04 17:00:00', 4),
('Scientific Innovations Forum', '2024-08-05 13:00:00', '2024-08-05 14:00:00', 5);`;


// Execute the queries
connection.query(createDatabaseAndTables, (error, results, fields) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
connection.query(createDatabaseAndTables, (error, results, fields) => {
connection.query(createDatabaseAndTables, (error, _, __) => {

Since you are not using the two params

if (error) throw error;
console.log('Database and tables created, and data inserted');
});

// Close the connection
connection.end();
44 changes: 44 additions & 0 deletions Week1/exercise_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const mysql = require('mysql');

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

/*Connect to SQL server*/
connection.connect(err => {
if (err) {
return console.error('Connection error: ' + err.stack);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
console.log('Connected!');
});


// Queries
const queries = [
"SELECT Name FROM country WHERE Population > 8000000;",
"SELECT Name FROM country WHERE Name LIKE '%land%';",
"SELECT Name FROM city WHERE Population BETWEEN 500000 AND 1000000;",
"SELECT Name FROM country WHERE Continent = 'Europe';",
"SELECT Name FROM country ORDER BY SurfaceArea DESC;",
"SELECT Name FROM city WHERE CountryCode = 'NLD';",
"SELECT Population FROM city WHERE Name = 'Rotterdam';",
"SELECT Name FROM country ORDER BY SurfaceArea DESC LIMIT 10;",
"SELECT Name FROM city ORDER BY Population DESC LIMIT 10;",
"SELECT SUM(Population) AS WorldPopulation FROM country;"
];


// Execute each query
queries.forEach((query, index) => {
connection.query(query, (error, results) => {
if (error) throw error;
console.log(`Query ${index + 1}:`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(`Query ${index + 1}:`);
console.log(`Query ${index + 1}: ${queries[index]}`);

console.log(results);
});
});

// End the connection
connection.end();