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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
| -- 기존 테이블 삭제
DROP TABLE IF EXISTS ORDER_ITEM;
DROP TABLE IF EXISTS `ORDER`;
DROP TABLE IF EXISTS PRODUCT;
-- PRODUCT 테이블 생성
CREATE TABLE PRODUCT (
ID INT AUTO_INCREMENT PRIMARY KEY,
LABEL VARCHAR(255) NOT NULL,
PRICE DECIMAL(10,2) NOT NULL,
FIELD VARCHAR(255) NOT NULL,
CREATED_AT DATETIME NOT NULL
);
create index PRODUCT_CREATED_AT_index
on PRODUCT (CREATED_AT);
-- ORDER 테이블 생성
CREATE TABLE `ORDER` (
ID INT AUTO_INCREMENT PRIMARY KEY,
CUSTOMER_NAME VARCHAR(255) NOT NULL,
SHIPPING_COST DECIMAL(10,2) NOT NULL,
TOTAL_ORDER_PRICE DECIMAL(10,2) NOT NULL,
CREATED_AT DATETIME NOT NULL
);
-- ORDER_ITEM 테이블 생성
CREATE TABLE ORDER_ITEM (
ID INT AUTO_INCREMENT PRIMARY KEY,
ORDER_ID INT NOT NULL,
PRODUCT_ID INT NOT NULL,
PRODUCT_NAME VARCHAR(255) NOT NULL,
PRICE DECIMAL(10,2) NOT NULL,
QUANTITY INT NOT NULL,
TOTAL_PRICE DECIMAL(10,2) NOT NULL,
CREATED_AT DATETIME NOT NULL,
FOREIGN KEY (ORDER_ID) REFERENCES `ORDER`(ID),
FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCT(ID)
);
DELIMITER //
CREATE PROCEDURE POPULATE_TABLES()
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE j INT DEFAULT 1;
DECLARE item_count INT;
DECLARE shipping DECIMAL(10,2);
DECLARE total DECIMAL(10,2);
DECLARE product_price DECIMAL(10,2);
DECLARE order_id INT;
DECLARE product_id INT;
DECLARE price DECIMAL(10,2);
DECLARE quantity INT;
DECLARE total_price DECIMAL(10,2);
DECLARE created_at DATETIME;
-- 1. PRODUCT 테이블에 데이터 삽입
WHILE i <= 500000 DO
SET created_at = DATE_ADD('2020-01-01', INTERVAL FLOOR(RAND() * 3650) DAY) + INTERVAL FLOOR(RAND() * 86400) SECOND;
INSERT INTO PRODUCT (LABEL, PRICE, FIELD, CREATED_AT)
VALUES (
CONCAT('Product ', i),
ROUND(RAND() * 1000, 2), -- 가격: 0 ~ 1000 사이의 랜덤 값
CONCAT('Field ', FLOOR(RAND() * 10) + 1), -- 필드: Field 1 ~ Field 10
created_at
);
SET i = i + 1;
-- 진행 상황 출력 (선택 사항)
IF MOD(i, 10000) = 0 THEN
SELECT CONCAT('Inserted ', i, ' PRODUCTS');
END IF;
END WHILE;
SET i = 1;
-- 2. ORDER 테이블에 데이터 삽입
WHILE i <= 500000 DO
SET shipping = ROUND(RAND() * 50, 2); -- 배송비: 0 ~ 50 사이의 랜덤 값
-- 총 주문 가격을 배송비로 초기화
SET total = shipping;
-- 임의의 상품 수 (1 ~ 5개)를 선택하여 총 가격 계산
SET item_count = FLOOR(RAND() * 5) + 1;
SET j = 1;
WHILE j <= item_count DO
SET product_price = ROUND(RAND() * 1000, 2);
SET total = total + product_price;
SET j = j + 1;
END WHILE;
-- 무작위로 고객 이름 할당 (5명)
SET @rand_num = FLOOR(1 + RAND() * 5);
SET @customer = CASE @rand_num
WHEN 1 THEN 'Customer A'
WHEN 2 THEN 'Customer B'
WHEN 3 THEN 'Customer C'
WHEN 4 THEN 'Customer D'
ELSE 'Customer E'
END;
-- 무작위 생성 날짜
SET created_at = DATE_ADD('2020-01-01', INTERVAL FLOOR(RAND() * 3650) DAY) + INTERVAL FLOOR(RAND() * 86400) SECOND;
INSERT INTO `ORDER` (CUSTOMER_NAME, SHIPPING_COST, TOTAL_ORDER_PRICE, CREATED_AT)
VALUES (
@customer,
shipping,
total,
created_at
);
SET i = i + 1;
-- 진행 상황 출력 (선택 사항)
IF MOD(i, 10000) = 0 THEN
SELECT CONCAT('Inserted ', i, ' ORDERS');
END IF;
END WHILE;
SET i = 1;
-- 3. ORDER_ITEM 테이블에 데이터 삽입
WHILE i <= 500000 DO
SET order_id = FLOOR(RAND() * 100000) + 1; -- 1 ~ 100000
SET product_id = FLOOR(RAND() * 100000) + 1; -- 1 ~ 100000
SET price = ROUND(RAND() * 1000, 2); -- 0 ~ 1000
SET quantity = FLOOR(RAND() * 5) + 1; -- 1 ~ 5
SET total_price = ROUND(price * quantity, 2);
SET created_at = DATE_ADD('2020-01-01', INTERVAL FLOOR(RAND() * 3650) DAY) + INTERVAL FLOOR(RAND() * 86400) SECOND;
INSERT INTO ORDER_ITEM (ORDER_ID, PRODUCT_ID, PRODUCT_NAME, PRICE, QUANTITY, TOTAL_PRICE, CREATED_AT)
VALUES (
order_id,
product_id,
CONCAT('Product ', product_id),
price,
quantity,
total_price,
created_at
);
SET i = i + 1;
-- 진행 상황 출력 (선택 사항)
IF MOD(i, 10000) = 0 THEN
SELECT CONCAT('Inserted ', i, ' ORDER_ITEMS');
END IF;
END WHILE;
END //
DELIMITER ;
CALL POPULATE_TABLES();
|