Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ERD.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions create.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
-- Create the database
CREATE DATABASE IF NOT EXISTS lab_mysql;
USE lab_mysql;

-- Drop tables if they already exist
DROP TABLE IF EXISTS invoices;
DROP TABLE IF EXISTS cars;
DROP TABLE IF EXISTS customers;
DROP TABLE IF EXISTS salespersons;

-- Create Cars table
CREATE TABLE cars (
car_id INT AUTO_INCREMENT PRIMARY KEY,
vin VARCHAR(50) NOT NULL UNIQUE,
manufacturer VARCHAR(50) NOT NULL,
model VARCHAR(50) NOT NULL,
year YEAR NOT NULL,
color VARCHAR(30)
);

-- Create Customers table
CREATE TABLE customers (
customer_pk INT AUTO_INCREMENT PRIMARY KEY,
customer_id VARCHAR(50) NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL,
phone_number VARCHAR(20),
email VARCHAR(100),
address VARCHAR(200),
city VARCHAR(50),
state_province VARCHAR(50),
country VARCHAR(50),
zip_postal_code VARCHAR(20)
);

-- Create Salespersons table
CREATE TABLE salespersons (
salesperson_pk INT AUTO_INCREMENT PRIMARY KEY,
staff_id VARCHAR(50) NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL,
store VARCHAR(100)
);

-- Create Invoices table
CREATE TABLE invoices (
invoice_pk INT AUTO_INCREMENT PRIMARY KEY,
invoice_number VARCHAR(50) NOT NULL UNIQUE,
invoice_date DATE NOT NULL,
car_id INT NOT NULL,
customer_pk INT NOT NULL,
salesperson_pk INT NOT NULL,
FOREIGN KEY (car_id) REFERENCES cars(car_id),
FOREIGN KEY (customer_pk) REFERENCES customers(customer_pk),
FOREIGN KEY (salesperson_pk) REFERENCES salespersons(salesperson_pk)
);
5 changes: 5 additions & 0 deletions delete.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
USE lab_mysql;

-- Delete duplicate car entry
DELETE FROM cars
WHERE car_id = 4;
37 changes: 37 additions & 0 deletions seeding.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
USE lab_mysql;

-- Cars seeding
INSERT INTO cars (car_id, vin, manufacturer, 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, 'DAM56UDN3CHU2WVF6', 'Volvo', 'V60 Cross Country', 2019, 'Gray');

-- Customers seeding
INSERT INTO customers (customer_pk, customer_id, name, phone_number, email, address, city, state_province, country, zip_postal_code)
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 seeding
INSERT INTO salespersons (salesperson_pk, staff_id, name, store)
VALUES
(1, '00001', 'Petey Cruiser', 'Madrid'),
(2, '00002', 'Anna Sthesia', 'Barcelona'),
(3, '00003', 'Paul Molive', 'Berlin'),
(4, '00004', 'Gail Forcewind', 'Paris'),
(5, '00005', 'Paige Turner', 'Mimia'),
(6, '00006', 'Bob Frapples', 'Mexico City'),
(7, '00007', 'Walter Melon', 'Amsterdam'),
(8, '00008', 'Shonda Leer', 'São Paulo');

-- Invoices seeding
INSERT INTO invoices (invoice_pk, invoice_number, invoice_date, car_id, customer_pk, salesperson_pk)
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);
19 changes: 19 additions & 0 deletions update.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
USE lab_mysql;

SET SQL_SAFE_UPDATES = 0;

-- Update email
UPDATE customers
SET email = '[email protected]'
WHERE name = 'Pablo Picasso';

UPDATE customers
SET email = '[email protected]'
WHERE name = 'Abraham Lincoln';

UPDATE customers
SET email = '[email protected]'
WHERE name = 'Napoléon Bonaparte';


SET SQL_SAFE_UPDATES = 1;