> For the complete documentation index, see [llms.txt](https://learnsql.gitbook.io/lernmysql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learnsql.gitbook.io/lernmysql/chuan-hoa-normalization/thuc-hanh-1-2-3.md).

# Thực Hành 1,2,3

### 🎯 Bài toán: Quản lý sinh viên (Crud Cơ Bản)

#### 1. Tạo bảng

```
CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    age INT,
    email VARCHAR(100)
);
```

***

#### 2. Insert

```
INSERT INTO students (name, age, email)
VALUES ('An', 20, 'an@gmail.com');
```

***

#### 3. Select

```
SELECT * FROM students;
```

***

#### 4. Update

```
UPDATE students
SET age = 21
WHERE id = 1;
```

***

#### 5. Delete

```
DELETE FROM students
WHERE id = 1;
```

### 🎯 Bài toán: Quản lý đơn hàng (Quan Hệ Bảng)

#### 1. Thiết kế

```
users
- id
- name

orders
- id
- user_id
- total_price
```

***

#### 2. SQL tạo bảng

```
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100)
);

CREATE TABLE orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT,
    total_price DECIMAL(10,2),
    FOREIGN KEY (user_id) REFERENCES users(id)
);
```

***

#### 3. JOIN dữ liệu

```
SELECT users.name, orders.total_price
FROM orders
JOIN users ON users.id = orders.user_id;
```

### 🎯 Bài toán: Website bán hàng Bài Toán Thực Tế (Rất Quan Trọng)

### 1. Thiết kế database

#### Tables:

* users
* products
* orders
* order\_items

***

### 2. Structure

```
users
- id
- name

products
- id
- name
- price

orders
- id
- user_id
- created_at

order_items
- id
- order_id
- product_id
- quantity
```

***

### 🧠 Giải thích

👉 Vì 1 đơn hàng có nhiều sản phẩm\
→ cần bảng trung gian `order_items`

### 🎯 Bài tập

#### 🧪 Bài 1: Quản lý blog

Thiết kế:

* users
* posts
* comments

👉 Yêu cầu:

* 1 user có nhiều bài viết
* 1 bài viết có nhiều comment

***

#### 🧪 Bài 2: Hệ thống học online

Thiết kế:

* students
* courses
* enrollments

👉 Hint:

* many-to-many → bảng trung gian

***

#### 🧪 Bài 3: Quản lý công việc (TODO)

* users
* tasks
* status
