Quy ước đặt tên (Naming Conventions)

1. Tên bảng

Quy tắc: t_huca_<tên_số_nhiều> — chữ thường, dùng dấu gạch dưới.
LoạiVí dụ
Thực thể đơnt_huca_users, t_huca_events, t_huca_companies
Bảng quan hệ N:Nt_huca_group_members, t_huca_user_roles
Bảng danh mụct_huca_provinces, t_huca_faculties, t_huca_roles
Bảng logt_huca_audit_logs, t_huca_email_logs
-- ✅ Đúng
t_huca_users
t_huca_event_registrations
t_huca_liaison_committees

-- ❌ Sai
Users
HUCA_USER
tbl_Event

2. Tên trường (Columns)

Quy tắc: TitleCase (PascalCase) — viết hoa chữ cái đầu mỗi từ.

Khóa chính

Id   BIGINT IDENTITY(1,1) PRIMARY KEY   -- Luôn là "Id", không phải "UserId" hay "ID"

Khóa ngoại

<TênThựcThểCha>Id   -- VD: UserId, FacultyId, EventId, CompanyId

Trường thông thường

-- ✅ Đúng
FullName, DateOfBirth, PhoneNumber, IsActive, CreatedAt, UpdatedAt
JobTitle, WorkField, AvatarUrl, QrCode

-- ❌ Sai
full_name, date_of_birth, PHONE_NUMBER, isactive, created_at

Trường Boolean

Dùng tiền tố Is hoặc Has:
IsActive, IsVerified, IsApproved, IsPublished, IsFeatured
IsAnonymous, IsRemote, IsPinned, IsMuted
ShowEmail, ShowPhone, ShowAddress   -- Cài đặt hiển thị

Trường ngày giờ

CreatedAt    DATETIME2   -- Thời điểm tạo
UpdatedAt    DATETIME2   -- Thời điểm cập nhật gần nhất
DeletedAt    DATETIME2   -- Soft delete timestamp
StartDate    DATE        -- Ngày (không có giờ)
StartDateTime DATETIME2  -- Ngày giờ đầy đủ

3. Constraint

Primary Key

-- Tự động đặt tên: PK_<tablename>
-- SQL Server tự tạo khi dùng PRIMARY KEY keyword

Unique Constraint

-- Quy tắc: UQ_<tablename>_<columns>
CONSTRAINT UQ_users_Email UNIQUE (Email)
CONSTRAINT UQ_event_registrations UNIQUE (EventId, UserId)
CONSTRAINT UQ_account_links_Token_Code UNIQUE (LinkToken, PersonalCode)

Foreign Key

-- Quy tắc: FK_<child_table>_<reference_entity>
CONSTRAINT FK_users_Faculty FOREIGN KEY (FacultyId) REFERENCES t_huca_faculties(Id)
CONSTRAINT FK_posts_Author FOREIGN KEY (AuthorId) REFERENCES t_huca_users(Id)
CONSTRAINT FK_committee_members_Committee FOREIGN KEY (CommitteeId) REFERENCES t_huca_liaison_committees(Id)

4. Index

-- Quy tắc: IX_<tablename>_<columns>
CREATE INDEX IX_users_FacultyId ON t_huca_users(FacultyId);
CREATE INDEX IX_posts_CreatedAt ON t_huca_posts(CreatedAt DESC);
CREATE INDEX IX_notifications_UserId ON t_huca_notifications(UserId, IsRead, CreatedAt DESC);

-- Filtered index: thêm mô tả vào tên
CREATE INDEX IX_articles_IsFeatured ON t_huca_articles(IsFeatured) WHERE IsFeatured = 1;

5. Kiểu dữ liệu chuẩn

Mục đíchKiểuGhi chú
Khóa chính (bảng lớn)BIGINT IDENTITY(1,1)users, events, posts, messages…
Khóa chính (danh mục)INT IDENTITY(1,1)faculties, provinces, roles…
Chuỗi Tiếng ViệtNVARCHAR(n)Bắt buộc dùng N-prefixed
Chuỗi không dấuVARCHAR(n)Code, URL, Email, IP…
Số tiềnDECIMAL(18,2)2 chữ số thập phân
Tọa độ GPSDECIMAL(10,7)7 chữ số thập phân
Phần trăm/ĐiểmDECIMAL(5,2)VD: 95.50%
Ngày giờDATETIME2Ưu tiên hơn DATETIME
NgàyDATEKhông lưu giờ phút
BooleanBIT1/0, DEFAULT rõ ràng
JSON dataNVARCHAR(MAX)OptionsJson, AnswersJson…
HTML contentNVARCHAR(MAX)Content bài viết, email…

6. Giá trị Enum (Status Fields)

Dùng chuỗi mô tả rõ ràng thay vì số nguyên:
-- ✅ Đúng — dễ debug, dễ đọc log
Status  NVARCHAR(20) DEFAULT 'Draft'
-- Giá trị: 'Draft', 'Published', 'Active', 'Completed', 'Cancelled'

-- ❌ Sai — khó hiểu
Status  INT DEFAULT 0
-- Giá trị: 0, 1, 2, 3, 4

Quy ước giá trị Status theo bảng

BảngTrường StatusGiá trị
t_huca_usersIsApprovedBIT (0/1)
t_huca_eventsStatusDraft, Published, Ongoing, Completed, Cancelled
t_huca_jobsStatusDraft, Active, Closed, Filled
t_huca_fundraising_campaignsStatusDraft, Active, Completed, Closed
t_huca_paymentsStatusPending, Completed, Failed, Refunded
t_huca_training_programsStatusDraft, Open, InProgress, Completed, Cancelled
t_huca_mentorship_matchesStatusPending, Active, Completed, Cancelled
t_huca_event_attendancesStatusValid, Invalid, Suspicious
t_huca_admin_requestsStatusReceived, Processing, Completed, Rejected

7. Trường polymorphic

Khi 1 bảng quan hệ với nhiều bảng khác nhau, dùng cặp <Type> + <Id>:
-- t_huca_reactions
TargetType  VARCHAR(20)   -- 'Post' hoặc 'Comment'
TargetId    BIGINT        -- Id của Post hoặc Comment

-- t_huca_notifications
ReferenceType  VARCHAR(30)  -- 'Event', 'Post', 'Donation'...
ReferenceId    BIGINT

-- t_huca_payments
ReferenceType  VARCHAR(30)  -- 'EventRegistration', 'Donation', 'ProgramRegistration'
ReferenceId    BIGINT

-- t_huca_media
OwnerType   VARCHAR(30)  -- 'Post', 'Article', 'Event', 'Company', 'Message'
OwnerId     BIGINT
:::note Không có FK trực tiếp Các trường polymorphic không có FOREIGN KEY constraint (vì không thể FK đến nhiều bảng). Tính toàn vẹn được đảm bảo ở tầng application logic. :::