-
Notifications
You must be signed in to change notification settings - Fork 510
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
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
package.json | ||
package-lock.json | ||
create-table.js | ||
create-table.js | ||
|
||
|
||
Week1/node_modules/ |
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); | ||||||
} | ||||||
console.log('Connected!'); | ||||||
}); | ||||||
|
||||||
|
||||||
/*SQL queries*/ | ||||||
const createDatabaseAndTables = | ||||||
`DROP DATABASE IF EXISTS meetup; | ||||||
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. Proposing to move |
||||||
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, | ||||||
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. Small pet peeve, indentations are a bit off. Your indentations on |
||||||
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) => { | ||||||
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.
Suggested change
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(); |
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); | ||||||||
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. Same comment as this: https://github.com/HackYourFuture/databases/pull/567/files#r1736012412 |
||||||||
} | ||||||||
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}:`); | ||||||||
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.
Suggested change
|
||||||||
console.log(results); | ||||||||
}); | ||||||||
}); | ||||||||
|
||||||||
// End the connection | ||||||||
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.
Would be nice to know what the error message says too rather than the stack trace