npx skills add https://github.com/danhvb/my-ba-skills --skill 'Data Modeling'创建清晰的数据模型,以记录系统设计和开发所需的数据结构、关系和业务规则。
实体:我们存储数据的对象/事物(例如,Customer、Order、Product) 属性:实体的属性(例如,customer_name、order_date) 关系:实体之间如何相互关联
一对一 示例:User ↔ UserProfile
一对多 示例:Customer → Orders
多对多 示例:Products ↔ Categories
||──────|| : 有且仅有一个
||──────< : 一对多
>──────< : 多对多
o|──────< : 零个或一个对多个
||──────o< : 一个对零个或多个
erDiagram
CUSTOMER ||--o{ ORDER : places
CUSTOMER {
uuid customer_id PK
string email UK
string first_name
string last_name
string phone
datetime created_at
}
ORDER ||--|{ ORDER_ITEM : contains
ORDER {
uuid order_id PK
uuid customer_id FK
string order_number UK
decimal subtotal
decimal tax
decimal shipping
decimal total
string status
datetime order_date
}
ORDER_ITEM {
uuid item_id PK
uuid order_id FK
uuid product_id FK
int quantity
decimal unit_price
decimal total_price
}
PRODUCT ||--o{ ORDER_ITEM : "included in"
PRODUCT {
uuid product_id PK
string sku UK
string name
text description
decimal price
int stock_quantity
string status
}
PRODUCT }|--|| CATEGORY : "belongs to"
CATEGORY {
uuid category_id PK
string name
uuid parent_id FK
}
erDiagram
ACCOUNT ||--o{ CONTACT : has
ACCOUNT ||--o{ OPPORTUNITY : has
ACCOUNT {
uuid account_id PK
string name
string industry
string website
int employee_count
decimal annual_revenue
uuid owner_id FK
}
CONTACT {
uuid contact_id PK
uuid account_id FK
string first_name
string last_name
string email UK
string phone
string title
}
OPPORTUNITY }o--|| CONTACT : "primary contact"
OPPORTUNITY {
uuid opportunity_id PK
uuid account_id FK
uuid contact_id FK
string name
string stage
decimal amount
date close_date
int probability
uuid owner_id FK
}
LEAD {
uuid lead_id PK
string first_name
string last_name
string email
string company
string source
int score
string status
}
| 属性 | 数据类型 | 大小 | 必填 | 默认值 | 描述 | 验证规则 |
|---|---|---|---|---|---|---|
| customer_id | UUID | - | 是 | 自动生成 | 唯一标识符 | UUID 格式 |
| VARCHAR | 255 | 是 | - | 客户邮箱 | 有效邮箱地址 | |
| status | ENUM | - | 是 | 'active' | 账户状态 | active, inactive, suspended |
| 属性 | 类型 | 大小 | 必填 | 默认值 | 描述 | 规则 |
|---|---|---|---|---|---|---|
| order_id | UUID | - | 是 | 自动 | 主键 | 唯一 |
| order_number | VARCHAR | 20 | 是 | 生成 | 显示编号 | 格式:ORD-YYYYMMDD-XXXX |
| customer_id | UUID | - | 是 | - | 外键指向客户 | 必须存在 |
| order_date | DATETIME | - | 是 | NOW() | 下单时间 | 不能是未来时间 |
❌ 不良示例:customer_phones = "123-456, 789-012" ✅ 良好示例:使用 customer_id 外键的独立电话表
❌ 不良示例:Order 表包含 customer_email(依赖于 customer_id,而非 order) ✅ 良好示例:通过 Customer 表连接获取 customer_email
✅ 应该做:
❌ 不应该做:
数据建模之后:
每周安装次数
–
代码仓库
首次出现
–
安全审计
Create clear data models that document data structures, relationships, and business rules for system design and development.
Entities : Objects/things we store data about (e.g., Customer, Order, Product) Attributes : Properties of entities (e.g., customer_name, order_date) Relationships : How entities relate to each other
One-to-One (1:1) Example: User ↔ UserProfile
One-to-Many (1:N) Example: Customer → Orders
Many-to-Many (M:N) Example: Products ↔ Categories
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| status | ENUM | - | 是 | 'pending' | 订单状态 | pending, processing, shipped, delivered, cancelled |
| subtotal | DECIMAL | 10,2 | 是 | 0.00 | 商品项总和 | >= 0 |
| tax | DECIMAL | 10,2 | 是 | 0.00 | 计算税额 | >= 0 |
| shipping_cost | DECIMAL | 10,2 | 是 | 0.00 | 运费 | >= 0 |
| total | DECIMAL | 10,2 | 是 | - | 最终总额 | = subtotal + tax + shipping |
| shipping_address | JSON | - | 是 | - | 配送地址 | 有效地址 |
| billing_address | JSON | - | 是 | - | 账单地址 | 有效地址 |
| notes | TEXT | - | 否 | NULL | 订单备注 | 最多 2000 字符 |
| created_at | DATETIME | - | 是 | NOW() | 记录创建时间 | 不可变 |
| updated_at | DATETIME | - | 是 | NOW() | 最后修改时间 | 自动更新 |
||──────|| : One and only one (1:1)
||──────< : One-to-Many (1:N)
>──────< : Many-to-Many (M:N)
o|──────< : Zero or one to Many
||──────o< : One to Zero or Many
erDiagram
CUSTOMER ||--o{ ORDER : places
CUSTOMER {
uuid customer_id PK
string email UK
string first_name
string last_name
string phone
datetime created_at
}
ORDER ||--|{ ORDER_ITEM : contains
ORDER {
uuid order_id PK
uuid customer_id FK
string order_number UK
decimal subtotal
decimal tax
decimal shipping
decimal total
string status
datetime order_date
}
ORDER_ITEM {
uuid item_id PK
uuid order_id FK
uuid product_id FK
int quantity
decimal unit_price
decimal total_price
}
PRODUCT ||--o{ ORDER_ITEM : "included in"
PRODUCT {
uuid product_id PK
string sku UK
string name
text description
decimal price
int stock_quantity
string status
}
PRODUCT }|--|| CATEGORY : "belongs to"
CATEGORY {
uuid category_id PK
string name
uuid parent_id FK
}
erDiagram
ACCOUNT ||--o{ CONTACT : has
ACCOUNT ||--o{ OPPORTUNITY : has
ACCOUNT {
uuid account_id PK
string name
string industry
string website
int employee_count
decimal annual_revenue
uuid owner_id FK
}
CONTACT {
uuid contact_id PK
uuid account_id FK
string first_name
string last_name
string email UK
string phone
string title
}
OPPORTUNITY }o--|| CONTACT : "primary contact"
OPPORTUNITY {
uuid opportunity_id PK
uuid account_id FK
uuid contact_id FK
string name
string stage
decimal amount
date close_date
int probability
uuid owner_id FK
}
LEAD {
uuid lead_id PK
string first_name
string last_name
string email
string company
string source
int score
string status
}
| Attribute | Data Type | Size | Required | Default | Description | Validation |
|---|---|---|---|---|---|---|
| customer_id | UUID | - | Yes | Auto-gen | Unique identifier | UUID format |
| VARCHAR | 255 | Yes | - | Customer email | Valid email | |
| status | ENUM | - | Yes | 'active' | Account status | active, inactive, suspended |
| Attribute | Type | Size | Required | Default | Description | Rules |
|---|---|---|---|---|---|---|
| order_id | UUID | - | Yes | Auto | Primary key | Unique |
| order_number | VARCHAR | 20 | Yes | Generated | Display number | Format: ORD-YYYYMMDD-XXXX |
| customer_id | UUID | - | Yes | - | FK to Customer | Must exist |
| order_date | DATETIME | - | Yes | NOW() | When order placed | Cannot be future |
| status | ENUM | - | Yes | 'pending' | Order status | pending, processing, shipped, delivered, cancelled |
| subtotal | DECIMAL | 10,2 | Yes | 0.00 | Sum of items | >= 0 |
| tax | DECIMAL | 10,2 | Yes | 0.00 | Calculated tax | >= 0 |
| shipping_cost | DECIMAL | 10,2 | Yes | 0.00 | Shipping fee | >= 0 |
| total | DECIMAL | 10,2 | Yes | - | Final total | = subtotal + tax + shipping |
| shipping_address | JSON | - | Yes | - | Delivery address | Valid address |
| billing_address | JSON | - | Yes | - | Billing address | Valid address |
| notes | TEXT | - | No | NULL | Order notes | Max 2000 chars |
| created_at | DATETIME | - | Yes | NOW() | Record created | Immutable |
| updated_at | DATETIME | - | Yes | NOW() | Last modified | Auto-update |
❌ Bad: customer_phones = "123-456, 789-012" ✅ Good: Separate phone table with customer_id FK
❌ Bad: Order has customer_email (depends on customer_id, not order) ✅ Good: Get customer_email via Customer table join
✅ Do :
❌ Don't :
After data modeling:
Weekly Installs
–
Repository
First Seen
–
Security Audits
智能OCR文字识别工具 - 支持100+语言,高精度提取图片/PDF/手写文本
933 周安装