database

A set of database design rules written in Chinese. It covers relationships between tables, application-level checks, transactions, indexes, and other database practices.

In plain words
What is it for?
Use it as a reference when designing tables, validating relationships in application code, handling related changes in transactions, and adding indexes or uniqueness rules.
Why use it?
It documents a particular approach to keeping related data consistent without relying on database-level foreign-key constraints. The rules give developers a shared standard for database changes.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/mywand/cusrsor-do-it/database
Clone the repo
git clone --depth 1 https://github.com/mywand/cusrsor-do-it

Made for: Cursor.

Per session 10,817 This file is loaded in full into every session.
When invoked 10,817 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.10817 $0.10817
Opus 5 $0.05409 $0.05409
Sonnet 5 $0.02163 $0.02163
Haiku 4.5 $0.01082 $0.01082

Measured yesterday against content hash d178615dff6f, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

database scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/tools/database.mdc · 1,380 lines

How it starts

The opening of the file, as written. The whole thing — 1,380 lines — stays where its author put it; the contents beside it link to each section on GitHub.

最近更新: 2025-12-28

数据库设计规范

涵盖数据库设计、建模、索引、约束、事务等核心规范

1. 外键约束原则

1.1 核心规则

  • 禁止物理外键:在数据库层面不要添加物理外键关联(FOREIGN KEY 约束)
  • 业务逻辑层处理:表之间的关联关系仅在业务逻辑层(应用层)进行维护和校验

1.2 原因说明

维度 说明
灵活性 避免级联删除/更新带来的意外影响,业务逻辑更可控
扩展性 便于分库分表和水平扩展,不受外键约束限制
性能 减少数据库层面的锁竞争,提高并发性能
维护性 支持更灵活的关联关系变更,无需修改数据库结构

1.3 实践示例

-- ❌ 不推荐:使用物理外键
CREATE TABLE orders (
    id BIGINT PRIMARY KEY,
    user_id BIGINT NOT NULL,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- ✅ 推荐:不使用物理外键,在业务逻辑层校验
CREATE TABLE orders (
    id BIGINT PRIMARY KEY,
    user_id BIGINT NOT NULL,
    INDEX idx_user_id (user_id)  -- 仅添加索引以优化查询性能
    -- 不在数据库层面添加 FOREIGN KEY 约束
);

2. 关联关系维护

2.1 维护原则

  • 应用层校验:在应用层通过代码逻辑保证数据一致性
  • 事务保证:使用事务确保关联操作的原子性
  • 约束替代:通过唯一索引保证业务层面的唯一性约束

2.2 Java 示例

// ✅ 在业务逻辑层进行关联校验
public class OrderService {
    
    @Transactional
    public Order createOrder(CreateOrderRequest request) {
        // 1. 在业务逻辑层验证用户是否存在
        User user = userRepository.findById(request.getUserId())
            .orElseThrow(() -> new BusinessException("用户不存在"));
        
        // 2. 验证商品库存
        for (OrderItem item : request.getItems()) {
            Product product = productRepository.findById(item.getProductId())
                .orElseThrow(() -> new BusinessException("商品不存在"));
            
            if (product.getStock() < item.getQuantity()) {
                throw new BusinessException("商品库存不足");
            }
        }
        
        // 3. 创建订单(在事务中保证原子性)
        Order order = new Order();
        order.setUserId(request.getUserId());
        order.setStatus(OrderStatus.PENDING);
        order = orderRepository.save(order);
        
        // 4. 创建订单项并扣减库存
        for (OrderItem item : request.getItems()) {
            item.setOrderId(order.getId());
            orderItemRepository.save(item);
            productRepository.decreaseStock(item.getProductId(), item.getQuantity());
        }
        
        return order;
    }
}

Read the full file on GitHub · 1,380 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 1,380 lines · 10,817 tokens per session scan A d178615dff6f

Subscribe to this mod's changes

database is a cursor rule published in the GitHub repository mywand/cusrsor-do-it (2 stars, last pushed 7mo ago), licensed Apache-2.0. It adds 10,817 tokens to every session, about $0.0541 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.