postgresql-core-schema
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePostgreSQL Core & Schema
PostgreSQL 核心与Schema设计
When to use this skill
何时使用该技能
- Creating or modifying PostgreSQL tables (DDL).
- Working with JSONB, Arrays, or specialized Postgres types.
- Creating triggers or functions (PL/pgSQL).
- 创建或修改PostgreSQL表(DDL操作)。
- 处理JSONB、数组或PostgreSQL专属数据类型。
- 创建触发器或函数(PL/pgSQL)。
1. Data Types
1. 数据类型
- Timestamps: Always use (Timestamp with Time Zone), rarely
timestamptz(without TZ).timestamp - Text: Use instead of
textunless a strict limit is architecturally required.varchar(n) - JSON: Use (binary) for storage and indexing, not
jsonb.json - Primary Keys: or
bigint GENERATED ALWAYS AS IDENTITY(v4/v7).uuid
- 时间戳:始终使用(带时区的时间戳),尽量避免使用
timestamptz(无时区)。timestamp - 文本类型:使用而非
text,除非架构上明确要求严格的长度限制。varchar(n) - JSON类型:存储和索引时使用(二进制格式),而非
jsonb。json - 主键:使用或
bigint GENERATED ALWAYS AS IDENTITY(v4/v7版本)。uuid
2. Constraints & Integrity
2. 约束与完整性
- Check Constraints: Use constraints generously (e.g.,
CHECK).CHECK (price > 0) - Foreign Keys: Index all FK columns manually (Postgres does not auto-index them).
- Exclusion Constraints: Use where is not enough (e.g., non-overlapping time ranges).
UNIQUE
- 检查约束:大量使用约束(例如:
CHECK)。CHECK (price > 0) - 外键:手动为所有外键列创建索引(PostgreSQL不会自动为其创建索引)。
- 排他约束:当约束不足以满足需求时使用(例如:非重叠时间范围)。
UNIQUE
3. Advanced Features
3. 高级特性
- Triggers: Use for audit logs or complex data consistency that cannot be enforced by constraints.
- Partitions: Consider declarative partitioning for massive time-series tables.
- Enumerations: Use Native Enums for strict, infrequently changing sets; otherwise use a reference table.
- 触发器:用于审计日志或无法通过约束实现的复杂数据一致性保障。
- 分区表:对于大规模时间序列表,考虑使用声明式分区。
- 枚举类型:对于严格且不常变更的集合,使用原生枚举类型;否则使用参考表。