From fbaa1b5fc9679c563f23ea9aa926b9e89fc851d8 Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Wed, 10 Jul 2024 14:49:28 -0400 Subject: [PATCH] feat: final ? --- .../java/org/example/DonorsGenerator.java | 4 +- src/main/java/org/example/Main.java | 2 +- src/main/sql/answers.sql | 176 ++++++++++++++++++ 3 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 src/main/sql/answers.sql diff --git a/src/main/java/org/example/DonorsGenerator.java b/src/main/java/org/example/DonorsGenerator.java index 8b32c4b..fbf8b53 100644 --- a/src/main/java/org/example/DonorsGenerator.java +++ b/src/main/java/org/example/DonorsGenerator.java @@ -18,7 +18,7 @@ public class DonorsGenerator { "Hiking", "Skiing", "Swimming", "Bird watching", "Racing", "Walking", "Gaming", }; - private final Random RANDOM; + private static Random RANDOM; private static Connection conn; @@ -27,7 +27,7 @@ public class DonorsGenerator { public DonorsGenerator(int recordCount, Connection conn, Random random) { DonorsGenerator.conn = conn; count = recordCount; - RANDOM = random; + DonorsGenerator.RANDOM = random; } public void insertDonors() throws SQLException { diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 373d148..fe95192 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -8,7 +8,7 @@ import java.util.Random; public class Main { // change the DB_URL accordingly static final String HOST = "localhost"; - static final String PORT = "32772"; + static final String PORT = "32768"; static final String DATABASE_NAME = "assignment1"; static final String DB_URL = "jdbc:mysql://" + HOST + ":" + PORT + "/" + DATABASE_NAME; diff --git a/src/main/sql/answers.sql b/src/main/sql/answers.sql new file mode 100644 index 0000000..1a3bc3f --- /dev/null +++ b/src/main/sql/answers.sql @@ -0,0 +1,176 @@ +-- Question 1 part 1 a) +CREATE TABLE Donors +( + donorID INT NOT NULL, + firstName VARCHAR(50) NOT NULL, + lastName VARCHAR(50) NOT NULL, + middleInitial CHAR(1), + dateOfBirth DATE, + address VARCHAR(100), -- might need to change to civic code only + city VARCHAR(50), + postalCode VARCHAR(7), + province ENUM ( + 'AB', -- Alberta + 'BC', -- British Columbia + 'MB', -- Manitoba + 'NB', -- New Brunswick + 'NL', -- Newfoundland and Labrador + 'NS', -- Nova Scotia + 'NT', -- Northwest Territories + 'NU', -- Nunavut + 'ON', -- Ontario + 'PE', -- Prince Edward Island + 'QC', -- Quebec + 'SK', -- Saskatchewan + 'YT' -- Yukon + ), + gender ENUM ('M', 'F', 'U'), + SSN VARCHAR(11), + hobby VARCHAR(100), + phone VARCHAR(15), + email VARCHAR(100), + CONSTRAINT donors_pk PRIMARY KEY (donorID) +); + +CREATE TABLE Donations +( + dID INT NOT NULL, + donorID INT NOT NULL, + date DATE NOT NULL, + type VARCHAR(7) NOT NULL, + amount DECIMAL(10, 2) NOT NULL, + CONSTRAINT donations_pk PRIMARY KEY (dID), + CONSTRAINT donations_donorID_fk FOREIGN KEY (donorID) REFERENCES Donors (donorID), + CONSTRAINT valid_donation_type CHECK (type IN ('money', 'product')) +); + +create table Products +( + pID INT NOT NULL, + description varchar(100), + date DATE NOT NULL, + price DECIMAL(10, 2) NOT NULL, + weight DECIMAL(5, 2) NOT NULL, + CONSTRAINT products_pk PRIMARY KEY (pID) +); + +CREATE TABLE Sales +( + sID INT NOT NULL, + date DATE NOT NULL, + amount DECIMAL(10, 2) NOT NULL, + totalWeight DECIMAL(10, 2) NOT NULL, + deliveryFee DECIMAL(10, 2) DEFAULT 0 NOT NULL, + CONSTRAINT sales_pk primary key (sID), + CONSTRAINT valid_delivery_fee CHECK (deliveryFee >= 0) +); + +CREATE TABLE SalesItems +( + sID INT NOT NULL, + pID INT NOT NULL, + PRIMARY KEY (sID, pID), + FOREIGN KEY (sID) REFERENCES Sales (sID), + FOREIGN KEY (pID) REFERENCES Products (pID) +); + +-- Question 1 part 1 b) +ALTER TABLE Donors + DROP COLUMN hobby; + +-- Question 1 part 1 c) +ALTER TABLE Products + ADD location VARCHAR(50) DEFAULT 'Unknown'; +ALTER TABLE Products + ADD color VARCHAR(10) DEFAULT 'Unknown'; + +-- Question 1 part 1 d) +-- !!! ASSUME THAT HOBBY COLUMN IS NO LONGER THERE +INSERT INTO Donors (donorID, firstName, lastName, middleInitial, dateOfBirth, address, city, postalCode, province, + gender, SSN, phone, email) +VALUES (1, 'John', 'Smith', 'A', '1985-03-15', '123 Maple St', 'Toronto', 'M5V 2T6', 'ON', 'M', '123-456-789', + '(416) 555-1234', 'john.smith@email.com'); + +INSERT INTO Donors (donorID, firstName, lastName, middleInitial, dateOfBirth, address, city, postalCode, province, + gender, SSN, phone, email) +VALUES (2, 'Emily', 'Johnson', 'M', '1990-07-22', '456 Oak Ave', 'Vancouver', 'V6B 3K9', 'BC', 'F', '987-654-321', + '(604) 555-5678', 'emily.j@email.com'); + +INSERT INTO Donors (donorID, firstName, lastName, middleInitial, dateOfBirth, address, city, postalCode, province, + gender, SSN, phone, email) +VALUES (3, 'Alex', 'Lee', NULL, '1988-11-30', '789 Pine Rd', 'Montreal', 'H3A 1A1', 'QC', 'U', '456-789-123', + '(514) 555-9012', 'alex.lee@email.com'); + +INSERT INTO Donations (dID, donorID, date, type, amount) +VALUES (1, 1, '2023-05-15', 'money', 100.00); + +INSERT INTO Donations (dID, donorID, date, type, amount) +VALUES (2, 2, '2023-06-22', 'product', 75.50); + +INSERT INTO Donations (dID, donorID, date, type, amount) +VALUES (3, 3, '2023-07-03', 'money', 250.00); + +-- Question 1 part 1 e) +DELETE +FROM Donors +WHERE donorID > 0; +DELETE +FROM Donations +WHERE donorID > 0; + +-- Question 1 part 1 f) +-- We have not covered ON DELETE/ ON UPDATE CASCADE +-- in class so wer have to delete in such order of statements +DROP TABLE Donations; +DROP TABLE Donors; +DROP TABLE SalesItems; +DROP TABLE Products; +DROP TABLE Sales; + +-- Question 1 part 2 a) +SELECT Donors.donorID, + Donors.firstName, + Donors.lastName, + Donors.middleInitial, + Donors.dateOfBirth, + Donors.phone, + Donors.email, + Donors.SSN +FROM Donors +WHERE (Donors.gender = 'M') + AND (Donors.city = 'Montreal' OR Donors.city = 'Laval'); + +-- Question 1 part 2 b) +SELECT Sales.sID, Products.pID, Products.description, Products.price, Products.weight +FROM Sales, + Products +WHERE Products.color = 'red' + AND Sales.date = '2024-02-14'; + +-- Question 1 part 2 c) +-- One sID can have multiple products +-- We need the DISTINCT keyword +SELECT COUNT(DISTINCT sID) +FROM Sales +WHERE Sales.date = '2024-02-14'; + +-- Question 1 part 2 d) +SELECT EXTRACT(MONTH FROM date) AS month, + COUNT(DISTINCT sID) AS total_sales, + SUM(amount) AS total_amount, + SUM(deliveryFee) AS total_delivery_fees +FROM Sales +WHERE EXTRACT(YEAR FROM date) = 2023 +GROUP BY EXTRACT(MONTH FROM date) +ORDER BY month; + +-- Question 1 part 2 e) +SELECT Donors.gender, Donors.lastName, Donors.firstName, SUM(Donations.amount) AS total_donation_amount +FROM Donors, + Donations +WHERE Donors.city = 'Brossard' + AND (Donations.date > '2022-01-01') + AND (Donations.date < '2023-12-31') +GROUP BY Donors.donorID, Donors.gender, Donors.lastName, Donors.firstName +ORDER BY Donors.gender, Donors.lastName, Donors.firstName; +