-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.sql
61 lines (47 loc) · 1.61 KB
/
orders.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
CREATE TABLE orders
(
id INT NOT NULL AUTO_INCREMENT,
total INT NOT NULL,
order_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE = InnoDB;
DESCRIBE orders;
CREATE TABLE orders_detail
(
id_product VARCHAR(10) NOT NULL,
id_order INT NOT NULL,
price INT NOT NULL,
quantity INT NOT NULL,
PRIMARY KEY (id_product, id_order)
) ENGINE = InnoDB;
DESCRIBE orders_detail;
ALTER TABLE orders_detail
ADD CONSTRAINT fk_orders_detail_product
FOREIGN KEY (id_product) REFERENCES products (id);
ALTER TABLE orders_detail
ADD CONSTRAINT fk_orders_detail_orders
FOREIGN KEY (id_order) REFERENCES orders (id);
SHOW CREATE TABLE orders_detail;
SELECT *
FROM orders;
INSERT INTO orders(total)
VALUES (50000);
INSERT INTO orders_detail(id_product, id_order, price, quantity)
VALUES ('P0001', 1, 25000, 1),
('P0002', 1, 25000, 1);
INSERT INTO orders_detail(id_product, id_order, price, quantity)
VALUES ('P0003', 2, 25000, 1),
('P0004', 3, 25000, 1);
INSERT INTO orders_detail(id_product, id_order, price, quantity)
VALUES ('P0001', 2, 25000, 1),
('P0003', 3, 25000, 1);
SELECT *
FROM orders_detail;
SELECT *
FROM orders
JOIN orders_detail ON (orders_detail.id_order = orders.id)
JOIN products ON (products.id = orders_detail.id_product);
SELECT orders.id, products.id, products.name, orders_detail.quantity, orders_detail.price
FROM orders
JOIN orders_detail ON (orders_detail.id_order = orders.id)
JOIN products ON (products.id = orders_detail.id_product);