diff --git a/E-R diagram.png b/E-R diagram.png new file mode 100644 index 0000000..50f1724 Binary files /dev/null and b/E-R diagram.png differ diff --git a/README.md b/README.md index 57ab389..6ce75a7 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Before this starting this lab, you should have learnt about: - Fundamental concepts of database design, including entities, attributes, primary keys, and foreign keys. - Basic comprehension of SQL syntax and statements, such as CREATE, INSERT INTO, UPDATE, and DELETE. -- Familiarity with the concept of NOT NULL constraints and data types in SQL. +- Familiarity with the concept of NOT NULL constraints and data types in SQL

diff --git a/create.sql b/create.sql new file mode 100644 index 0000000..b88997e --- /dev/null +++ b/create.sql @@ -0,0 +1,61 @@ +-- create.sql +CREATE DATABASE IF NOT EXISTS lab_mysql; +USE lab_mysql; + +-- Drop in FK-safe order +DROP TABLE IF EXISTS invoices; +DROP TABLE IF EXISTS salespersons; +DROP TABLE IF EXISTS customers; +DROP TABLE IF EXISTS cars; + +-- Cars +CREATE TABLE cars ( + id INT AUTO_INCREMENT PRIMARY KEY, + vin VARCHAR(17) NOT NULL, + manufacturer VARCHAR(50) NOT NULL, + model VARCHAR(100) NOT NULL, + model_year YEAR NOT NULL, + color VARCHAR(30) NOT NULL + -- Optionally enforce VIN uniqueness after cleaning: + -- , UNIQUE KEY uk_cars_vin (vin) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +-- Customers +CREATE TABLE customers ( + id INT AUTO_INCREMENT PRIMARY KEY, + cust_id INT NOT NULL, -- business ID (not PK) + cust_name VARCHAR(100) NOT NULL, + cust_phone VARCHAR(30), + cust_email VARCHAR(100), + cust_address VARCHAR(150), + cust_city VARCHAR(100), + cust_state VARCHAR(100), + cust_country VARCHAR(100), + cust_zipcode VARCHAR(20), + UNIQUE KEY uk_customers_cust_id (cust_id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +-- Salespersons +CREATE TABLE salespersons ( + id INT AUTO_INCREMENT PRIMARY KEY, + staff_id INT NOT NULL, -- business ID (not PK) + name VARCHAR(100) NOT NULL, + store VARCHAR(100) NOT NULL, + UNIQUE KEY uk_salespersons_staff_id (staff_id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +-- Invoices (car sale) +CREATE TABLE invoices ( + id INT AUTO_INCREMENT PRIMARY KEY, + invoice_number BIGINT NOT NULL, + invoice_date DATE NOT NULL, + car_id INT NOT NULL, + customer_id INT NOT NULL, + salesperson_id INT NOT NULL, + UNIQUE KEY uk_invoice_number (invoice_number), + -- Enforce 1 sale per car (optional but recommended): + UNIQUE KEY uk_invoices_car_once (car_id), + CONSTRAINT fk_inv_car FOREIGN KEY (car_id) REFERENCES cars(id) ON DELETE RESTRICT, + CONSTRAINT fk_inv_customer FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE RESTRICT, + CONSTRAINT fk_inv_salesperson FOREIGN KEY (salesperson_id) REFERENCES salespersons(id) ON DELETE RESTRICT +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/delete.sql b/delete.sql new file mode 100644 index 0000000..2824c35 --- /dev/null +++ b/delete.sql @@ -0,0 +1,21 @@ +-- delete.sql (variant A) +USE lab_mysql; +DELETE FROM cars WHERE id = 4; + +-- Verify it’s gone +SELECT id, vin, manufacturer, model FROM cars ORDER BY id; + +-- delete.sql (variant B) +USE lab_mysql; + +-- Remove duplicate VIN rows, keeping the smallest id for each VIN +DELETE c1 FROM cars c1 +JOIN cars c2 + ON c1.vin = c2.vin + AND c1.id > c2.id; + +-- After cleaning, prevent future duplicates (add UNIQUE index) +ALTER TABLE cars ADD UNIQUE KEY uk_cars_vin (vin); + +-- Verify +SELECT vin, COUNT(*) AS n FROM cars GROUP BY vin HAVING n > 1; diff --git a/seeding.sql b/seeding.sql new file mode 100644 index 0000000..b231366 --- /dev/null +++ b/seeding.sql @@ -0,0 +1,34 @@ +-- seeding.sql +USE lab_mysql; + +-- Cars (note duplicated VIN for ids 5 and 6 on purpose for Bonus) +INSERT INTO cars (id, vin, manufacturer, model, model_year, color) VALUES +(1, '3K096I98581DHSNUP', 'Volkswagen', 'Tiguan', 2019, 'Blue'), +(2, 'ZM8G7BEUQZ97IH46V', 'Peugeot', 'Rifter', 2019, 'Red'), +(3, 'RKXVNNIHLVVZOUB4M', 'Ford', 'Fusion', 2018, 'White'), +(4, 'HKNDGS7CU31E9Z7JW', 'Toyota', 'RAV4', 2018, 'Silver'), +(5, 'DAM41UDN3CHU2WVF6', 'Volvo', 'V60', 2019, 'Gray'), +(6, 'DAM41UDN3CHU2WVF6', 'Volvo', 'V60 Cross Country', 2019, 'Gray'); + +-- Customers (1-based IDs to match invoices) +INSERT INTO customers (id, cust_id, cust_name, cust_phone, cust_email, cust_address, cust_city, cust_state, cust_country, cust_zipcode) VALUES +(1, 10001, 'Pablo Picasso', '+34 636 17 63 82', NULL, 'Paseo de la Chopera, 14', 'Madrid', 'Madrid', 'Spain', '28045'), +(2, 20001, 'Abraham Lincoln', '+1 305 907 7086', NULL, '120 SW 8th St', 'Miami', 'Florida', 'United States', '33130'), +(3, 30001, 'Napoléon Bonaparte', '+33 1 79 75 40 00', NULL, '40 Rue du Colisée', 'Paris', 'Île-de-France', 'France', '75008'); + +-- Salespersons +INSERT INTO salespersons (id, staff_id, name, store) VALUES +(1, 1, 'Petey Cruiser', 'Madrid'), +(2, 2, 'Anna Sthesia', 'Barcelona'), +(3, 3, 'Paul Molive', 'Berlin'), +(4, 4, 'Gail Forcewind', 'Paris'), +(5, 5, 'Paige Turner', 'Miami'), +(6, 6, 'Bob Frapples', 'Mexico City'), +(7, 7, 'Walter Melon', 'Amsterdam'), +(8, 8, 'Shonda Leer', 'São Paulo'); + +-- Invoices +INSERT INTO invoices (id, invoice_number, invoice_date, car_id, customer_id, salesperson_id) VALUES +(1, 852399038, '2018-08-22', 1, 1, 3), +(2, 731166526, '2018-12-31', 3, 3, 5), +(3, 271135104, '2019-01-22', 2, 2, 7); diff --git a/update.sql b/update.sql new file mode 100644 index 0000000..c357b5f --- /dev/null +++ b/update.sql @@ -0,0 +1,20 @@ +-- update.sql +USE lab_mysql; + +-- If Workbench complains about safe updates: +SET SQL_SAFE_UPDATES = 0; + +UPDATE customers SET cust_email = 'ppicasso@gmail.com' +WHERE cust_name = 'Pablo Picasso'; + +UPDATE customers SET cust_email = 'lincoln@us.gov' +WHERE cust_name = 'Abraham Lincoln'; + +UPDATE customers SET cust_email = 'hello@napoleon.me' +WHERE cust_name = 'Napoléon Bonaparte'; + +-- Optional: turn safe updates back on +SET SQL_SAFE_UPDATES = 1; + +-- Verify +SELECT id, cust_name, cust_email FROM customers ORDER BY id;