diff --git a/Week1/assignment/3.1-exercise/connection-test.js b/Week1/assignment/3.1-exercise/connection-test.js new file mode 100644 index 000000000..43ea8f0f5 --- /dev/null +++ b/Week1/assignment/3.1-exercise/connection-test.js @@ -0,0 +1,70 @@ +import mysql from "mysql2"; +const connection = mysql.createConnection({ + host: "localhost", + user: "hyfuser", + password: "hyfpassword", + multipleStatements: true, +}); + +const setUpQueries = ` +DROP DATABASE IF EXISTS meetup; +create database meetup; +use meetup; + +/* create invite table */ + +create table invitee( +invitee_no int auto_increment primary key, +invitee_name varchar(100), +invitee_by varchar(100) +); + +/* create room table */ + +create table room ( +room_no int auto_increment primary key, +room_name varchar (100), +floor_no TINYINT UNSIGNED, +); + +/* create meeting table */ + +create table meeting ( +meeting_no int auto_increment primary key, +meeting_title varchar(200), +starting_time DATETIME, +ending_time DATETIME, +room_no int, foreign key (room_no) references room (room_no) +); + +/* inserting 5 invitees */ + +insert into invitee (invite_name,invite_by) +values ('rizan','stas'),('ibrahim','gea'),('araz','ahmed'),('sara','stas'),('jasmin','shero'); + + +/* inserting 5 rooms */ +insert into room (room_name,room_no) +values ('room1',1),('room2',2),('room3',3),('room4',4),('room5',5); + + +/* inserting meeting */ + +insert into meeting (meeting_title,starting_time,ending_time,room_no) +values ('career-session', '2025-05-21 11:00:00', '2025-05-21 13:00:00', 1), +('product', '2025-05-20 11:30:00', '2025-05-20 18:30:00', 2), +('android-workshop', '2025-05-21 15:30:00', '2025-05-21 20:30:00', 3), +('marketing', '2025-05-23 10:30:00', '2025-05-23 14:30:00', 4), +('crypto-event', '2025-05-26 11:00:00', '2025-05-26 15:00:00', 5); + +`; +connection.connect((err) => { + if (err) throw err; + console.log("connected to mysql"); + + connection.query(setUpQueries, (err) => { + if (err) throw err; + console.log("databese and tables and data been inserted "); + connection.end(); + }); +}); diff --git a/Week1/assignment/3.1-exercise/diagram for the database.png b/Week1/assignment/3.1-exercise/diagram for the database.png new file mode 100644 index 000000000..394d3daa8 Binary files /dev/null and b/Week1/assignment/3.1-exercise/diagram for the database.png differ diff --git a/Week1/assignment/3.2-exercise/queries.js b/Week1/assignment/3.2-exercise/queries.js new file mode 100644 index 000000000..4b2621877 --- /dev/null +++ b/Week1/assignment/3.2-exercise/queries.js @@ -0,0 +1,37 @@ +import mysql from "mysql2"; +const connection = mysql.createConnection({ + host: "localhost", + user: "hyfuser", + password: "hyfpassword", + database: "new_world", +}); +connection.connect((err) => { + if (err) throw err; + console.log(" Connected to new_world database"); + + const queries = [ + "select name from country where Population > 8000000", + "SELECT name FROM country WHERE name LIKE '%land%'", + "select name from city where population between 500000 and 1000000", + "SeLect Name FROM country WHERE Continent = 'Europe'", + "SELECT Name, SurfaceArea FROM country ORDER BY SurfaceArea DESC", + 'select name from city where countrycode = "nld" ', + 'select population from city where name = "rotterdam" ', + "select name, surfacearea from country order by surfacearea desc limit 10", + "select name, population from city order by population desc limit 10", + "select sum (population) as wrold_population from country", + ]; + + queries.forEach((query, index) => { + connection.query(query, (err, results) => { + if (err) throw err; + console.log(`${index + 1}:`); + console.table(results); + }); + }); + + setTimeout(() => { + connection.end(); + console.log("Done running all queries"); + }, 1500); +});