> 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/chua-bai-thuc-hanh-2.md).

# Chữa bài Thực Hành 2

Tôi thiết kế Hệ thống học online: students: id,name,email courses: id,title,body enrollments: id,is\_active,student\_id,sourse\_id

Bạn đang đi **đúng hướng rồi**, vì bạn đã nhận ra cần bảng `enrollments` (many-to-many) 👍\
Nhưng nếu review kiểu “đi làm thật / senior level” thì vẫn có vài lỗi quan trọng cần sửa.

***

## 🔍 THIẾT KẾ CỦA BẠN

```
students: id, name, email
courses: id, title, body
enrollments: id, is_active, student_id, sourse_id
```

***

## ❌ CÁC VẤN ĐỀ

### 1. ❌ Sai chính tả: `sourse_id`

👉 Phải là:

```
course_id
```

***

### 2. ⚠️ `enrollments` thiếu ý nghĩa business

Hiện tại:

```
is_active
```

👉 Không sai, nhưng **quá yếu**, không phản ánh thực tế

***

### 3. ⚠️ Thiếu ràng buộc quan trọng

* Không có UNIQUE (student\_id, course\_id)\
  👉 Có thể enroll trùng → bug

***

### 4. ⚠️ Thiếu timestamp

👉 Không biết:

* học từ khi nào
* học xong chưa

***

## ✅ THIẾT KẾ CHUẨN (REAL-WORLD)

```
students
- id (PK)
- name
- email (UNIQUE)

courses
- id (PK)
- title
- body

enrollments
- id (PK)
- student_id (FK)
- course_id (FK)
- status
- enrolled_at
```

***

## 🧠 GIẢI THÍCH (CỰC QUAN TRỌNG)

### 1. 🎯 Many-to-Many

* 1 student → nhiều course
* 1 course → nhiều student

👉 → cần bảng trung gian ✅

***

### 2. 🎯 `status` tốt hơn `is_active`

Thay vì:

```
is_active (true/false)
```

👉 dùng:

```
status:
- enrolled
- learning
- completed
- dropped
```

👉 Đây là tư duy **system thật**

***

### 3. 🎯 UNIQUE constraint

```
UNIQUE(student_id, course_id)
```

👉 tránh:

* enroll 2 lần cùng course

***

### 4. 🎯 Timestamp

```
enrolled_at
completed_at (optional)
```

👉 cực quan trọng cho:

* report
* analytics
* tracking

***

## 🔥 SQL CHUẨN

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

CREATE TABLE courses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255),
    body TEXT
);

CREATE TABLE enrollments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT,
    course_id INT,
    status VARCHAR(20),
    enrolled_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    UNIQUE(student_id, course_id),

    FOREIGN KEY (student_id) REFERENCES students(id),
    FOREIGN KEY (course_id) REFERENCES courses(id)
);
```

***

## 💎 LEVEL UP (NẾU LÀ SYSTEM THẬT)

### Có thể thêm:

```
courses
- price
- teacher_id

students
- created_at

enrollments
- progress (%)
```

***

## 📊 ĐÁNH GIÁ THIẾT KẾ CỦA BẠN

| Tiêu chí               | Đánh giá   |
| ---------------------- | ---------- |
| Nhận ra many-to-many   | ✅ Rất tốt  |
| Tư duy bảng trung gian | ✅ Đúng     |
| Naming                 | ⚠️ Sai nhẹ |
| Business logic         | ⚠️ Còn yếu |
| Production-ready       | ❌ Chưa     |

👉 Tổng thể: **7/10 (khá tốt cho level đang học)**
