diff --git a/Week2/assignment/Ex2Relationships.js b/Week2/assignment/Ex2Relationships.js new file mode 100644 index 000000000..f505921a3 --- /dev/null +++ b/Week2/assignment/Ex2Relationships.js @@ -0,0 +1,51 @@ +import { Client } from "pg"; + +// Database connection configuration +const config = { + host: "localhost", + user: "hyfuser", + password: "hyfpassword", + database: "DBassignmentW2", + port: 5432, +}; + +const client = new Client(config); + +async function createTables(client) { + const CREATE_RESEARCH_PAPERS_TABLE = ` + CREATE TABLE IF NOT EXISTS research_papers ( + paper_id SERIAL PRIMARY KEY, + paper_title VARCHAR(50) NOT NULL, + conference VARCHAR(50), + publish_date DATE + )`; + + const CREATE_PAPER_AUTHORS_TABLE = ` + CREATE TABLE IF NOT EXISTS paper_authors ( + paper_id INTEGER NOT NULL, + author_id INTEGER NOT NULL, + PRIMARY KEY (paper_id, author_id), + CONSTRAINT fk_paper + FOREIGN KEY (paper_id) + REFERENCES research_papers(paper_id) + ON DELETE CASCADE, + CONSTRAINT fk_author + FOREIGN KEY (author_id) + REFERENCES authors(author_id) + ON DELETE CASCADE + )`; + + try { + await client.connect(); + console.log("Connected to PostgreSQL database!"); + + // Create tables + await client.query(CREATE_RESEARCH_PAPERS_TABLE); + await client.query(CREATE_PAPER_AUTHORS_TABLE); + } catch (error) { + console.error("Error creating tables:", error); + } finally { + await client.end(); + } +} +createTables(client); diff --git a/Week2/assignment/Ex3AddInfo&Joins.js b/Week2/assignment/Ex3AddInfo&Joins.js new file mode 100644 index 000000000..3a09bc2ee --- /dev/null +++ b/Week2/assignment/Ex3AddInfo&Joins.js @@ -0,0 +1,162 @@ +import { Client } from "pg"; +import { authors, researchPapers, paperAuthors } from "./generateSampleData.js"; + +// Database connection configuration +const config = { + host: "localhost", + user: "hyfuser", + password: "hyfpassword", + database: "DBassignmentW2", + port: 5432, +}; + +const client = new Client(config); +let queryRes; + +async function insertAuthors(client) { + try { + // Insert authors with mentors + for (let i = 0; i < authors.length; i++) { + const author = authors[i]; + // Set mentor to null for the first 5 authors (they will be mentors) + // Others will have a random mentor from the first 5 authors + const mentorId = i < 5 ? null : Math.floor(Math.random() * 5) + 1; + + const query = { + text: ` + INSERT INTO authors (author_name, university, date_of_birth, h_index, gender, mentor) + VALUES ($1, $2, $3, $4, $5, $6) + RETURNING author_id + `, + values: [ + author.name, + author.university, + author.dob, + author.hIndex, + author.gender, + mentorId, + ], + }; + + const result = await client.query(query); + console.log( + `Inserted author: ${author.name} with ID: ${result.rows[0].author_id}` + ); + } + console.log("All authors inserted successfully"); + } catch (error) { + console.error("Error inserting authors:", error); + throw error; + } +} + +async function insertResearchPapers(client) { + try { + for (const paper of researchPapers) { + const query = { + text: ` + INSERT INTO research_papers (paper_title, conference, publish_date) + VALUES ($1, $2, $3) + RETURNING paper_id + `, + values: [paper.title, paper.conference, paper.date], + }; + + const result = await client.query(query); + console.log( + `Inserted paper: ${paper.title} with ID: ${result.rows[0].paper_id}` + ); + } + console.log("All research papers inserted successfully"); + } catch (error) { + console.error("Error inserting research papers:", error); + throw error; + } +} + +async function insertPaperAuthors(client) { + try { + for (const paperAuthor of paperAuthors) { + const query = { + text: ` + INSERT INTO paper_authors (paper_id, author_id) + VALUES ($1, $2) + ON CONFLICT DO NOTHING + `, + values: [paperAuthor.paper_id, paperAuthor.authorId], + }; + + await client.query(query); + console.log( + `Linked paper ${paperAuthor.paper_id} with author ${paperAuthor.authorId}` + ); + } + console.log("All paper-author relationships inserted successfully"); + } catch (error) { + console.error("Error inserting paper-author relationships:", error); + throw error; + } +} + +async function seedDatabase(client) { + try { + await client.connect(); + console.log("Connected to PostgreSQL database!"); + + // Insert new data + await insertAuthors(client); + await insertResearchPapers(client); + await insertPaperAuthors(client); + + // Create a view for all authors and their corresponding mentors + const CREATE_AUTHOR_MENTOR_VIEW = ` + CREATE OR REPLACE VIEW author_mentor AS + SELECT + author_name, + mentor + FROM + authors + `; + + await client.query(CREATE_AUTHOR_MENTOR_VIEW); + // Query the view to verify it works + queryRes = await client.query("SELECT * FROM author_mentor"); + console.log( + "\nAuthors with their mentors:", + JSON.stringify(queryRes.rows, null, 2) + ); + + // Create a view for all the vegetarian recipes with potatoes + const CREATE_AUTHORS_PAPERS_VIEW = ` + CREATE OR REPLACE VIEW authors_papers AS + SELECT + a.author_name, + rp.paper_title + FROM + authors a + LEFT JOIN paper_authors pa ON a.author_id = pa.author_id + LEFT JOIN research_papers rp ON pa.paper_id = rp.paper_id + `; + + await client.query(CREATE_AUTHORS_PAPERS_VIEW); + // Query the view to verify it works + queryRes = await client.query("SELECT * FROM authors_papers"); + if (queryRes.rows.length !== 0) { + console.log( + "\nAuthors with their papers:", + JSON.stringify(queryRes.rows, null, 2) + ); + } else { + console.log("There are no papers in the database"); + } + + console.log("Database seeding completed successfully!"); + } catch (error) { + console.error("Error seeding database:", error); + } finally { + await client.end(); + } +} + +// Execute the seeding function +seedDatabase(client); diff --git a/Week2/assignment/Ex4AggregateFunctions.js b/Week2/assignment/Ex4AggregateFunctions.js new file mode 100644 index 000000000..feea863c4 --- /dev/null +++ b/Week2/assignment/Ex4AggregateFunctions.js @@ -0,0 +1,140 @@ +import { Client } from "pg"; +import { authors, researchPapers, paperAuthors } from "./generateSampleData.js"; + +// Database connection configuration +const config = { + host: "localhost", + user: "hyfuser", + password: "hyfpassword", + database: "DBassignmentW2", + port: 5432, +}; + +const client = new Client(config); +let queryRes; + +async function createDBviews(client) { + try { + await client.connect(); + console.log("Connected to PostgreSQL database!"); + + // Create a view for all research papers and the number of authors that wrote that paper. + const CREATE_AUTHOR_QUANTITY_VIEW = ` + CREATE OR REPLACE VIEW paper_authors_qty AS + SELECT + paper_title, + COUNT(pa.author_id) as number_of_authors + FROM + research_papers rp + JOIN paper_authors pa ON rp.paper_id = pa.paper_id + GROUP BY paper_title + `; + + await client.query(CREATE_AUTHOR_QUANTITY_VIEW); + + queryRes = await client.query("SELECT * FROM paper_authors_qty"); + console.log( + "\nResearch papers and the number of their authors:", + JSON.stringify(queryRes.rows, null, 2) + ); + + // Create a view for the sum of the research papers published by all female authors. + const CREATE_FEMALE_PAPER_NUMBER_VIEW = ` + CREATE OR REPLACE VIEW female_paper_number AS + SELECT + COUNT(DISTINCT rp.paper_id) as publications_sum + FROM + research_papers rp + JOIN paper_authors pa ON rp.paper_id = pa.paper_id + JOIN authors a ON a.author_id = pa.author_id + WHERE a.gender = 'Female' + `; + + await client.query(CREATE_FEMALE_PAPER_NUMBER_VIEW); + + queryRes = await client.query("SELECT * FROM female_paper_number"); + console.log( + "\nThe sum of the research papers published by all female authors:", + JSON.stringify(queryRes.rows, null, 2) + ); + + // Create a view for Average of the h-index of all authors per university. + const CREATE_HINDEX_VIEW = ` + CREATE OR REPLACE VIEW h_index AS + SELECT + university, + AVG(h_index) as average_h_index + FROM authors + GROUP BY university + `; + + await client.query(CREATE_HINDEX_VIEW); + queryRes = await client.query("SELECT * FROM h_index"); + console.log( + "\nThe average of the h-index of all authors per university:", + JSON.stringify(queryRes.rows, null, 2) + ); + + // Create a view for the sum of the research papers per university. + const CREATE_PAPERS_PER_UNIVERSITY_VIEW = ` + CREATE OR REPLACE VIEW papers_per_university AS + SELECT + a.university, + COUNT(DISTINCT rp.paper_id) as university_publications + FROM authors a + JOIN paper_authors pa ON pa.author_id = a.author_id + JOIN research_papers rp ON rp.paper_id = pa.paper_id + GROUP BY a.university + `; + + await client.query(CREATE_PAPERS_PER_UNIVERSITY_VIEW); + queryRes = await client.query("SELECT * FROM papers_per_university"); + console.log( + "\nThe sum of the research papers per university:", + JSON.stringify(queryRes.rows, null, 2) + ); + + // Create a view for minimum of the h-index of all authors per university. + const CREATE_HINDEX_MIN_VIEW = ` + CREATE OR REPLACE VIEW h_index_min AS + SELECT + university, + MIN(h_index) as min_h_index + FROM authors + GROUP BY university + `; + + await client.query(CREATE_HINDEX_MIN_VIEW); + queryRes = await client.query("SELECT * FROM h_index_min"); + console.log( + "\nThe minimum of the h-index of all authors per university:", + JSON.stringify(queryRes.rows, null, 2) + ); + + // Create a view for maximum of the h-index of all authors per university. + const CREATE_HINDEX_MAX_VIEW = ` + CREATE OR REPLACE VIEW h_index_max AS + SELECT + university, + MAX(h_index) as max_h_index + FROM authors + GROUP BY university + `; + + await client.query(CREATE_HINDEX_MAX_VIEW); + queryRes = await client.query("SELECT * FROM h_index_max"); + console.log( + "\nThe maximum of the h-index of all authors per university:", + JSON.stringify(queryRes.rows, null, 2) + ); + + console.log("Database seeding completed successfully!"); + } catch (error) { + console.error("Error seeding database:", error); + } finally { + await client.end(); + } +} + +// Execute the seeding function +createDBviews(client); diff --git a/Week2/assignment/ex1keys.js b/Week2/assignment/ex1keys.js new file mode 100644 index 000000000..38e32650c --- /dev/null +++ b/Week2/assignment/ex1keys.js @@ -0,0 +1,50 @@ +import { Client } from "pg"; + +// Database connection configuration +const config = { + host: "localhost", + user: "hyfuser", + password: "hyfpassword", + database: "DBassignmentW2", + port: 5432, +}; + +const client = new Client(config); + +async function createTables(client) { + const CREATE_GENDER_TYPE = ` + CREATE TYPE GENDER AS ENUM ('Male', 'Female', 'X'); + `; + + const CREATE_AUTHORS_TABLE = ` + CREATE TABLE IF NOT EXISTS authors ( + author_id SERIAL PRIMARY KEY, + author_name VARCHAR(50) NOT NULL, + university VARCHAR(50), + date_of_birth DATE, + h_index SMALLINT, + gender GENDER + )`; + + const ALTER_AUTHORS_TABLE = ` + ALTER TABLE authors + ADD COLUMN IF NOT EXISTS mentor INTEGER, + ADD CONSTRAINT fk_mentor FOREIGN KEY (mentor) REFERENCES authors(author_id) ON DELETE CASCADE + `; + + try { + await client.connect(); + console.log("Connected to PostgreSQL database!"); + + // Create types and tables + await client.query(CREATE_GENDER_TYPE); + await client.query(CREATE_AUTHORS_TABLE); + await client.query(ALTER_AUTHORS_TABLE); + } catch (error) { + console.error("Error creating tables:", error); + } finally { + await client.end(); + } +} + +createTables(client); diff --git a/Week2/assignment/generateSampleData.js b/Week2/assignment/generateSampleData.js new file mode 100644 index 000000000..fe4c2ca4c --- /dev/null +++ b/Week2/assignment/generateSampleData.js @@ -0,0 +1,417 @@ +// Sample data for authors +const authors = [ + { + name: "John Smith", + university: "MIT", + dob: "1980-05-15", + hIndex: 42, + gender: "Male", + }, + { + name: "Emily Johnson", + university: "Stanford", + dob: "1975-08-22", + hIndex: 37, + gender: "Female", + }, + { + name: "Michael Brown", + university: "Harvard", + dob: "1985-03-10", + hIndex: 29, + gender: "Male", + }, + { + name: "Sarah Davis", + university: "Caltech", + dob: "1978-11-05", + hIndex: 35, + gender: "Female", + }, + { + name: "David Wilson", + university: "Oxford", + dob: "1972-07-19", + hIndex: 51, + gender: "Male", + }, + { + name: "Jennifer Lee", + university: "Cambridge", + dob: "1982-09-30", + hIndex: 31, + gender: "Female", + }, + { + name: "Robert Taylor", + university: "ETH Zurich", + dob: "1970-12-25", + hIndex: 48, + gender: "Male", + }, + { + name: "Lisa Anderson", + university: "UCLA", + dob: "1988-04-12", + hIndex: 25, + gender: "Female", + }, + { + name: "James Martin", + university: "UCLA", + dob: "1983-06-18", + hIndex: 33, + gender: "Male", + }, + { + name: "Patricia White", + university: "UC Berkeley", + dob: "1976-02-28", + hIndex: 39, + gender: "Female", + }, + { + name: "Daniel Clark", + university: "University of Tokyo", + dob: "1981-10-14", + hIndex: 28, + gender: "Male", + }, + { + name: "Elizabeth Lewis", + university: "EPFL", + dob: "1979-07-23", + hIndex: 36, + gender: "Female", + }, + { + name: "Thomas Walker", + university: "MIT", + dob: "1974-01-09", + hIndex: 44, + gender: "Male", + }, + { + name: "Nancy Hall", + university: "MIT", + dob: "1984-08-07", + hIndex: 27, + gender: "Female", + }, + { + name: "Charles Allen", + university: "MIT", + dob: "1977-11-16", + hIndex: 32, + gender: "Male", + }, +]; + +// Sample data for research papers +const researchPapers = [ + { + paper_id: 1, + title: "Advancements in Quantum Computing", + conference: "QIP 2023", + date: "2023-01-15", + }, + { + paper_id: 2, + title: "Machine Learning in Healthcare", + conference: "NeurIPS 2022", + date: "2022-12-10", + }, + { + paper_id: 3, + title: "The Future of Renewable Energy", + conference: "Nature Energy", + date: "2023-02-20", + }, + { + paper_id: 4, + title: "Blockchain Technology and Security", + conference: "IEEE S&P 2023", + date: "2023-03-05", + }, + { + paper_id: 5, + title: "Neural Networks for Image Recognition", + conference: "CVPR 2023", + date: "2023-06-15", + }, + { + paper_id: 6, + title: "Climate Change Modeling", + conference: "Nature Climate", + date: "2023-01-30", + }, + { + paper_id: 7, + title: "Cybersecurity in IoT", + conference: "USENIX Security 2023", + date: "2023-04-12", + }, + { + paper_id: 8, + title: "Artificial General Intelligence", + conference: "ICML 2023", + date: "2023-07-22", + }, + { + paper_id: 9, + title: "Sustainable Urban Development", + conference: "Nature Cities", + date: "2023-02-28", + }, + { + paper_id: 10, + title: "Quantum Cryptography", + conference: "CRYPTO 2023", + date: "2023-08-10", + }, + { + paper_id: 11, + title: "Deep Learning for Drug Discovery", + conference: "Nature Medicine", + date: "2023-03-15", + }, + { + paper_id: 12, + title: "Autonomous Vehicles", + conference: "ICRA 2023", + date: "2023-05-20", + }, + { + paper_id: 13, + title: "Natural Language Processing", + conference: "ACL 2023", + date: "2023-07-30", + }, + { + paper_id: 14, + title: "Robotics in Manufacturing", + conference: "IROS 2023", + date: "2023-09-12", + }, + { + paper_id: 15, + title: "Augmented Reality Applications", + conference: "UIST 2023", + date: "2023-10-05", + }, + { + paper_id: 16, + title: "Edge Computing", + conference: "MobiCom 2023", + date: "2023-04-25", + }, + { + paper_id: 17, + title: "5G Network Security", + conference: "NDSS 2023", + date: "2023-06-30", + }, + { + paper_id: 18, + title: "Biomedical Engineering", + conference: "Nature Biomedical", + date: "2023-03-10", + }, + { + paper_id: 19, + title: "Computer Vision in Agriculture", + conference: "CVPR 2023", + date: "2023-06-20", + }, + { + paper_id: 20, + title: "AI Ethics and Bias", + conference: "FAT* 2023", + date: "2023-01-20", + }, + { + paper_id: 21, + title: "Renewable Energy Storage", + conference: "Nature Energy", + date: "2023-02-15", + }, + { + paper_id: 22, + title: "Distributed Systems", + conference: "SOSP 2023", + date: "2023-10-15", + }, + { + paper_id: 23, + title: "Human-Computer Interaction", + conference: "CHI 2023", + date: "2023-04-30", + }, + { + paper_id: 24, + title: "Data Privacy", + conference: "PETS 2023", + date: "2023-05-10", + }, + { + paper_id: 25, + title: "Computational Biology", + conference: "RECOMB 2023", + date: "2023-07-05", + }, + { + paper_id: 26, + title: "Autonomous Drones", + conference: "ICRA 2023", + date: "2023-05-25", + }, + { + paper_id: 27, + title: "Natural Language Generation", + conference: "EMNLP 2023", + date: "2023-11-10", + }, + { + paper_id: 28, + title: "Robotic Process Automation", + conference: "ICAPS 2023", + date: "2023-08-20", + }, + { + paper_id: 29, + title: "Extended Reality in Education", + conference: "CHI 2023", + date: "2023-04-15", + }, + { + paper_id: 30, + title: "Federated Learning", + conference: "ICLR 2023", + date: "2023-09-25", + }, +]; + +// Junction table data for paper-authors relationship +const paperAuthors = [ + { paper_id: 1, authorId: 1 }, + { paper_id: 1, authorId: 3 }, + { paper_id: 1, authorId: 5 }, + { paper_id: 2, authorId: 2 }, + { paper_id: 2, authorId: 4 }, + { paper_id: 2, authorId: 6 }, + { paper_id: 2, authorId: 8 }, + { paper_id: 3, authorId: 3 }, + { paper_id: 3, authorId: 5 }, + { paper_id: 3, authorId: 7 }, + { paper_id: 3, authorId: 9 }, + { paper_id: 3, authorId: 11 }, + { paper_id: 4, authorId: 4 }, + { paper_id: 4, authorId: 6 }, + { paper_id: 4, authorId: 8 }, + { paper_id: 5, authorId: 5 }, + { paper_id: 5, authorId: 7 }, + { paper_id: 5, authorId: 9 }, + { paper_id: 5, authorId: 11 }, + { paper_id: 6, authorId: 6 }, + { paper_id: 6, authorId: 8 }, + { paper_id: 6, authorId: 10 }, + { paper_id: 7, authorId: 7 }, + { paper_id: 7, authorId: 9 }, + { paper_id: 7, authorId: 11 }, + { paper_id: 7, authorId: 13 }, + { paper_id: 8, authorId: 8 }, + { paper_id: 8, authorId: 10 }, + { paper_id: 8, authorId: 12 }, + { paper_id: 9, authorId: 9 }, + { paper_id: 9, authorId: 11 }, + { paper_id: 9, authorId: 13 }, + { paper_id: 9, authorId: 15 }, + { paper_id: 10, authorId: 10 }, + { paper_id: 10, authorId: 12 }, + { paper_id: 10, authorId: 15 }, + { paper_id: 11, authorId: 11 }, + { paper_id: 11, authorId: 13 }, + { paper_id: 11, authorId: 2 }, + { paper_id: 12, authorId: 12 }, + { paper_id: 12, authorId: 15 }, + { paper_id: 12, authorId: 3 }, + { paper_id: 12, authorId: 5 }, + { paper_id: 13, authorId: 13 }, + { paper_id: 13, authorId: 1 }, + { paper_id: 13, authorId: 3 }, + { paper_id: 13, authorId: 5 }, + { paper_id: 13, authorId: 7 }, + { paper_id: 14, authorId: 1 }, + { paper_id: 14, authorId: 2 }, + { paper_id: 14, authorId: 3 }, + { paper_id: 15, authorId: 2 }, + { paper_id: 15, authorId: 4 }, + { paper_id: 15, authorId: 6 }, + { paper_id: 15, authorId: 8 }, + { paper_id: 16, authorId: 3 }, + { paper_id: 16, authorId: 5 }, + { paper_id: 16, authorId: 7 }, + { paper_id: 16, authorId: 9 }, + { paper_id: 16, authorId: 11 }, + { paper_id: 17, authorId: 4 }, + { paper_id: 17, authorId: 6 }, + { paper_id: 17, authorId: 8 }, + { paper_id: 18, authorId: 5 }, + { paper_id: 18, authorId: 7 }, + { paper_id: 18, authorId: 9 }, + { paper_id: 18, authorId: 11 }, + { paper_id: 19, authorId: 6 }, + { paper_id: 19, authorId: 8 }, + { paper_id: 19, authorId: 10 }, + { paper_id: 19, authorId: 12 }, + { paper_id: 20, authorId: 7 }, + { paper_id: 20, authorId: 9 }, + { paper_id: 20, authorId: 11 }, + { paper_id: 20, authorId: 13 }, + { paper_id: 20, authorId: 15 }, + { paper_id: 21, authorId: 8 }, + { paper_id: 21, authorId: 10 }, + { paper_id: 21, authorId: 12 }, + { paper_id: 22, authorId: 9 }, + { paper_id: 22, authorId: 11 }, + { paper_id: 22, authorId: 13 }, + { paper_id: 22, authorId: 15 }, + { paper_id: 23, authorId: 10 }, + { paper_id: 23, authorId: 12 }, + { paper_id: 23, authorId: 1 }, + { paper_id: 23, authorId: 3 }, + { paper_id: 24, authorId: 11 }, + { paper_id: 24, authorId: 13 }, + { paper_id: 24, authorId: 2 }, + { paper_id: 24, authorId: 4 }, + { paper_id: 24, authorId: 6 }, + { paper_id: 25, authorId: 12 }, + { paper_id: 25, authorId: 15 }, + { paper_id: 25, authorId: 3 }, + { paper_id: 25, authorId: 5 }, + { paper_id: 26, authorId: 13 }, + { paper_id: 26, authorId: 1 }, + { paper_id: 26, authorId: 4 }, + { paper_id: 26, authorId: 6 }, + { paper_id: 26, authorId: 8 }, + { paper_id: 27, authorId: 1 }, + { paper_id: 27, authorId: 2 }, + { paper_id: 27, authorId: 5 }, + { paper_id: 27, authorId: 7 }, + { paper_id: 28, authorId: 2 }, + { paper_id: 28, authorId: 3 }, + { paper_id: 28, authorId: 6 }, + { paper_id: 28, authorId: 8 }, + { paper_id: 28, authorId: 10 }, + { paper_id: 29, authorId: 3 }, + { paper_id: 29, authorId: 4 }, + { paper_id: 29, authorId: 7 }, + { paper_id: 29, authorId: 9 }, + { paper_id: 30, authorId: 4 }, + { paper_id: 30, authorId: 5 }, + { paper_id: 30, authorId: 8 }, + { paper_id: 30, authorId: 10 }, + { paper_id: 30, authorId: 12 }, +]; + +module.exports = { authors, researchPapers, paperAuthors };