forked from HackYourFuture/databases
-
Notifications
You must be signed in to change notification settings - Fork 9
Karim w2 db #20
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
hkarimx
wants to merge
3
commits into
HackYourAssignment:main
Choose a base branch
from
hkarimx:KARIM-W2-DB
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
Karim w2 db #20
Changes from 2 commits
Commits
Show all changes
3 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,106 @@ | ||
| const { Client } = require("pg"); | ||
|
|
||
| const DB_USER = "hyfuser"; | ||
| const DB_PASSWORD = "hyfpassword"; | ||
| const DB_HOST = "localhost"; | ||
| const DB_PORT = 5432; | ||
|
|
||
| const baseConfig = { | ||
| host: DB_HOST, | ||
| port: DB_PORT, | ||
| user: DB_USER, | ||
| password: DB_PASSWORD, | ||
| }; | ||
|
|
||
| async function withClient(database, handler) { | ||
| const client = new Client({ ...baseConfig, database }); | ||
| await client.connect(); | ||
|
|
||
| try { | ||
| return await handler(client); | ||
| } finally { | ||
| await client.end(); | ||
| } | ||
| } | ||
|
|
||
| async function setupMeetup() { | ||
| try { | ||
| // 1) Drop & recreate database meetup | ||
| await withClient("postgres", async (client) => { | ||
| console.log("Recreating database meetup..."); | ||
|
|
||
| await client.query("DROP DATABASE IF EXISTS meetup;"); | ||
| await client.query("CREATE DATABASE meetup;"); | ||
| }); | ||
|
|
||
|
|
||
| await withClient("meetup", async (db) => { | ||
| console.log("Creating tables..."); | ||
|
|
||
| await db.query(` | ||
| CREATE TABLE Invitee ( | ||
| invitee_no SERIAL PRIMARY KEY, | ||
| invitee_name VARCHAR(80) NOT NULL, | ||
| invited_by VARCHAR(80) NOT NULL | ||
| ); | ||
| `); | ||
|
|
||
| await db.query(` | ||
| CREATE TABLE Room ( | ||
| room_no SERIAL PRIMARY KEY, | ||
| room_name VARCHAR(80) NOT NULL, | ||
| floor_number INTEGER NOT NULL | ||
| ); | ||
| `); | ||
|
|
||
| await db.query(` | ||
| CREATE TABLE Meeting ( | ||
| meeting_no SERIAL PRIMARY KEY, | ||
| meeting_title VARCHAR(120) NOT NULL, | ||
| starting_time TIMESTAMP NOT NULL, | ||
| ending_time TIMESTAMP NOT NULL, | ||
| room_no INTEGER REFERENCES Room(room_no) | ||
| ); | ||
| `); | ||
|
|
||
| console.log("Inserting sample data into Invitee..."); | ||
| await db.query(` | ||
| INSERT INTO Invitee (invitee_name, invited_by) VALUES | ||
| ('Karim', 'Majd'), | ||
| ('Rim', 'Karim'), | ||
| ('Alaa', 'Karim'), | ||
| ('Ivan', 'Majd'), | ||
| ('Dima', 'Ivan'); | ||
| `); | ||
|
|
||
| console.log("Inserting sample data into Room..."); | ||
| await db.query(` | ||
| INSERT INTO Room (room_name, floor_number) VALUES | ||
| ('Alfa Room', 1), | ||
| ('Atlas Room', 1), | ||
| ('Nova Room', 2), | ||
| ('Fox Room', 2), | ||
| ('Sky Room', 3); | ||
| `); | ||
|
|
||
| console.log("Inserting sample data into Meeting..."); | ||
| await db.query(` | ||
| INSERT INTO Meeting (meeting_title, starting_time, ending_time, room_no) VALUES | ||
| ('Intro to Databases', '2025-11-20 09:00', '2025-11-20 10:00', 1), | ||
| ('SQL Practice', '2025-11-20 10:30', '2025-11-20 11:30', 2), | ||
| ('Node & Postgres', '2025-11-21 09:00', '2025-11-21 10:30', 3), | ||
| ('Security Session', '2025-11-21 11:00', '2025-11-21 12:00', 4), | ||
| ('Retrospective Meetup', '2025-11-22 15:00', '2025-11-22 16:00', 5); | ||
| `); | ||
| }); | ||
|
|
||
| console.log("Meetup database created and filled with data."); | ||
| } catch (err) { | ||
| console.error("Error while setting up meetup database:", err.message); | ||
| } | ||
| } | ||
|
|
||
| setupMeetup().catch((err) => { | ||
| console.error("Unexpected error:", err); | ||
| }); | ||
|
|
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,127 @@ | ||
| const { Client } = require("pg"); | ||
|
|
||
| const DB_USER = "hyfuser"; | ||
| const DB_PASSWORD = "hyfpassword"; | ||
| const DB_HOST = "localhost"; | ||
| const DB_PORT = 5432; | ||
|
|
||
| const client = new Client({ | ||
| user: DB_USER, | ||
| password: DB_PASSWORD, | ||
| host: DB_HOST, | ||
| port: DB_PORT, | ||
| database: "world", | ||
| }); | ||
|
|
||
| async function runQuery(label, sql) { | ||
| console.log(`\n=== ${label} ===`); | ||
|
|
||
| const result = await client.query(sql); | ||
| console.table(result.rows); | ||
| } | ||
|
|
||
| async function runWorldQueries() { | ||
|
|
||
| try { | ||
|
|
||
| await client.connect(); | ||
| console.log("Connected to world database"); | ||
|
|
||
| await runQuery("1) Countries with population greater than 8 million", | ||
| ` | ||
| SELECT name, population | ||
| FROM country | ||
| WHERE population > 8000000 | ||
| ORDER BY population DESC; | ||
| `); | ||
|
|
||
| await runQuery('2) Countries that have "land" in their names', | ||
| ` | ||
| SELECT name | ||
| FROM country | ||
| WHERE name ILIKE '%land%' | ||
| ORDER BY name; | ||
| `); | ||
|
|
||
| await runQuery("3) Cities with population between 500,000 and 1,000,000", | ||
| ` | ||
| SELECT name, population | ||
| FROM city | ||
| WHERE population BETWEEN 500000 AND 1000000 | ||
| ORDER BY population; | ||
| `); | ||
|
|
||
| await runQuery("4) Countries on the continent 'Europe'", | ||
| ` | ||
| SELECT name | ||
| FROM country | ||
| WHERE continent = 'Europe' | ||
| ORDER BY name; | ||
| `); | ||
|
|
||
| await runQuery("5) Countries ordered by surface area (descending)", | ||
| ` | ||
| SELECT name, surfacearea | ||
| FROM country | ||
| ORDER BY surfacearea DESC; | ||
| `); | ||
|
|
||
| await runQuery("6) Cities in the Netherlands", | ||
| ` | ||
| SELECT c.name | ||
| FROM city AS c | ||
| JOIN country AS co ON c.countrycode = co.code | ||
| WHERE co.name = 'Netherlands' | ||
| ORDER BY c.name; | ||
| `); | ||
|
|
||
| await runQuery("7) Population of Rotterdam", | ||
| ` | ||
| SELECT population | ||
| FROM city | ||
| WHERE name = 'Rotterdam'; | ||
| `); | ||
|
|
||
| await runQuery("8) Top 10 countries by surface area", | ||
| ` | ||
| SELECT name, surfacearea | ||
| FROM country | ||
| ORDER BY surfacearea DESC | ||
| LIMIT 10; | ||
| `); | ||
|
|
||
| await runQuery( | ||
| "9) Top 10 most populated cities", | ||
| ` | ||
| SELECT name, population | ||
| FROM city | ||
| ORDER BY population DESC | ||
| LIMIT 10; | ||
| `); | ||
|
|
||
| await runQuery( | ||
| "10) Total world population", | ||
| ` | ||
| SELECT SUM(population) AS world_population | ||
| FROM country; | ||
| `); | ||
|
|
||
|
|
||
| console.log("\n All queries executed successfully."); | ||
|
|
||
| } catch (err) { | ||
|
|
||
| console.error("Error while running world queries:", err.message); | ||
|
|
||
| } finally { | ||
|
|
||
| await client.end(); | ||
|
|
||
| console.log("Connection closed."); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| runWorldQueries().catch((err) => { | ||
| console.error("Unexpected error:", err); | ||
| }); |
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,13 @@ | ||
| import pg from "pg"; | ||
|
|
||
| const { Pool } = pg; | ||
|
|
||
| const pool = new Pool({ | ||
| host: process.env.DB_HOST ?? "localhost", | ||
| user: process.env.DB_USER ?? "hyfuser", | ||
| password: process.env.DB_PASSWORD ?? "hyfpassword", | ||
| database: process.env.DB_NAME ?? "research_w2", | ||
| port: Number(process.env.DB_PORT ?? 5432), | ||
| }); | ||
|
|
||
| export default pool; | ||
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,42 @@ | ||
| import pool from "./db.js"; | ||
|
|
||
| async function run() { | ||
| try { | ||
|
|
||
| await pool.query("DROP TABLE IF EXISTS authors CASCADE;"); | ||
| await pool.query(` | ||
| CREATE TABLE authors ( | ||
| author_id SERIAL PRIMARY KEY, | ||
| full_name VARCHAR(120) NOT NULL, | ||
| university VARCHAR(120), | ||
| date_of_birth DATE, | ||
hkarimx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| h_index INTEGER, | ||
| gender VARCHAR(10) | ||
hkarimx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| `); | ||
|
|
||
|
|
||
| await pool.query(` | ||
| ALTER TABLE authors | ||
| ADD COLUMN mentor INTEGER; | ||
| `); | ||
|
|
||
|
|
||
| await pool.query(` | ||
| ALTER TABLE authors | ||
| ADD CONSTRAINT fk_authors_mentor | ||
| FOREIGN KEY (mentor) | ||
| REFERENCES authors (author_id) | ||
| ON DELETE SET NULL; | ||
| `); | ||
|
|
||
| console.log("Exercise 1: authors table + mentor FK created"); | ||
| } catch (err) { | ||
| console.error("Error in ex1-keys:", err.message); | ||
| } finally { | ||
| await pool.end(); | ||
| console.log("Connection closed"); | ||
| } | ||
| } | ||
|
|
||
| run(); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.