diff --git a/ERD diagram.mwb b/ERD diagram.mwb new file mode 100644 index 0000000..91cf75f Binary files /dev/null and b/ERD diagram.mwb differ diff --git a/ERD.png b/ERD.png new file mode 100644 index 0000000..f65e96f Binary files /dev/null and b/ERD.png differ diff --git a/create.sql b/create.sql new file mode 100644 index 0000000..c67cc00 --- /dev/null +++ b/create.sql @@ -0,0 +1,56 @@ +-- create.sql +-- Create database and all tables from scratch + +CREATE DATABASE IF NOT EXISTS lab_mysql; +USE lab_mysql; + +-- Drop in child→parent order (disable FKs while dropping) +SET FOREIGN_KEY_CHECKS = 0; +DROP TABLE IF EXISTS invoices; +DROP TABLE IF EXISTS salespersons; +DROP TABLE IF EXISTS customers; +DROP TABLE IF EXISTS cars; +SET FOREIGN_KEY_CHECKS = 1; + +-- Parent tables +CREATE TABLE cars ( + id INT AUTO_INCREMENT PRIMARY KEY, + vin VARCHAR(32) NOT NULL, + manufacturer VARCHAR(50) NOT NULL, + model VARCHAR(80) NOT NULL, + year YEAR NOT NULL, + color VARCHAR(30) NOT NULL +) ENGINE=InnoDB; + +CREATE TABLE customers ( + id INT AUTO_INCREMENT PRIMARY KEY, + cust_id INT NOT NULL, + cust_name VARCHAR(100) NOT NULL, + cust_phone VARCHAR(30), + cust_email VARCHAR(255), + cust_address VARCHAR(255), + cust_city VARCHAR(100), + cust_state VARCHAR(100), + cust_country VARCHAR(100), + cust_zipcode VARCHAR(20) +) ENGINE=InnoDB; + +CREATE TABLE salespersons ( + id INT AUTO_INCREMENT PRIMARY KEY, + staff_id INT NOT NULL, + name VARCHAR(100) NOT NULL, + store VARCHAR(100) +) ENGINE=InnoDB; + +-- Child table with FKs to the three parents +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, + CONSTRAINT fk_invoices_car FOREIGN KEY (car_id) REFERENCES cars(id), + CONSTRAINT fk_invoices_customer FOREIGN KEY (customer_id) REFERENCES customers(id), + CONSTRAINT fk_invoices_salesperson FOREIGN KEY (salesperson_id) REFERENCES salespersons(id) +) ENGINE=InnoDB; diff --git a/erd1.png b/erd1.png new file mode 100644 index 0000000..f65e96f Binary files /dev/null and b/erd1.png differ diff --git a/seeding.sql b/seeding.sql new file mode 100644 index 0000000..f7ff57c --- /dev/null +++ b/seeding.sql @@ -0,0 +1,33 @@ +-- seeding.sql +USE lab_mysql; + +-- Insert sample data (don’t set the auto-increment id; let MySQL assign 1,2,3…) +INSERT INTO cars (vin, manufacturer, model, year, color) VALUES +('3K096I98581DHSNUP', 'Volkswagen', 'Tiguan', 2019, 'Blue'), +('ZM8G7BEUQZ97IH46V', 'Peugeot', 'Rifter', 2019, 'Red'), +('RKXVNNIHLVVZOUB4M', 'Ford', 'Fusion', 2018, 'White'), +('HKNDGS7CU31E9Z7JW', 'Toyota', 'RAV4', 2018, 'Silver'), +('DAM41UDN3CHU2WVF6', 'Volvo', 'V60', 2019, 'Gray'), +('DAM41UDN3CHU2WVF6', 'Volvo', 'V60 Cross Country', 2019, 'Gray'); + +INSERT INTO customers (cust_id, cust_name, cust_phone, cust_email, cust_address, cust_city, cust_state, cust_country, cust_zipcode) VALUES +(10001, 'Pablo Picasso', '+34 636 17 63 82', NULL, 'Paseo de la Chopera, 14', 'Madrid', 'Madrid', 'Spain', '28045'), +(20001, 'Abraham Lincoln', '+1 305 907 7086', NULL, '120 SW 8th St', 'Miami', 'Florida', 'United States', '33130'), +(30001, 'Napoléon Bonaparte', '+33 1 79 75 40 00', NULL, '40 Rue du Colisée', 'Paris', 'Île-de-France', 'France', '75008'); + +INSERT INTO salespersons (staff_id, name, store) VALUES +(1, 'Petey Cruiser', 'Madrid'), +(2, 'Anna Sthesia', 'Barcelona'), +(3, 'Paul Molive', 'Berlin'), +(4, 'Gail Forcewind', 'Paris'), +(5, 'Paige Turner', 'Mimia'), +(6, 'Bob Frapples', 'Mexico City'), +(7, 'Walter Melon', 'Amsterdam'), +(8, 'Shonda Leer', 'São Paulo'); + +-- The following INSERTs assume the ids assigned by AUTO_INCREMENT are: +-- cars: 1..6 in the order inserted; customers: 1..3; salespersons: 1..8 +INSERT INTO invoices (invoice_number, invoice_date, car_id, customer_id, salesperson_id) VALUES +(852399038, '2018-08-22', 1, 1, 3), +(731166526, '2018-12-31', 3, 3, 5), +(271135104, '2019-01-22', 2, 2, 7); diff --git a/update&delete.sql b/update&delete.sql new file mode 100644 index 0000000..51d5a4d --- /dev/null +++ b/update&delete.sql @@ -0,0 +1,20 @@ +-- update.sql +USE lab_mysql; + +-- Safe-update mode can block these; disable it if you get a warning. +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'; +-- delete.sql +USE lab_mysql; + +-- The README asks to remove the entry with car ID #4. +DELETE FROM cars +WHERE id = 4; diff --git a/verify.sql b/verify.sql new file mode 100644 index 0000000..f4fa6ff --- /dev/null +++ b/verify.sql @@ -0,0 +1,12 @@ +USE lab_mysql; + +SELECT * FROM cars; +SELECT * FROM customers; +SELECT * FROM salespersons; +SELECT * FROM invoices; + +-- After updates: +SELECT cust_name, cust_email FROM customers; + +-- After delete: +SELECT id, vin, manufacturer, model FROM cars ORDER BY id;