forked from HackYourFuture/databases
-
Notifications
You must be signed in to change notification settings - Fork 9
Majd hussein w2 database #17
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
Open
Majoodeh
wants to merge
2
commits into
HackYourAssignment:main
Choose a base branch
from
Majoodeh:MAJD_HUSSEIN-w2=-database
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .env | ||
| ex1_explantion.js | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import pg from 'pg'; | ||
| import { Client } from 'pg'; | ||
| import dotenv from 'dotenv'; | ||
| dotenv.config(); | ||
|
|
||
| const password = String(process.env.databasePassword); | ||
|
|
||
| // connection configuration | ||
| const config = { | ||
| user: 'hyfuser', | ||
| host: 'localhost', | ||
| database: 'postgres', | ||
| password: password, | ||
| port: 5433, | ||
| }; | ||
|
|
||
| async function runQueries() { | ||
| // code will go here | ||
| const client = new pg.Client(config); | ||
|
|
||
| try { | ||
| await client.connect(); | ||
|
|
||
| // create the database | ||
| await client.query('CREATE DATABASE meetup;'); | ||
| console.log('Database " meetup " created.'); | ||
| } catch (error) { | ||
| console.error('Error during database creation:', error.message); | ||
| } | ||
| } | ||
|
|
||
| runQueries(); | ||
|
|
||
| const meetupConfig = { | ||
| user: 'hyfuser', | ||
| host: 'localhost', | ||
| database: 'meetup', | ||
| password: password, | ||
| port: 5433, | ||
| }; | ||
|
|
||
| const meetupClient = new pg.Client(meetupConfig); | ||
|
|
||
| async function runMeetupQueries() { | ||
| try { | ||
| await meetupClient.connect(); | ||
| console.log('Connected to the " meetup " database.'); | ||
|
|
||
| // create tables | ||
|
|
||
| // INVITEE TABLE | ||
| //3.Create a table called Invitee with the following fields (invitee_no, invitee_name and invited_by) | ||
|
|
||
| const createInviteeTable = ` | ||
| CREATE TABLE Invitee ( | ||
| invitee_no SERIAL PRIMARY KEY, | ||
| invitee_name VARCHAR(100) NOT NULL, | ||
| invited_by VARCHAR(100) NOT NULL | ||
| );`; | ||
| await meetupClient.query(createInviteeTable); | ||
| console.log('Table Invitee created.'); | ||
|
|
||
| // ROOM TABLE | ||
| //4.Create a table called Room with the following fields (room_no, room_name and floor_number) | ||
|
|
||
| const createRoomTable = ` | ||
| CREATE TABLE room ( | ||
| room_no SERIAL PRIMARY KEY, | ||
| room_name VARCHAR(100) NOT NULL, | ||
| floor_number INTEGER );`; | ||
| await meetupClient.query(createRoomTable); | ||
| console.log('Table room created.'); | ||
|
|
||
| // MEETING TABLE | ||
| //5.Create a table called Meeting with the following fields (meeting_no, meeting_title, starting_time, ending_time ,room_no) | ||
|
|
||
| const creatMeetingTable = ` | ||
| CREATE TABLE Meeting ( | ||
| meeting_no SERIAL PRIMARY KEY, | ||
| meeting_title VARCHAR(100) NOT NULL, | ||
| starting_time TIMESTAMP NOT NULL, | ||
| ending_time TIMESTAMP NOT NULL, | ||
| room_no INTEGER | ||
| );`; | ||
| await meetupClient.query(creatMeetingTable); | ||
| console.log('Table Meeting created.'); | ||
|
|
||
| // Data Insertion | ||
| // 6- Insert 5 rows into each table with relevant fields. | ||
| // Room data: | ||
| const insertRooms = [ | ||
| // [room_name , floor_number] | ||
| ['Room A', 1], | ||
| ['Room B', 2], | ||
| ['Room C', 3], | ||
| ['Room D', 4], | ||
| ['Room E', 5], | ||
| ]; | ||
| for (const [room_name, floor_number] of insertRooms) { | ||
| await meetupClient.query( | ||
| 'INSERT INTO room (room_name, floor_number) VALUES ($1, $2);', | ||
| [room_name, floor_number] | ||
| ); | ||
| } | ||
| console.log('Inserted 5 rows into Room table.'); | ||
|
|
||
| // Invitee data: | ||
| const insertInvitees = [ | ||
| // [invitee_name , invited_by] | ||
| ['Sanne', 'Jeroen'], | ||
| ['Femke', 'Thijs'], | ||
| ['Lotte', 'Daan'], | ||
| ['Maud', 'Luuk'], | ||
| ['Noa', 'Tijs'], | ||
| ]; | ||
| for (const [invitee_name, invited_by] of insertInvitees) { | ||
| await meetupClient.query( | ||
| `INSERT INTO Invitee (invitee_name, invited_by) VALUES ($1, $2);`, | ||
| [invitee_name, invited_by] | ||
| ); | ||
| } | ||
| console.log('Inserted 5 rows into Invitee table.'); | ||
| // Meeting data: | ||
| const insertMeetings = [ | ||
| // [meeting_title, starting_time, ending_time, room_no] | ||
| ['Procrastination Workshop', '2024-07-01 10:00', '2024-07-01 11:00', 1], | ||
| ['Advanced Couch Sitting', '2024-07-02 14:00', '2024-07-02 15:30', 2], | ||
| ['Nap Optimization Meeting', '2024-07-03 09:00', '2024-07-03 10:30', 3], | ||
| ['Email Ignoring Strategies', '2024-07-04 16:00', '2024-07-04 17:00', 4], | ||
| [ | ||
| 'Motivation Recovery Session', | ||
| '2024-07-05 11:00', | ||
| '2024-07-05 12:00', | ||
| 5, | ||
| ], | ||
| ]; | ||
| for (const [ | ||
| meeting_title, | ||
| starting_time, | ||
| ending_time, | ||
| room_no, | ||
| ] of insertMeetings) { | ||
| await meetupClient.query( | ||
| `INSERT INTO Meeting(meeting_title, starting_time, ending_time, room_no) VALUES ($1, $2, $3, $4);`, | ||
| [meeting_title, starting_time, ending_time, room_no] | ||
| ); | ||
| console.log('Inserted 5 rows into Meeting table.'); | ||
|
|
||
| console.log('Tables creation and data insertion completed.'); | ||
| } | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error connecting to the " meetup " database:', | ||
| error.message | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| runMeetupQueries(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import dotenv from 'dotenv'; | ||
| import { Client } from 'pg'; | ||
| import pg from 'pg'; | ||
| dotenv.config(); | ||
|
|
||
| const password = String(process.env.databasePassword); | ||
|
|
||
| const config = { | ||
| user: 'hyfuser', | ||
| host: 'localhost', | ||
| database: 'world', | ||
| password: password, | ||
| port: 5433, | ||
| }; | ||
|
|
||
| async function worldDatabase() { | ||
| const client = new pg.Client(config); | ||
| // function to execute query | ||
| async function executeQuery(client, query) { | ||
| const result = await client.query(query); | ||
| console.log(result.rows); | ||
| return result.rows; | ||
| } | ||
|
|
||
| try { | ||
| await client.connect(); | ||
| console.log('Connected to {WORLD} database.'); | ||
|
|
||
| //1.What are the names of countries with population greater than 8 million? | ||
| const queryPopulation = `SELECT Name FROM country WHERE Population > 8000000 ;`; | ||
| await executeQuery(client, queryPopulation); | ||
|
|
||
| // 2.What are the names of countries that have "land" in their names? | ||
| const queryLand = `SELECT Name FROM country WHERE Name LIKE '%land%';`; | ||
| await executeQuery(client, queryLand); | ||
|
|
||
| //3. What are the names of the cities with population in between 500,000 and 1 million? | ||
| const queryCityPopulation = ` SELECT Name, Population FROM city WHERE Population BETWEEN 500000 AND 1000000;`; | ||
| await executeQuery(client, queryCityPopulation); | ||
|
|
||
| //4. What's the name of all the countries on the continent 'Europe'? | ||
| const queryEurope = `SELECT Name FROM country WHERE Continent = 'Europe';`; | ||
| await executeQuery(client, queryEurope); | ||
|
|
||
| //5. List all the countries in the descending order of their surface area. | ||
| const querySurfaceArea = `SELECT Name, SurfaceArea From country ORDER BY SurfaceArea DESC;`; | ||
| await executeQuery(client, querySurfaceArea); | ||
|
|
||
| //6. What are the names of all the cities in the Netherlands? | ||
| const queryNetherlands = ` SELECT Name FROM city WHERE CountryCode = 'NLD';`; | ||
| await executeQuery(client, queryNetherlands); | ||
|
|
||
| //7. What is the total population of the Rotterdam ? | ||
| const queryRotterdam = ` SELECT Population FROM city WHERE Name = 'Rotterdam';`; | ||
| await executeQuery(client, queryRotterdam); | ||
|
|
||
| //8. What's the top 10 countries by Surface Area? | ||
| const query10SurfaceArea = `SELECT Name, SurfaceArea FROM country ORDER BY SurfaceArea DESC LIMIT 10;`; | ||
| await executeQuery(client, query10SurfaceArea); | ||
|
|
||
| //9. What's the top 10 most populated cities? | ||
| const query10PopulatedCities = `SELECT Name, Population FROM city ORDER BY Population DESC LIMIT 10;`; | ||
| await executeQuery(client, query10PopulatedCities); | ||
|
|
||
| //10. What is the population of the world? | ||
| const queryWorldPopulation = ` SELECT SUM(Population ) AS WorldPopulation FROM country;`; | ||
| await executeQuery(client, queryWorldPopulation); | ||
| } catch (error) { | ||
| console.error('Connection error', error.message); | ||
| } | ||
| } | ||
|
|
||
| worldDatabase(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| const authorsTable = `CREATE TABLE author ( | ||
| author_id INT PRIMARY KEY, | ||
| author_name VARCHAR(50) NOT NULL, | ||
| university VARCHAR(50), | ||
| date_of_birth DATE, | ||
| h_index INT, | ||
| gender VARCHAR(10));`; | ||
|
|
||
| authorsTable; | ||
|
|
||
| const updateAuthorTable = ` ALTER TABLE author( | ||
| ADD CONSTRAINT mentor INT, | ||
| FOREIGN KEY (mentor) REFERENCES author(author_id));`; | ||
|
|
||
| updateAuthorTable; | ||
|
|
||
| export { authorsTable, updateAuthorTable }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { authorsTable, updateAuthorTable } from './ex1.js'; | ||
|
|
||
| const researchPapers = ` CREATE TABLE research_Papers( | ||
| paper_id INT PRIMARY KEY, | ||
| paper_title VARCHAR(50) NOT NULL, | ||
| conference VARCHAR(50), | ||
| publish_date DATE, | ||
|
|
||
| ) `; | ||
|
|
||
| researchPapers; | ||
|
|
||
| const authorsPapers = ` CREATE TABLE authors_papers( | ||
| author_id INT NOT NULL, | ||
| paper_id INT NOT NULL, | ||
| PRIMARY KEY (author_id, paper_id), | ||
| FOREIGN KEY (author_id) REFERENCES authorsTable(author_id), | ||
| FOREIGN KEY (paper_id) REFERENCES research_Papers(paper_id)`; | ||
|
Comment on lines
+13
to
+18
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. nice implementation of the many to many |
||
|
|
||
| authorsPapers; | ||
|
|
||
| //15 authors data insert | ||
|
|
||
| const insertIntoAuthors = `INSERT INTO authorsTable(author_id, author_name, university, date_of_birth, h_index, gender, mentor) VALUES | ||
| (1, 'Alice Smith', 'University A', '1980-05-15', 25, 'Female',1), | ||
| (2, 'Bob Johnson', 'University B', '1975-09-23', 30, 'Male', 2), | ||
| (3, 'Carol Williams', 'University C', '1988-12-02', 15, 'Female',5), | ||
| (4, 'David Brown', 'University D', '1990-07-19', 20, 'Male',9), | ||
| (5, 'Eva Davis', 'University E', '1982-11-30', 28, 'Female',3), | ||
| (5, 'Eva Davis', 'University E', '1982-11-30', 28, 'Female',9); | ||
| (6, 'Frank Miller', 'University F', '1978-03-14', 22, 'Male',4), | ||
| (7, 'Grace Wilson', 'University G', '1985-08-25', 18, 'Female',7), | ||
| (8, 'Henry Moore', 'University H', '1992-01-10', 12, 'Male',8), | ||
| (9, 'Ivy Taylor', 'University I', '1983-04-05', 27, 'Female',9), | ||
| (10, 'Jack Anderson', 'University J', '1979-10-17', 35, 'Male',10), | ||
| (11, 'Kathy Thomas', 'University K', '1987-06-22', 16, 'Female',11), | ||
| (12, 'Larry Jackson', 'University L', '1991-02-28', 14, 'Male',3), | ||
| (13, 'Mona White', 'University M', '1984-09-09', 24, 'Female',2), | ||
| (14, 'Nate Harris', 'University N', '1976-12-15', 29, 'Male',5), | ||
| (15, 'Olivia Martin', 'University O', '1989-03-03', 19, 'Female',1);`; | ||
|
|
||
| const insertINtoResearchPapers = `INSERT INTO researchPapers(paper_id,paper_title,conference,publish_date) VALUES | ||
| (100, 'Advances in AI', 'AI Conference 2020', '2020-06-15'), | ||
| (200, 'Quantum Computing Basics', 'Quantum Tech 2019', '2019-11-20'), | ||
| (300, 'Blockchain Innovations', 'Blockchain Summit 2021', '2021-03-10'), | ||
| (400, 'Cybersecurity Trends', 'CyberSec Expo 2018', '2018-09-05'), | ||
| (500, 'Data Science Applications', 'DataCon 2020', '2020-12-01'), | ||
| (600, 'Machine Learning Techniques', 'ML Symposium 2019', '2019-05-22'), | ||
| (700, 'Cloud Computing Advances', 'CloudConf 2021', '2021-07-30'), | ||
| (800, 'IoT Developments', 'IoT World 2018', '2018-04-18'), | ||
| (900, 'AR/VR Technologies', 'TechVision 2020', '2020-10-12'), | ||
| (1000, '5G Network Evolution', 'NetTech 2019', '2019-08-27'), | ||
| (1100, 'Big Data Analytics', 'Data Summit 2021', '2021-02-14'), | ||
| (1200, 'Edge Computing', 'EdgeCon 2018', '2018-11-03'), | ||
| (1300, 'DevOps Best Practices', 'DevOps Days 2020', '2020-05-19'), | ||
| (1400, 'Software Engineering Trends', 'SoftEng Conf 2019', '2019-03-29'), | ||
| (1500, 'Digital Transformation', 'DigiTrans 2021', '2021-09-07'), | ||
| (1600, 'AI in Healthcare', 'HealthTech 2020', '2020-01-23'), | ||
| (1700, 'Robotics Innovations', 'RoboCon 2019', '2019-12-11'), | ||
| (1800, 'Sustainable Tech', 'GreenTech 2021', '2021-06-16'), | ||
| (1900, 'Virtual Collaboration Tools', 'CollabConf 2018', '2018-02-08'), | ||
| (2000, 'Autonomous Vehicles', 'AutoTech 2020', '2020-08-29'), | ||
| (2100, 'Natural Language Processing', 'NLP Summit 2019', '2019-04-14'), | ||
| (2200, 'Quantum Cryptography', 'QuantumSec 2021', '2021-10-21'), | ||
| (2300, 'Wearable Technologies', 'WearTech 2018', '2018-07-13'), | ||
| (2400, 'Smart Cities', 'UrbanTech 2020', '2020-11-05'), | ||
| (2500, 'Digital Twins', 'DT Conference 2019', '2019-09-30'), | ||
| (2600, 'AI Ethics', 'Ethics in Tech 2021', '2021-01-18'), | ||
| (2700, '5G and IoT Integration', 'NetIoT 2020', '2020-03-27'), | ||
| (2800, 'Cloud Security', 'CloudSec 2019', '2019-06-09'), | ||
| (2900, 'Data Privacy', 'PrivacyCon 2021', '2021-08-15'), | ||
| (3000, 'Future of Work', 'WorkTech 2018', '2018-10-22');`; | ||
|
|
||
| const insertIntoAuthorsPapers = `INSERT INTO authors_papers(author_id,paper_id) VALUES | ||
| (1, 100), | ||
| (2, 200), | ||
| (3, 300), | ||
| (4, 400), | ||
| (5, 500), | ||
| (6, 600), | ||
| (7, 700), | ||
| (8, 800), | ||
| (9, 900), | ||
| (10, 1000), | ||
| (11, 1100), | ||
| (12, 1200), | ||
| (13, 1300), | ||
| (14, 1400), | ||
| (15, 1500), | ||
| (1, 1600), | ||
| (2, 1700), | ||
| (3, 1800), | ||
| (4, 1900), | ||
| (5, 2000), | ||
| (6, 2100), | ||
| (7, 2200),`; | ||
|
|
||
| export { | ||
| researchPapers, | ||
| authorsPapers, | ||
| insertIntoAuthors, | ||
| insertINtoResearchPapers, | ||
| insertIntoAuthorsPapers, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { authorsTable, updateAuthorTable } from './ex1.js'; | ||
| import { | ||
| researchPapers, | ||
| authorsPapers, | ||
| insertIntoAuthors, | ||
| insertINtoResearchPapers, | ||
| insertIntoAuthorsPapers, | ||
| } from './ex2.js'; | ||
|
|
||
| // Write a query that prints names of all authors and their corresponding mentors. | ||
| const authorsMentorsQuery = ` SELECT | ||
| a.Author AS Author, M.Author AS mentor | ||
| FROM Author AS A LEFT JOIN Author AS M ON A.mentor = M.author_id;`; | ||
|
|
||
| authorsMentorsQuery; | ||
|
|
||
| // Write a query that prints all columns of authors and their published paper_title | ||
| // If there is an author without any research_Papers, print the information of that author too. | ||
|
|
||
| const authorsPapersQuery = ` SELECT A.*, RP.paper_title | ||
| FROM Author AS A LEFT JOIN research_Papers AS RP ON A.author_id = RP.paper_id | ||
| LEFT JOIN research_Papers AS RP ON A.author_id = RP.paper_id;`; | ||
|
|
||
| authorsPapersQuery; | ||
|
|
||
| export { authorsMentorsQuery, authorsPapersQuery }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nice work on the foreign key!