Day 10 - Advanced: Views & Data Manipulation
Bối cảnh: Hệ thống E-commerce (Thương mại điện tử)
1. UPDATE
UPDATE: Dùng để cập nhật dữ liệu trong bảng.
Ví dụ:
UPDATE products SET price = price * 0.9 WHERE category = 'Electronics';
Câu lệnh này sẽ giảm giá 10% cho tất cả sản phẩm thuộc danh mục "Electronics".
2. Challenge: UPDATE
- Yêu cầu: Cập nhật trạng thái (status) của tất cả đơn hàng trong bảng orders thành "Shipped" nếu ngày đặt hàng (order_date) là trước ngày 1 tháng 10 năm 2023.
3. Solution: UPDATE
Giải pháp:
UPDATE orders SET status = 'Shipped' WHERE order_date < '2023-10-01';
4. DELETE
DELETE: Dùng để xóa dữ liệu từ bảng.
Ví dụ:
DELETE FROM customers WHERE customer_id = 101;
Câu lệnh này sẽ xóa khách hàng có customer_id = 101.
5. Challenge: DELETE
- Yêu cầu: Xóa tất cả đơn hàng trong bảng orders có tổng giá trị (total_amount) nhỏ hơn 10.
6. Solution: DELETE
Giải pháp:
DELETE FROM orders WHERE total_amount < 10;
7. CREATE TABLE AS
CREATE TABLE AS: Dùng để tạo một bảng mới từ kết quả của một truy vấn.
Ví dụ:
CREATE TABLE high_value_orders AS SELECT * FROM orders WHERE total_amount > 500;
Câu lệnh này sẽ tạo bảng high_value_orders chứa các đơn hàng có tổng giá trị lớn hơn 500.
8. Challenge: CREATE TABLE AS
- Yêu cầu: Tạo bảng vip_customers chứa thông tin khách hàng có tổng giá trị đơn hàng (total_amount) lớn hơn 1000.
9. Solution: CREATE TABLE AS
Giải pháp:
CREATE TABLE vip_customers AS SELECT c.customer_id, c.customer_name, SUM(o.total_amount) AS total_spent FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id, c.customer_name HAVING SUM(o.total_amount) > 1000;
10. CREATE VIEW
CREATE VIEW: Dùng để tạo một view (khung nhìn) từ kết quả của một truy vấn.
Ví dụ:
CREATE VIEW top_products AS SELECT product_name, SUM(quantity) AS total_quantity FROM orders GROUP BY product_name ORDER BY total_quantity DESC LIMIT 10;
Câu lệnh này sẽ tạo view top_products chứa 10 sản phẩm bán chạy nhất.
11. Challenge: CREATE VIEW
- Yêu cầu: Tạo view monthly_revenue để hiển thị tổng doanh thu (total_amount) theo từng tháng.
12. Solution: CREATE VIEW
Giải pháp:
CREATE VIEW monthly_revenue AS SELECT EXTRACT(MONTH FROM order_date) AS order_month, SUM(total_amount) AS total_revenue FROM orders GROUP BY order_month ORDER BY order_month;
13. CREATE MATERIALIZED VIEW
CREATE MATERIALIZED VIEW: Tạo một materialized view (khung nhìn vật chất) để lưu trữ kết quả của một truy vấn.
Ví dụ:
CREATE MATERIALIZED VIEW top_customers AS SELECT c.customer_name, SUM(o.total_amount) AS total_spent FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_name ORDER BY total_spent DESC LIMIT 10;
Câu lệnh này sẽ tạo materialized view top_customers chứa 10 khách hàng có tổng giá trị đơn hàng cao nhất.
14. Managing views
Quản lý views: Có thể cập nhật hoặc xóa views.
Ví dụ:
DROP VIEW top_products;
Câu lệnh này sẽ xóa view top_products.
15. Challenge: Managing views
- Yêu cầu: Xóa view monthly_revenue.
16. Solution: Managing views
Giải pháp:
DROP VIEW monthly_revenue;
17. Import & Export
Import: Nhập dữ liệu từ file vào bảng.
Export: Xuất dữ liệu từ bảng ra file.
Ví dụ (sử dụng PostgreSQL):
-- Import dữ liệu từ file CSV vào bảng products COPY products FROM '/path/to/products.csv' DELIMITER ',' CSV HEADER; -- Export dữ liệu từ bảng orders ra file CSV COPY orders TO '/path/to/orders.csv' DELIMITER ',' CSV HEADER;
18. Today's slides
Tổng kết lại các kiến thức về views và thao tác dữ liệu.
Cung cấp slide tham khảo để bạn ôn tập và thực hành thêm.
19. Today's summary
Hôm nay, bạn đã học:
Cách sử dụng UPDATE và DELETE để thao tác dữ liệu.
Cách tạo bảng mới từ truy vấn với CREATE TABLE AS.
Cách tạo và quản lý views và materialized views.
Cách nhập và xuất dữ liệu.
Hẹn gặp lại bạn vào ngày 11 với chủ đề Pro: Window Functions!
20. Today's challenges
Thử thách 1: Tạo view customer_orders để hiển thị thông tin đơn hàng (order_id, customer_name, total_amount) của từng khách hàng.
Thử thách 2: Tạo materialized view product_sales để hiển thị tổng số lượng bán (total_quantity) của từng sản phẩm.
Thử thách 3: Xuất dữ liệu từ bảng customers ra file CSV.