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 diagram.mwb
Binary file not shown.
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.
56 changes: 56 additions & 0 deletions create.sql
Original file line number Diff line number Diff line change
@@ -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;
Binary file added erd1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions seeding.sql
Original file line number Diff line number Diff line change
@@ -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);
20 changes: 20 additions & 0 deletions update&delete.sql
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]'
WHERE cust_name = 'Pablo Picasso';

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

UPDATE customers SET cust_email = '[email protected]'
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;
12 changes: 12 additions & 0 deletions verify.sql
Original file line number Diff line number Diff line change
@@ -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;