forked from LinkedInLearning/level-up-mysql-4374133
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_2_setup.sql
100 lines (83 loc) · 3.22 KB
/
chapter_2_setup.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
ALTER TABLE transactions
RENAME COLUMN `Employee Name` TO EmployeeName,
RENAME COLUMN `Sales Region` TO SalesRegion,
RENAME COLUMN `Employee Job Title` TO EmployeeJobTitle,
RENAME COLUMN `Customer ID` TO CustomerID;
UPDATE transactions, product_catalog
SET transactions.ProdName = product_catalog.ProdName
WHERE transactions.ProdNumber = product_catalog.ProdNumber;
UPDATE transactions SET OrderNum = concat('110', OrderNum)
WHERE OrderNum NOT LIKE '110%' AND OrderNum NOT LIKE '111%';
UPDATE transactions SET OrderNum = replace(OrderNum, '-', '');
UPDATE transactions SET OrderNum = replace(OrderNum, ']', '');
UPDATE transactions SET OrderNum = replace(OrderNum, '00', '')
WHERE LENGTH(OrderNum) > 7;
CREATE TABLE employee (
EmpID INT NOT NULL,
EmployeeName VARCHAR(100) NOT NULL,
EmployeeJobTitle TEXT NOT NULL,
SalesRegion VARCHAR(50) NOT NULL,
PRIMARY KEY (EmpID));
CREATE TABLE customer (
CustomerID INT NOT NULL,
CustName VARCHAR(100) NOT NULL,
CustState VARCHAR(45) NOT NULL,
CustomerType VARCHAR(45) NOT NULL,
PRIMARY KEY (CustomerID));
CREATE TABLE product (
ProdNumber VARCHAR(6) NOT NULL,
ProdName VARCHAR(100) NOT NULL,
Price DECIMAL(10,2) NOT NULL,
ProductActive VARCHAR(3) NOT NULL,
PRIMARY KEY (ProdNumber));
CREATE TABLE orders (
OrderNum INT NOT NULL,
OrderDate TEXT NOT NULL,
EmpID INT NOT NULL,
CustomerID INT NOT NULL,
ProdNumber VARCHAR(6) NOT NULL,
OrderType VARCHAR(15) NOT NULL,
Quantity INT NOT NULL,
Discount DECIMAL(2,2) NOT NULL,
PRIMARY KEY (OrderNum));
ALTER TABLE orders
ADD CONSTRAINT idx_orders_customerid_fk
FOREIGN KEY (CustomerID)
REFERENCES customer (CustomerID),
ADD CONSTRAINT idx_orders_empid_fk
FOREIGN KEY (EmpID)
REFERENCES employee (EmpID),
ADD CONSTRAINT idx_orders_prodnumber_fk
FOREIGN KEY (ProdNumber)
REFERENCES product (ProdNumber);
UPDATE transactions SET Price = replace(Price, '$', '');
UPDATE transactions SET Discount = replace(Discount, '%', '');
UPDATE transactions SET Discount = Discount / 100;
INSERT INTO customer
SELECT DISTINCT CustomerID, CustName, CustState, CustomerType FROM transactions;
INSERT INTO employee
SELECT DISTINCT EmpID, EmployeeName, EmployeeJobTitle, SalesRegion FROM transactions;
INSERT INTO product
SELECT DISTINCT ProdNumber, ProdName, Price, ProductActive FROM transactions;
INSERT INTO orders
SELECT DISTINCT OrderNum, OrderDate, EmpID, CustomerID, ProdNumber, OrderType, Quantity, Discount FROM transactions;
CREATE VIEW sales_transactions AS
SELECT orders.OrderDate, employee.EmployeeName,
customer.CustName, product.ProdName,
product.Price, orders.Quantity,
product.Price * orders.Quantity AS SalePrice FROM orders
JOIN product ON product.ProdNumber = orders.ProdNumber
JOIN customer USING (CustomerID)
JOIN employee USING (EmpID)
ORDER BY orders.OrderDate DESC;
ALTER TABLE orders
CHANGE COLUMN OrderNum
OrderNum INT NOT NULL AUTO_INCREMENT;
SELECT EmpID FROM employee
WHERE EmployeeName = 'Kendra Morales' INTO @empid;
SELECT CustomerID FROM customer
WHERE CustName = 'Hashim Colerick' INTO @customerid;
SELECT ProdNumber FROM product
WHERE ProdName = 'Mineral Water - Orange - 32oz - case' INTO @prodnumber;
INSERT INTO orders VALUES (DEFAULT, '5/24/2023', @empid, @customerid,
@prodnumber, 'Retail', 22, 0);