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

w2-aziz-databases #41

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Empty file added .vscode/settings.json
Empty file.
52 changes: 52 additions & 0 deletions Week2/Joins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const mysql = require("mysql2/promise");

async function executeQuery(connection, query) {
try {
const [rows] = await connection.query(query);
return rows;
} catch (error) {
console.error("Error executing query:", error);
}
}

async function main() {
const connection = await mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "meetup",
});

try {
const authorsWithMentorsQuery = `
SELECT a.author_name AS Author, m.author_name AS Mentor
FROM authors a
LEFT JOIN authors m ON a.mentor = m.author_id;
`;
const authorsWithMentors = await executeQuery(
connection,
authorsWithMentorsQuery
);
console.log("Authors and their Mentors:");
console.table(authorsWithMentors);

// 2. Query to get all columns of authors and their published paper titles
const authorsWithPapersQuery = `
SELECT a.*, p.paper_title
FROM authors a
LEFT JOIN research_papers p ON a.author_id = p.author_id;
`;
const authorsWithPapers = await executeQuery(
connection,
authorsWithPapersQuery
);
console.log("Authors and their Published Papers:");
console.table(authorsWithPapers);
} finally {
await connection.end();
console.log("Connection closed.");
}
}

// Run the main function`````````````
main();
132 changes: 132 additions & 0 deletions Week2/Relationships.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
const mysql = require("mysql2/promise");

async function executeQuery(connection, query) {
try {
await connection.query(query);
} catch (error) {
console.error("Error executing query:", error);
}
}

async function createAuthorsTable(connection) {
const createTableQuery = `
CREATE TABLE IF NOT EXISTS 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)
);
`;
await executeQuery(connection, createTableQuery);
console.log("Authors table created.");
}

async function createResearchPapersTable(connection) {
const createTableQuery = `
CREATE TABLE IF NOT EXISTS research_papers (
paper_id INT AUTO_INCREMENT PRIMARY KEY,
paper_title VARCHAR(255) NOT NULL,
conference VARCHAR(255),
publish_date DATE
);
`;
await executeQuery(connection, createTableQuery);
console.log("Research papers table created.");
}

async function createResearchPaperAuthorsTable(connection) {
const createTableQuery = `
CREATE TABLE IF NOT EXISTS research_paper_authors (
paper_id INT,
author_id INT,
PRIMARY KEY (paper_id, author_id),
FOREIGN KEY (paper_id) REFERENCES research_papers(paper_id) ON DELETE CASCADE,
FOREIGN KEY (author_id) REFERENCES authors(author_id) ON DELETE CASCADE
);
`;
await executeQuery(connection, createTableQuery);
console.log("Research paper authors table created.");
}

async function insertSampleData(connection) {
const authors = [
["Alice Smith", "University A", "1985-05-12", 15, "Female"],
["Bob Johnson", "University B", "1978-11-22", 20, "Male"],
["Carol Williams", "University C", "1990-02-17", 10, "Female"],
["David Brown", "University D", "1982-03-03", 25, "Male"],
["Eve Davis", "University E", "1989-07-25", 30, "Female"],
["Frank Miller", "University F", "1983-09-30", 12, "Male"],
["Grace Wilson", "University G", "1991-01-15", 8, "Female"],
["Hank Moore", "University H", "1975-12-05", 22, "Male"],
["Ivy Taylor", "University I", "1987-06-14", 18, "Female"],
["Jack Anderson", "University J", "1980-10-01", 16, "Male"],
["Kathy Thomas", "University K", "1992-04-21", 5, "Female"],
["Liam Jackson", "University L", "1986-08-11", 14, "Male"],
["Mia White", "University M", "1993-05-19", 7, "Female"],
["Noah Harris", "University N", "1984-03-12", 20, "Male"],
["Olivia Martin", "University O", "1990-11-30", 15, "Female"],
];

const insertAuthorsQuery = `
INSERT INTO authors (author_name, university, date_of_birth, h_index, gender)
VALUES ?;
`;

await connection.query(insertAuthorsQuery, [authors]);

// Insert research papers
const papers = [];
for (let i = 1; i <= 30; i++) {
papers.push([`Research Paper ${i}`, `Conference ${i}`, `2023-01-${i}`]);
}

const insertPapersQuery = `
INSERT INTO research_papers (paper_title, conference, publish_date)
VALUES ?;
`;

await connection.query(insertPapersQuery, [papers]);

// Insert into research_paper_authors table
const paperAuthors = [];
for (let i = 1; i <= 30; i++) {
const numAuthors = Math.floor(Math.random() * 3) + 1; // Random number of authors per paper (1 to 3)
for (let j = 0; j < numAuthors; j++) {
const authorId = Math.floor(Math.random() * 15) + 1; // Random author_id between 1 and 15
paperAuthors.push([i, authorId]);
}
}

const insertPaperAuthorsQuery = `
INSERT INTO research_paper_authors (paper_id, author_id)
VALUES ?;
`;
await connection.query(insertPaperAuthorsQuery, [paperAuthors]);

console.log("Sample data inserted.");
}

async function main() {
const connection = await mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "meetup",
});

try {
await createAuthorsTable(connection);
await createResearchPapersTable(connection);
await createResearchPaperAuthorsTable(connection);
await insertSampleData(connection);
} finally {
await connection.end();
console.log("Connection closed.");
}
}

main();
63 changes: 63 additions & 0 deletions Week2/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const mysql = require("mysql2/promise");

async function executeQuery(connection, query) {
try {
await connection.query(query);
} catch (error) {
console.error("Error executing query:", error);
}
}

async function main() {
const connection = await mysql.createConnection({
host: "localhost",
user: "hyfuser",
password: "hyfpassword",
database: "meetup",
});

try {
// Step 1: Create the authors table
const createTableQuery = `
CREATE TABLE IF NOT EXISTS 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')
);
`;
await executeQuery(connection, createTableQuery);
console.log("Authors table created.");

// Step 2: Check if the mentor column exists before adding it
const checkColumnQuery = `
SELECT COUNT(*) AS count
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'authors'
AND COLUMN_NAME = 'mentor';
`;
const [rows] = await connection.query(checkColumnQuery);

if (rows[0].count === 0) {
// Step 3: Add the mentor column with foreign key constraint
const addMentorColumnQuery = `
ALTER TABLE authors
ADD COLUMN mentor INT,
ADD CONSTRAINT fk_mentor
FOREIGN KEY (mentor) REFERENCES authors(author_id);
`;
await executeQuery(connection, addMentorColumnQuery);
console.log("Mentor column added with foreign key constraint.");
} else {
console.log("Mentor column already exists. Skipping addition.");
}
} finally {
await connection.end();
console.log("Connection closed.");
}
}

// Run the main function
main();
Loading