diff --git a/ERD.jpg b/ERD.jpg new file mode 100644 index 0000000..0dbbae0 Binary files /dev/null and b/ERD.jpg differ diff --git a/create.sql b/create.sql new file mode 100644 index 0000000..d195fe4 --- /dev/null +++ b/create.sql @@ -0,0 +1,53 @@ +CREATE DATABASE IF NOT EXISTS lab_my_sql; + +USE lab_my_sql; + +DROP TABLE IF EXISTS cars; + +CREATE TABLE cars (id INT AUTO_INCREMENT PRIMARY KEY, + id_vin INT, + manufacturer VARCHAR(30), + model VARCHAR(30), + year INT, + color VARCHAR(30), + price FLOAT); + +DROP TABLE IF EXISTS customers; + +CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, + id_cust INT, + name VARCHAR(50), + phone INT, + email VARCHAR(50), + address VARCHAR(50), + city VARCHAR(50), + state_province VARCHAR(50), + country VARCHAR(50), + zip_postal_code INT); + +DROP TABLE IF EXISTS sales_persons; + +CREATE TABLE sales_persons (id INT AUTO_INCREMENT PRIMARY KEY, + id_staff INT, + name_staff VARCHAR(30), + store VARCHAR(30)); + +DROP TABLE IF EXISTS invoices; + +CREATE TABLE invoices (id INT AUTO_INCREMENT PRIMARY KEY, + inv_nr INT UNIQUE, + date DATE, + customer_id INT, + car_id INT, + sales_id INT, + FOREIGN KEY (customer_id) REFERENCES customers(id), + FOREIGN KEY (car_id) REFERENCES cars(id), + FOREIGN KEY (sales_id) REFERENCES sales_persons(id)); + +SELECT * FROM lab_my_sql.cars + +USE lab_my_sql; +SHOW TABLES; +DESCRIBE sales_persons; + + diff --git a/my_sql_ERD.mwb b/my_sql_ERD.mwb new file mode 100644 index 0000000..c079299 Binary files /dev/null and b/my_sql_ERD.mwb differ diff --git a/my_sql_ERD.mwb.bak b/my_sql_ERD.mwb.bak new file mode 100644 index 0000000..d839325 Binary files /dev/null and b/my_sql_ERD.mwb.bak differ diff --git a/seeding.sql b/seeding.sql new file mode 100644 index 0000000..a2d3484 --- /dev/null +++ b/seeding.sql @@ -0,0 +1,64 @@ +USE lab_my_sql; + +ALTER TABLE cars MODIFY COLUMN id_vin VARCHAR(30); + +INSERT INTO cars (id_vin, manufacturer, model, year, color, price) +VALUES ('3K096I98581DHSNUP', 'Volkswagen', 'Tiguan', 2019, 'Blue', 18000), + ('ZM8G7BEUQZ97IH46V', 'Peugeot', 'Rifter', 2019, 'Red', 15000), + ('RKXVNNIHLVVZOUB4M', 'Ford', 'Fusion', 2018, 'White', 14400), + ('HKNDGS7CU31E9Z7JW', 'Toyota', 'RAV4', 2018, 'Silver', 21000), + ('DAM41UDN3CHU2WVF6', 'Volvo', 'V60', 2019, 'Gray', 25000), + ('DAM41UDN3CHU2WVF7', 'Volvo', 'V60 Cross Country', 2019, 'Gray', 26500); + + + + +SET SQL_SAFE_UPDATES = 0; + + +ALTER TABLE customers MODIFY COLUMN phone VARCHAR(30); + +INSERT INTO customers (id_cust, name, phone, email, address, city, state_province, country, zip_postal_code) +VALUES (10001, 'Pablo Picasso', '+34 636 17 63 82', ' ', 'Paseo de la Chopera, 14', 'Madrid', 'Madrid', 'Spain', 28045), + (20001, 'Abraham Lincoln', '+1 305 907 7086', ' ', '120 SW 8th St', 'Miami', 'Florida', 'United States', 33130), + (30001, 'Napoléon Bonaparte', '+33 1 79 75 40 00', ' ','40 Rue du Colisée', 'Paris', 'Île-de-France', 'France', 75008); + +UPDATE customers SET email = ' '; + +INSERT INTO sales_persons (id_staff, name_staff, store) +VALUES (201, 'Petey Cruiser', 'Madrid'), + (202, 'Anna Sthesia', 'Barcelona'), + (203, 'Paul Molive', 'Berlin'), + (204, 'Gail Forcewind', 'Paris'), + (205, 'Paige Turner', 'Mimia'), + (206, 'Bob Frapples', 'Mexico City'), + (207, 'Walter Melon', 'Amsterdam'), + (208, 'Shonda Leer', 'São Paulo'); + + +SELECT* +FROM customers; + +ALTER TABLE invoices DROP FOREIGN KEY invoices_ibfk_1; + +ALTER TABLE customers +ADD UNIQUE INDEX unique_id_cust (id_cust); + +ALTER TABLE invoices +ADD CONSTRAINT invoices_ibfk_1 FOREIGN KEY (customer_id) REFERENCES customers(id_cust); + +SELECT * FROM sales_persons; + +ALTER TABLE sales_persons +ADD UNIQUE INDEX unique_id_staff (id_staff); + +ALTER TABLE invoices DROP FOREIGN KEY invoices_ibfk_3; +ALTER TABLE invoices +ADD CONSTRAINT invoices_ibfk_3 FOREIGN KEY (sales_id) REFERENCES sales_persons(id_staff); + +INSERT INTO invoices (inv_nr, date, customer_id, car_id, sales_id) +VALUES (852399038, '2018-08-22', 10001, 13, 203), + (731166526, '2018-12-31', 20001, 16, 205), + (271135104, '2019-01-22', 30001, 17, 207); + +SELECT * FROM customers; \ No newline at end of file diff --git a/update.sql b/update.sql new file mode 100644 index 0000000..6cebff9 --- /dev/null +++ b/update.sql @@ -0,0 +1,15 @@ +USE lab_my_sql; + +SELECT * FROM customers; + +UPDATE lab_my_sql.customers +SET email = 'ppicasso@gmail.com' +WHERE id = 104; + +UPDATE lab_my_sql.customers +SET email = 'lincoln@us.gov' +WHERE id = 105; + +UPDATE lab_my_sql.customers +SET email = 'hello@napoleon.me' +WHERE id = 106;