Skip to content
youhoc
  • Pages
    • Home
    • Modern App Guidelines
    • Linux
      • Day 1: Linux Distributions & Navigation
      • Day 2: User Management
      • Day 3: File Permission & Ownership
      • Day 4: Package Management
      • Day 5: Services Management
    • Javascript
      • JS The Weird Part
        • Execution Context
        • Types & Operators
        • Objects & Functions
        • Error Handling & Strict Mode
        • Typescript, ES6, Tra
      • Modern JS
        • JS in the Browser
        • Data Storage JSON
        • Modern JS
        • Advanced Objects & Methods
        • Webpack & Babel
        • Async
      • jQuery
        • In-depth Analysis of jQuery
      • React-ready JS
        • Arrow Function
        • Template Literals
        • Logical AND, OR, Ternary, Nullish Operators
        • Destructuring & Rest Operator
        • Array Method
        • Immutability and Spread Operator
        • Promises, Async/Await, Callback
    • PHP
      • gruntJS
      • composer
      • MySQL
    • Docker
      • Container Basics
      • Container Networking
      • Container Image
      • Container Volume & Persistent Data
      • Dockerfile
      • Docker Compose
      • Docker Registry
    • Node.js
      • Installing & Exploring
      • Loading Modules
      • npm - Get Command Input
      • Web Server
        • Express Web Server
        • Template Engine & MVC
      • File System & Input Arguments
      • 6. Deploy to Heroku & Github
      • Authentication
      • 7. Databases
      • 8. Rest API
      • Errors
      • Sequelize
        • Sequelize Transactions: Đảm Bảo Tính Toàn Vẹn Dữ Liệu
        • icon picker
          7 loại Data Types phổ biến Trong Sequelize
    • File Upload với Multer, Express.js
    • ReactJS
      • React from Andrew
        • Summary from Next
        • 1. Basics
        • 2. React Components
        • 3. Webpack
        • 4. Styling with SCSS
        • 5. React Router
        • 6. React Hook
      • Modern React From The Beginning
        • Intro to JSX
        • Vite Build Tools
        • Basic Component Creation
        • Component State
        • Props & Component Composition
        • useState with Inputs & Form Submission
        • useEffect, useRef & Local Storage
        • Async / Await and Http Request in React
        • React Router: Declarative Mode
        • ContextAPI
        • React Router: Framework Mode
          • File-routing & HTML Layouts
          • Server-side Data Query
          • Links & Navigation
          • Loaders
    • Typescript
      • Type User vs UserProp
    • Payload CMS
    • Authentication

7 loại Data Types phổ biến Trong Sequelize

Sequelize cung cấp nhiều data types tương ứng với các kiểu dữ liệu trong SQL databases (MySQL, PostgreSQL, SQLite, MSSQL…). Việc chọn đúng data type giúp:
Tối ưu performance: Dữ liệu được lưu trữ hiệu quả hơn
Tiết kiệm storage: Không lãng phí dung lượng
Đảm bảo tính chính xác: Validate data đúng chuẩn
Tương thích database: Dễ migrate giữa các DB engines

1. String Types (Chuỗi ký tự)

Chuỗi có độ dài giới hạn (VARCHAR trong SQL).
Ứng dụng thực tế:
Best Practices:
Luôn đặt giới hạn độ dài hợp lý
Username: 20-50 chars
Email: 100 chars
Name: 100-150 chars
Status/enum: 20-30 chars

TEXT

Chuỗi không giới hạn độ dài (TEXT trong SQL).
Ứng dụng thực tế:
Khi nào dùng TEXT vs STRING:
STRING: Dữ liệu ngắn, cần index, search nhanh (title, name, email)
TEXT: Dữ liệu dài, không cần index (content, description, comments)

2. Number Types (Số)

INTEGER

Số nguyên.
Ứng dụng thực tế:
Best Practices:
Dùng UNSIGNED cho số không âm (id, count, quantity)
TINYINT: Cho enum số nhỏ (0-255)
SMALLINT: Cho số vừa (population nhỏ)
INTEGER: Mặc định cho hầu hết trường hợp
BIGINT: Cho số rất lớn (timestamp milliseconds, big analytics)

FLOAT & DOUBLE

Số thập phân.
⚠️ KHÔNG nên dùng FLOAT/DOUBLE cho tiền tệ!
Ứng dụng hợp lý:

DECIMAL

Số thập phân chính xác tuyệt đối.
Ứng dụng thực tế (QUAN TRỌNG cho e-commerce):
Best Practices cho tiền tệ:

3. Date & Time Types

DATE

Date và time.
Ứng dụng thực tế:
Working with dates:

4. Boolean Type

Ứng dụng thực tế:

5. JSON Type

Lưu trữ dữ liệu JSON (PostgreSQL, MySQL 5.7+, SQLite 3.9+).
Ứng dụng thực tế:
User Settings/Preferences:
⚠️ Lưu ý khi dùng JSON:
Khó query/index hơn so với columns riêng
Không validate structure tự động
Tốt cho dữ liệu flexible, ít query
PostgreSQL JSONB tốt hơn JSON (có index, nhanh hơn)

6. ENUM Type

Giá trị từ danh sách cố định.
Ứng dụng thực tế:
ENUM vs STRING cho status:
Lợi ích ENUM:
Database-level validation
Tối ưu storage (lưu số thay vì string)
Autocomplete trong code
Rõ ràng, dễ maintain

7. UUID Type

Unique identifier.
Ứng dụng thực tế:
UUID vs Auto-increment ID:
Khi nào dùng UUID:
API public (không muốn lộ số lượng users/posts)
Distributed systems
File uploads (tránh trùng tên)
Session tokens, API keys

Tổng hợp Best Practices

1. Chọn đúng type cho từng trường hợp

Dữ liệu
Type
Lý do
Username, Email
STRING
Ngắn, có index
Post content
TEXT
Dài, không index
Price, Money
DECIMAL(10,2)
Chính xác tuyệt đối
Quantity, Count
INTEGER.UNSIGNED
Không âm
GPS coordinates
FLOAT
Không cần chính xác tuyệt đối
Status, Role
ENUM
Giá trị cố định
Created date
DATE
Timestamp
Birth date
DATEONLY
Chỉ cần ngày
Active flag
BOOLEAN
True/False
Settings
JSON
Cấu trúc linh hoạt
File ID
UUID
Unique globally
There are no rows in this table

2. Template cho các models phổ biến

User Model:
Product Model:
Order Model:
Đây là tổng hợp đầy đủ về các Data Types trong Sequelize với ứng dụng thực tế trong production. Chọn đúng type giúp database hiệu quả, dễ maintain và scale!
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.