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

Elias Week 4 database #54

Open
wants to merge 9 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
102 changes: 102 additions & 0 deletions Week1/assignment/meetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Import the mysql package
const mysql = require('mysql2/promise');

// Create a connection to the database
async function main() {
const connection = await mysql.createConnection({
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
});

try {
console.log('Connected as id ' + connection.threadId);

await connection.query('DROP DATABASE IF EXISTS meetup');
console.log('Database dropped.');

await connection.query('CREATE DATABASE meetup');
console.log('Database created.');

// Use the newly created database
await connection.query('USE meetup');

// Create Invitee table
const createInviteeTable = `
CREATE TABLE Invitee (
invitee_no INT AUTO_INCREMENT PRIMARY KEY,
invitee_name VARCHAR(255) NOT NULL,
invited_by INT,
FOREIGN KEY (invited_by) REFERENCES Invitee(invitee_no)
)`;
await connection.query(createInviteeTable);
console.log('Invitee table created.');

// Create Room table
const createRoomTable = `
CREATE TABLE Room (
room_no INT AUTO_INCREMENT PRIMARY KEY,
room_name VARCHAR(255) NOT NULL,
floor_number TINYINT NOT NULL
)`;
await connection.query(createRoomTable);
console.log('Room table created.');

// Create Meeting table
const createMeetingTable = `
CREATE TABLE Meeting (
meeting_no INT AUTO_INCREMENT PRIMARY KEY,
meeting_title VARCHAR(255) NOT NULL,
starting_time DATETIME NOT NULL,
ending_time DATETIME NOT NULL,
room_no INT,
FOREIGN KEY (room_no) REFERENCES Room(room_no)
)`;
await connection.query(createMeetingTable);
console.log('Meeting table created.');

// Insert data into Invitee table
const insertInvitee = `
INSERT INTO Invitee (invitee_name, invited_by)
VALUES
('John Doe', NULL), -- Assuming initial invitees are not invited by anyone
('Jane Doe', NULL),
('Alice Smith', NULL),
('Bob Johnson', NULL),
('Charlie Brown', NULL)`;
await connection.query(insertInvitee);
console.log('5 rows inserted into Invitee table.');

// Insert data into Room table
const insertRoom = `
INSERT INTO Room (room_name, floor_number)
VALUES
('Conference Room A', 1),
('Conference Room B', 1),
('Meeting Room C', 2),
('Board Room', 3),
('Lounge', 1)`;
await connection.query(insertRoom);
console.log('5 rows inserted into Room table.');

// Insert data into Meeting table
const insertMeeting = `
INSERT INTO Meeting (meeting_title, starting_time, ending_time, room_no)
VALUES
('Project Kickoff', '2024-10-15 09:00:00', '2024-10-15 10:00:00', 1),
('Weekly Sync', '2024-10-15 10:30:00', '2024-10-15 11:30:00', 2),
('Client Meeting', '2024-10-15 14:00:00', '2024-10-15 15:00:00', 3),
('Team Building', '2024-10-15 16:00:00', '2024-10-15 17:00:00', 4),
('Product Review', '2024-10-15 11:00:00', '2024-10-15 12:00:00', 5)`;
await connection.query(insertMeeting);
console.log('5 rows inserted into Meeting table.');

} catch (err) {
console.error('Error: ', err);
} finally {
// End the connection
await connection.end();
}
}

main();
70 changes: 70 additions & 0 deletions Week1/assignment/queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const mysql = require('mysql2');

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

// Connect to the database
connection.connect((err) => {
if (err) {
console.error('Error connecting to the database:', err.stack);
return;
}
console.log('Connected to the database.');
});

const queries = [
// 1. Countries with population greater than 8 million
`SELECT Name FROM country WHERE Population > 8000000`,

// 2. Countries that have "land" in their names
`SELECT Name FROM country WHERE Name LIKE '%land%'`,

// 3. Cities with population between 500,000 and 1 million
`SELECT Name FROM city WHERE Population BETWEEN 500000 AND 1000000`,

// 4. All countries in Europe
`SELECT Name FROM country WHERE Continent = 'Europe'`,

// 5. Countries in descending order of surface area
`SELECT Name FROM country ORDER BY SurfaceArea DESC`,

// 6. All cities in the Netherlands
`SELECT Name FROM city WHERE CountryCode = 'NLD'`,

// 7. Population of Rotterdam
`SELECT Population FROM city WHERE Name = 'Rotterdam'`,

// 8. Top 10 countries by surface area
`SELECT Name FROM country ORDER BY SurfaceArea DESC LIMIT 10`,

// 9. Top 10 most populated cities
`SELECT Name FROM city ORDER BY Population DESC LIMIT 10`,

// 10. Population number of the world
`SELECT SUM(Population) AS WorldPopulation FROM country`
];

// Execute all queries and log results
queries.forEach((query, index) => {
connection.query(query, (error, results) => {
if (error) {
console.error(`Error executing query ${index + 1}:`, error);
} else {
console.log(`Results of query ${index + 1}:`, results);
}
});
});

// Close the connection
connection.end((err) => {
if (err) {
console.error('Error closing the connection:', err);
} else {
console.log('Connection closed.');
}
});
62 changes: 62 additions & 0 deletions Week2/assignment/aggregate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const mysql = require('mysql2');

// Create a connection to the database
const connection = mysql.createConnection({
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
database: 'userdb'
});

// Query for all research papers and the number of authors that wrote each paper
const queryPapersAndAuthorsCount = `
SELECT rp.paper_title, COUNT(ap.author_id) AS number_of_authors
FROM research_papers rp
LEFT JOIN author_paper ap ON rp.paper_id = ap.paper_id
GROUP BY rp.paper_id;`;

// Query for sum of research papers published by all female authors
const queryFemalePapersCount = `
SELECT COUNT(DISTINCT rp.paper_id) AS total_female_papers
FROM research_papers rp
JOIN author_paper ap ON rp.paper_id = ap.paper_id
JOIN authors a ON ap.author_id = a.author_id
WHERE a.gender = 'Female';`;

// Query for average h-index of all authors per university
const queryAverageHIndex = `
SELECT university, AVG(h_index) AS average_h_index
FROM authors
GROUP BY university;`;

// Query for sum of research papers of authors per university
const queryTotalPapersPerUniversity = `
SELECT a.university, COUNT(DISTINCT rp.paper_id) AS total_papers
FROM authors a
JOIN author_paper ap ON a.author_id = ap.author_id
JOIN research_papers rp ON ap.paper_id = rp.paper_id
GROUP BY a.university;`;

// Query for minimum and maximum of h-index of all authors per university
const queryMinMaxHIndex = `
SELECT university, MIN(h_index) AS min_h_index, MAX(h_index) AS max_h_index
FROM authors
GROUP BY university;`;

// Function to execute a query and log the result
function executeQuery(query, logMessage) {
connection.query(query, (error, results) => {
if (error) throw error;
console.log(logMessage, results);
});
}

// Execute the queries
executeQuery(queryPapersAndAuthorsCount, 'Research papers and number of authors:');
executeQuery(queryFemalePapersCount, 'Total research papers by female authors:');
executeQuery(queryAverageHIndex, 'Average H-index per university:');
executeQuery(queryTotalPapersPerUniversity, 'Total research papers per university:');
executeQuery(queryMinMaxHIndex, 'Min and Max H-index per university:');

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

// Create a connection to the database
const connection = mysql.createConnection({
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
database: 'userdb'
});

// SQL command to create the authors table
const createAuthorsTable = `
CREATE TABLE authors (
author_id INT AUTO_INCREMENT PRIMARY KEY,
author_name VARCHAR(255) NOT NULL,
university VARCHAR(255),
date_of_birth DATE,
h_index INT,
gender ENUM('Male', 'Female', 'Other'),
mentor INT,
FOREIGN KEY (mentor) REFERENCES authors(author_id)
);`;

// Execute the SQL command
connection.query(createAuthorsTable, (error, results) => {
if (error) {
console.error('Error creating table:', error);
return;
}
console.log('Authors table created successfully.');
});

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

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

const createAuthorPaperTable = `
CREATE TABLE author_paper (
author_id INT,
paper_id INT,
FOREIGN KEY (author_id) REFERENCES authors(author_id),
FOREIGN KEY (paper_id) REFERENCES research_papers(paper_id),
PRIMARY KEY (author_id, paper_id)
);`;

connection.query(createAuthorPaperTable, (error, results) => {
if (error) {
console.error('Error creating author_paper table:', error);
return;
}
console.log('Author-paper association table created successfully.');
});

connection.end();
28 changes: 28 additions & 0 deletions Week2/assignment/createResearchPapersTable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const mysql = require('mysql2');

// Create a connection to the database
const connection = mysql.createConnection({
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
database: 'userdb'
});

const createResearchPapersTable = `
CREATE TABLE research_papers (
paper_id INT AUTO_INCREMENT PRIMARY KEY,
paper_title VARCHAR(255) NOT NULL,
conference VARCHAR(255),
publish_date DATE
);`;

connection.query(createResearchPapersTable, (error, results) => {
if (error) {
console.error('Error creating research_papers table:', error);
return;
}
console.log('Research papers table created successfully.');
});

// Close the connection
connection.end();
Loading