1️⃣ 생성 (CREATE)
🟢 초급
- 고객 추가 (customers)
INSERT INTO customers (name, address) VALUES ('John Doe', '123 Main St');
- 제품 추가 (products)
INSERT INTO products (name, price) VALUES ('Toy Car', 19.99);
- 직원 추가 (employees)
INSERT INTO employees (firstName, lastName) VALUES ('Alice', 'Johnson');
- 사무실 추가 (offices)
INSERT INTO offices (city, phone) VALUES ('San Francisco', '123-456-7890');
- 주문 추가 (orders)
INSERT INTO orders (orderDate, customerID) VALUES ('2023-01-01', 1);
- 주문 상세 추가 (orderdetails)
INSERT INTO orderdetails (orderID, productID, quantityOrdered, priceEach) VALUES (1, 1, 2, 20.00);
- 지불 정보 추가 (payments)
INSERT INTO payments (customerID, amount, paymentDate) VALUES (1, 200.00, '2023-01-01');
- 제품 라인 추가 (productlines)
INSERT INTO productlines (productLine, textDescription) VALUES ('Classic Cars', 'Various classic cars models');
🟡 중급
- 여러 고객 추가
INSERT INTO customers (name, address) VALUES ('Bob Brown', '789 Oak St'), ('Sue White', '321 Pine St');
- 여러 제품 추가
INSERT INTO products (name, price) VALUES ('Model Ship', 22.99), ('Model Plane', 18.99);
- 여러 직원 추가
INSERT INTO employees (firstName, lastName) VALUES ('Gary', 'Martin'), ('Laura', 'Green');
- 주문 및 주문 상세 추가
INSERT INTO orders (orderDate, customerID) VALUES ('2023-01-02', 2);
INSERT INTO orderdetails (orderID, productID, quantityOrdered, priceEach) VALUES (LAST_INSERT_ID(), 3, 1, 15.00), (LAST_INSERT_ID(), 4, 2, 35.00);
🔴 고급
- 고객 추가 후 바로 주문 추가
INSERT INTO customers (name, address) VALUES ('Linda Grey', '654 Maple St');
INSERT INTO orders (customerID, orderDate) VALUES (LAST_INSERT_ID(), '2023-01-05');
INSERT INTO orderdetails (orderID, productID, quantityOrdered, priceEach) VALUES (LAST_INSERT_ID(), 5, 2, 45.00);
2️⃣ 읽기 (READ)
🟢 초급
- 모든 고객 정보 조회
SELECT * FROM customers;
- 모든 제품 조회
SELECT name, price FROM products;
🟡 중급
- 특정 고객의 모든 주문 조회
SELECT * FROM orders WHERE customerID = 2;
- 특정 제품의 주문 상세 조회
SELECT * FROM orderdetails WHERE productID = 3;
🔴 고급
- 지역별 고객 수 조회
SELECT city, COUNT(*) AS customerCount FROM customers GROUP BY city;
3️⃣ 갱신 (UPDATE)
🟢 초급
- 고객 주소 변경
UPDATE customers SET address = '456 Updated St' WHERE customerID = 1;
- 제품 가격 변경
UPDATE products SET price = 29.99 WHERE productID = 1;
🟡 중급
- 여러 직원 부서 변경
UPDATE employees SET officeCode = 2 WHERE department = 'Sales';
- 주문 상태 변경
UPDATE orders SET status = 'Cancelled' WHERE orderDate BETWEEN '2022-12-01' AND '2022-12-31';
🔴 고급
- 작년 주문 상태 변경
UPDATE orders SET status = 'On Hold' WHERE orderDate BETWEEN '2022-01-01' AND '2022-12-31';
4️⃣ 삭제 (DELETE)
🟢 초급
- 특정 고객 삭제
DELETE FROM customers WHERE customerID = 1;
- 특정 제품 삭제
DELETE FROM products WHERE productID = 1;
🟡 중급
- 특정 부서의 모든 직원 삭제
DELETE FROM employees WHERE department = 'Sales';
- 지난 달 모든 주문 삭제
DELETE FROM orders WHERE orderDate BETWEEN '2022-12-01' AND '2022-12-31';
🔴 고급
- 재고 없는 모든 제품 삭제
DELETE FROM products WHERE quantityInStock = 0;
- 1년간 활동 없는 고객 삭제
DELETE FROM customers WHERE lastOrderDate < '2022-01-01';
💡 Tip: 항상 DELETE와 UPDATE 문은 조건(WHERE)을 명확히 하여 실수를 방지하세요.
이 챌린지를 통해 SQL 문법을 단계별로 마스터하세요! 🚀
'과제' 카테고리의 다른 글
📌Mongo DB 심화 5문제 with python (0) | 2025.03.27 |
---|---|
페어 과제 (0) | 2025.03.24 |
Database- 데이터베이스 (문제풀이 3일차) (0) | 2025.03.20 |
Database- 데이터베이스 (문제풀이 2일차) (0) | 2025.03.19 |
Database- 데이터베이스 (문제풀이 1일차) (0) | 2025.03.18 |