Skip to content
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
109 changes: 109 additions & 0 deletions Week1/exercise3_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const { Client } = require("pg");

// PostgreSQL login (Docker HYF setup)
const adminConfig = {
user: "hyfuser",
host: "localhost",
database: "hyfuser",
password: "hyfpassword",
port: 5432,
};

async function main() {
// Step 1 — Connect to PostgreSQL as hyfuser
const adminClient = new Client(adminConfig);
let meetupClient;

try {
console.log("Connecting to PostgreSQL...");
await adminClient.connect();

// Step 2 — Drop & recreate database meetup
console.log("Dropping database meetup (if exists)...");
await adminClient.query("DROP DATABASE IF EXISTS meetup;");

console.log("Creating database meetup...");
await adminClient.query("CREATE DATABASE meetup;");

// Step 3 — Connect to the new meetup database
meetupClient = new Client({
...adminConfig,
database: "meetup",
});

await meetupClient.connect();
console.log("Connected to meetup database.");

// Step 4 — Create tables
console.log("Creating tables...");

await meetupClient.query(`
CREATE TABLE Invitee (
invitee_no SERIAL PRIMARY KEY,
invitee_name VARCHAR(50),
invited_by VARCHAR(50)
);
`);

await meetupClient.query(`
CREATE TABLE Room (
room_no SERIAL PRIMARY KEY,
room_name VARCHAR(50),
floor_number INT
);
`);

await meetupClient.query(`
CREATE TABLE Meeting (
meeting_no SERIAL PRIMARY KEY,
meeting_title VARCHAR(100),
starting_time TIME,
ending_time TIME,
room_no INT REFERENCES Room(room_no)
);
`);

// Step 5 — Insert 5 rows into each table
console.log("Inserting data...");

await meetupClient.query(`
INSERT INTO Invitee (invitee_name, invited_by) VALUES
('Majd', 'HYF'),
('karim', 'Majd'),
('alaa', 'karim'),
('rim', 'Majd'),
('ivan', 'rim');
`);

await meetupClient.query(`
INSERT INTO Room (room_name, floor_number) VALUES
('Blue Room', 1),
('Green Room', 2),
('Red Room', 3),
('Yellow Room', 1),
('VIP Room', 4);
`);

await meetupClient.query(`
INSERT INTO Meeting (meeting_title, starting_time, ending_time, room_no) VALUES
('Frontend Session', '10:00', '11:00', 1),
('Database Workshop', '12:00', '13:30', 2),
('Backend Lecture', '14:00', '15:00', 3),
('UX Review', '16:00', '17:00', 4),
('Career Talk', '09:00', '10:00', 5);
`);

console.log("All data inserted successfully.");
console.log("Exercise 3.1 completed.");

} catch (error) {
console.error("ERROR:", error);
} finally {
if (meetupClient) {
await meetupClient.end();
}
await adminClient.end();
}
}

main();
110 changes: 110 additions & 0 deletions Week1/exercise3_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// exercise3_2.js
const { Client } = require("pg");

async function run() {
const client = new Client({
host: "localhost",
port: 5432,
user: "hyfuser",
password: "hyfpassword",
database: "world",
});

try {
await client.connect();
console.log("Connected to world database\n");

let result;

console.log("1. Countries with population > 8 million:");
result = await client.query(`
SELECT name
FROM country
WHERE population > 8000000;
`);
console.log(result.rows);

console.log("\n2. Countries containing 'land' in the name:");
result = await client.query(`
SELECT name
FROM country
WHERE name ILIKE '%land%';
`);
console.log(result.rows);

console.log("\n3. Cities with population between 500,000 and 1,000,000:");
result = await client.query(`
SELECT name
FROM city
WHERE population BETWEEN 500000 AND 1000000;
`);
console.log(result.rows);

console.log("\n4. Countries in Europe:");
result = await client.query(`
SELECT name
FROM country
WHERE continent = 'Europe';
`);
console.log(result.rows);

console.log("\n5. Countries ordered by surface area (DESC):");
result = await client.query(`
SELECT name, surfacearea
FROM country
ORDER BY surfacearea DESC;
`);
console.log(result.rows);

console.log("\n6. Cities in the Netherlands:");
result = await client.query(`
SELECT city.name
FROM city
JOIN country ON city.countrycode = country.code
WHERE country.name = 'Netherlands';
`);
console.log(result.rows);

console.log("\n7. Population of Rotterdam:");
result = await client.query(`
SELECT population
FROM city
WHERE name = 'Rotterdam';
`);
console.log(result.rows);

console.log("\n8. Top 10 countries by surface area:");
result = await client.query(`
SELECT name, surfacearea
FROM country
ORDER BY surfacearea DESC
LIMIT 10;
`);
console.log(result.rows);

console.log("\n9. Top 10 most populated cities:");
result = await client.query(`
SELECT name, population
FROM city
ORDER BY population DESC
LIMIT 10;
`);
console.log(result.rows);

console.log("\n10. Total world population:");
result = await client.query(`
SELECT SUM(population) AS world_population
FROM country;
`);
console.log(result.rows);

console.log("\nAll queries executed.");
} catch (err) {
console.error("ERROR:", err);
} finally {
await client.end();
console.log("Connection closed.");
}
}

run();
12 changes: 12 additions & 0 deletions Week2/Week2assignment/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pg from 'pg';
const { Pool } = pg;

const pool = new Pool({
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
database: 'userdb',
port: 5432,
});

export default pool;
43 changes: 43 additions & 0 deletions Week2/Week2assignment/ex1-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pool from './db.js';

async function run() {
try {
// Create authors table
await pool.query(`
CREATE TABLE IF NOT EXISTS authors (
author_id SERIAL PRIMARY KEY,
author_name VARCHAR,
university VARCHAR,
date_of_birth DATE,
h_index INTEGER,
gender VARCHAR
);
`);

// Add mentor column with foreign key
// We use ADD COLUMN IF NOT EXISTS to avoid errors on re-runs if supported,
// or catch the error. Since standard SQL doesn't support IF NOT EXISTS for columns easily in one line without PL/SQL,
// we will just try to add it and catch the error if it exists, or check first.
// However, the prompt says "Each file must execute its SQL when run".
// I'll assume a fresh run or handle it gracefully.
// Let's try to add it. If it fails because it exists, that's fine.

try {
await pool.query(`
ALTER TABLE authors
ADD COLUMN mentor INTEGER REFERENCES authors(author_id);
`);
} catch (err) {
if (err.code !== '42701') { // duplicate_column
throw err;
}
}

} catch (err) {
console.error(err);
} finally {
await pool.end();
}
}

run();
Loading