diff --git a/your-code/ERD.jpg b/your-code/ERD.jpg new file mode 100644 index 0000000..8de1afd Binary files /dev/null and b/your-code/ERD.jpg differ diff --git a/your-code/ERD.pptx b/your-code/ERD.pptx new file mode 100644 index 0000000..e39255f Binary files /dev/null and b/your-code/ERD.pptx differ diff --git a/your-code/create.sql b/your-code/create.sql index e69de29..7c6ea3c 100644 --- a/your-code/create.sql +++ b/your-code/create.sql @@ -0,0 +1,60 @@ +/*Create the (empty) mySQL database*/ +SELECT CURRENT_USER(); + +CREATE DATABASE IF NOT EXISTS lab_mysql; + +/*Define the database that will be used in the queries*/ +USE lab_mysql; + +/*Populate the database with tables*/ +CREATE TABLE IF NOT EXISTS Cars( + ID INT PRIMARY KEY AUTO_INCREMENT, + VIN VARCHAR(17) NOT NULL, + Manufacturer VARCHAR(40) NOT NULL, + Model VARCHAR(40), + `Year` YEAR, + Color VARCHAR(30) + ); + +SELECT * FROM Cars; + + +CREATE TABLE IF NOT EXISTS Customers( + ID INT PRIMARY KEY AUTO_INCREMENT, + `Customer ID` INT NOT NULL, + `Name` VARCHAR(100) NOT NULL, + Phone VARCHAR(20), + Email VARCHAR(50), + Address VARCHAR(100) NOT NULL, + City VARCHAR(50) NOT NULL, + `State/Province` VARCHAR(50), + Country VARCHAR(50) NOT NULL, + Postal VARCHAR(10) + ); + +SELECT * FROM Customers; + + +CREATE TABLE IF NOT EXISTS Salespersons( + ID INT PRIMARY KEY AUTO_INCREMENT, + `Staff ID` VARCHAR(5) NOT NULL, + `Name` VARCHAR(100), + Store VARCHAR(50) + ); + +SELECT * FROM Salespersons; + + +CREATE TABLE IF NOT EXISTS Invoices( + ID INT PRIMARY KEY AUTO_INCREMENT, + `Invoice Number` INT NOT NULL, + `Date` DATE NOT NULL, + Car INT, + Customer INT, + `Sales Person` INT, + FOREIGN KEY (Car) REFERENCES Cars(ID), + FOREIGN KEY (Customer) REFERENCES Customers(ID), + FOREIGN KEY (`Sales Person`) REFERENCES Salespersons(ID) + ); + +SELECT * FROM Invoices; \ No newline at end of file diff --git a/your-code/delete.sql b/your-code/delete.sql index e69de29..b3ed656 100644 --- a/your-code/delete.sql +++ b/your-code/delete.sql @@ -0,0 +1,19 @@ +USE lab_mysql; + +/*To find duplicate entries*/ + +SELECT VIN, COUNT(*) as count +FROM Cars +GROUP BY VIN +HAVING count > 1; + + +/*Delete*/ +SET SQL_SAFE_UPDATES = 0; + +SELECT * FROM Cars; + +DELETE FROM Cars +WHERE (ID = 4); + +SET SQL_SAFE_UPDATES = 1; \ No newline at end of file diff --git a/your-code/seeding.sql b/your-code/seeding.sql index e69de29..6f60747 100644 --- a/your-code/seeding.sql +++ b/your-code/seeding.sql @@ -0,0 +1,47 @@ +/*In order to start the auto-increment attribute in mySQL starting from 0 instead of (default) 1*/ +SET sql_mode = "NO_AUTO_VALUE_ON_ZERO"; + +USE lab_mysql; + +INSERT INTO Cars +VALUES +(0, '3K096I98581DHSNUP', 'Volkswagen', 'Tiguan', 2019, 'Blue'), +(1, 'ZM8G7BEUQZ97IH46V', 'Peugeot', 'Rifter', 2019, 'Red'), +(2, 'RKXVNNIHLVVZOUB4M', 'Ford', 'Fusion', 2018, 'White'), +(3, 'HKNDGS7CU31E9Z7JW', 'Toyota', 'RAV4', 2018, 'Silver'), +(4, 'DAM41UDN3CHU2WVF6', 'Volvo', 'V60', 2019, 'Gray'), +(5, 'DAM41UDN3CHU2WVF6', 'Volvo', 'V60 Cross Country', 2019, 'Gray'); + +SELECT * FROM Cars; + + +INSERT INTO Customers +VALUES +(0, 10001, 'Pablo Picasso', '+34 636 17 63 82', '-', 'Paseo de la Chopera, 14', 'Madrid', 'Madrid', 'Spain', '28045'), +(1, 20001, 'Abraham Lincoln', '+1 305 907 7086', '-', '120 SW 8th St', 'Miami', 'Florida', 'United States', '33130'), +(2, 30001, 'Napoléon Bonaparte', '+33 1 79 75 40 00', '-', '40 Rue du Colisée', 'Paris', 'Île-de-France', 'France', '75008'); + +SELECT * FROM Customers; + + +INSERT INTO Salespersons +VALUES +(0, '00001', 'Petey Cruiser', 'Madrid'), +(1, '00002', 'Anna Sthesia', 'Barcelona'), +(2, '00003', 'Paul Molive', 'Berlin'), +(3, '00004', 'Gail Forcewind', 'Paris'), +(4, '00005', 'Paige Turner', 'Mimia'), +(5, '00006', 'Bob Frapples', 'Mexico City'), +(6, '00007', 'Walter Melon', 'Amsterdam'), +(7, '00008', 'Shonda Leer', 'São Paulo'); + +SELECT * FROM Salespersons; + + +INSERT INTO Invoices +VALUES +(0, '852399038', STR_TO_DATE('22-08-2018', '%d-%m-%Y'), 0, 1, 3), +(1, '731166526', STR_TO_DATE('31-12-2018', '%d-%m-%Y'), 3, 0, 5), +(2, '271135104', STR_TO_DATE('22-01-2019', '%d-%m-%Y'), 2, 2, 7); + +SELECT * FROM Invoices; \ No newline at end of file diff --git a/your-code/update.sql b/your-code/update.sql index e69de29..00beca8 100644 --- a/your-code/update.sql +++ b/your-code/update.sql @@ -0,0 +1,29 @@ +USE lab_mysql; + +/*Deactivate safe updates on mySQL*/ +SET SQL_SAFE_UPDATES = 0; + +/*Update*/ +SELECT * FROM Salespersons; + +UPDATE Salespersons +SET store = 'Miami' +WHERE (ID = 4); + +SELECT * FROM Customers; + +UPDATE Customers +SET Email = 'ppicasso@gmail.com' +WHERE (ID = 0); + +UPDATE Customers +SET Email = 'lincoln@us.gov' +WHERE (ID = 1); + +UPDATE Customers +SET Email = 'hello@napoleon.me' +WHERE (ID = 2); + + +/*Activate safe updates on mySQL*/ +SET SQL_SAFE_UPDATES = 1; \ No newline at end of file