Cursor / Claude Code / Windsurf rules 模板
三大主流 AI 编辑器的 MariaDB 专用 rules 文件,直接 copy 到项目
把这一页的内容 copy 到你的项目,可以显著降低 AI 在写 MariaDB 相关代码时的错误率。
通用规则(共享底层)
这套规则不绑定具体工具,是底层共识:
# MariaDB project rules
## Schema conventions
- Charset: `utf8mb4`, collation `utf8mb4_unicode_ci` (or `utf8mb4_uca1400_ai_ci` on 11.5+)
- Primary keys: `BIGINT AUTO_INCREMENT PRIMARY KEY`. No composite PK unless join-table.
- Foreign keys: explicit `CONSTRAINT fk_<table>_<col>` named
- Timestamps: every table has `created_at`, `updated_at DATETIME DEFAULT CURRENT_TIMESTAMP`
- Money: `*_cents BIGINT NOT NULL`, never float/decimal in app code
- Booleans: `is_*` BOOLEAN (not TINYINT(1))
- Status: ENUM with explicit values + COMMENT explaining the state machine
- Soft delete: `deleted_at DATETIME NULL` (don't use `is_deleted`)
- Every table has a `COMMENT` describing its purpose
- Every column with non-obvious meaning has a `COMMENT`
## Engine and storage
- Default engine: InnoDB. Never MyISAM.
- Use `BIGINT` for IDs even if you think `INT` is enough
- Vector columns: only on MariaDB 11.8+, declare distance in index
## Query conventions
- Never `SELECT *` outside ad-hoc shell
- Every `UPDATE`/`DELETE` must have a `WHERE`. Bare-table mutations need
`-- intentional: full table` comment
- `LIMIT` is required when result set could exceed 1000
- Prefer `EXISTS` over `IN (subquery)` when subquery could be large
- Use named indexes: `INDEX idx_<table>_<cols>`
- For JSON, use `JSON` type, not `TEXT`; access with `->>` operator
## Migration rules
- Never `DROP COLUMN` in a single deploy; add `ALTER TABLE ... DROP` 2 deploys later
- Schema changes go through Liquibase / Flyway / Atlas, never raw ALTER in prod
- Always wrap multi-statement migrations in a transaction where MariaDB supports it
(DDL is not transactional; use staged rollback plans)
## Performance
- Run `EXPLAIN` before merging any new query
- Reject queries with `type=ALL` and `rows>10000`
- Slow log threshold is 500ms, anything above needs an index or rewrite
## Security
- App connects with least-privilege account (no `SUPER`, no `ALL PRIVILEGES`)
- Never log raw SQL with parameters in production
- Always use parameterized queries; never string-concatenate SQL
- TLS required for any non-localhost connection
## Differences from MySQL 8.0 (LLM tends to confuse these)
- `JSON_TABLE` syntax differs — refer to MariaDB KB before generating
- `caching_sha2_password` auth plugin does NOT exist in MariaDB; use `mysql_native_password`
- Default `utf8mb4` collation differs (MariaDB 11.5+: `utf8mb4_uca1400_ai_ci`)
- Window functions and CTE both work, but optimizer behavior differs
- `RETURNING` clause works since MariaDB 10.5 (`INSERT … RETURNING`)Cursor
Cursor 用 .cursor/rules 文件夹下的 .mdc 文件,v0.40+ 起支持 apply: always、globs 等元数据。
.cursor/rules/mariadb.mdc:
---
description: MariaDB conventions and pitfalls
globs:
- "**/*.sql"
- "**/migrations/**"
- "**/schema/**"
- "**/*.ts"
- "**/*.py"
apply: always
---
# MariaDB rules
(把上面"通用规则"全部粘贴到这里)
## Cursor-specific
- When asked to "write a query", first check if `lib/db/schema.ts` (or `*.sql`) exists
- Use the project's existing ORM if any (Prisma / Drizzle / SQLAlchemy)
- For new migrations, look at the last 3 migrations in the folder for style
- When adding a column, also update the relevant TypeScript / Python type把 MCP Server 接入后,再加:
## Database access
- The `mariadb` MCP server is available with these tools:
- `list_tables()` — see all tables
- `describe_table(name)` — schema for one table
- `run_query(sql, limit)` — read-only, 500-row cap
- Always use `describe_table` before writing a query against unfamiliar tables
- Never paste a query result with >50 rows back into chatClaude Code
Claude Code 用 CLAUDE.md(项目根目录)。
# CLAUDE.md
(把上面"通用规则"全部粘贴)
## Workflow
- Before writing any SQL, list the relevant tables with `list_tables` MCP tool
- For any UPDATE/DELETE, show me an EXPLAIN first
- For new migrations, place them under `db/migrations/YYYY-MM-DD-name.sql`
- After running migrations, update `db/schema.ts` to match
## Approved scripts
- `pnpm db:migrate` — apply pending migrations
- `pnpm db:reset` — drop and recreate dev DB
- `pnpm db:dump` — `mariadb-dump --single-transaction` of current dev DBClaude Code 会自动把 CLAUDE.md 加进系统提示。如果你有多个 sub-project,每个 dir 放一个 CLAUDE.md。
Windsurf
Windsurf 用 .windsurf/rules.md 或全局 settings。结构与 CLAUDE.md 几乎一致:
# Windsurf rules — MariaDB project
(通用规则)
## Cascade-specific
- Cascade can execute `pnpm db:*` scripts automatically — please ask before destructive ones
- Always commit migrations and `db/schema.ts` in the same commit
- When running tests, use the `mariadb-test` MCP server (separate test DB)VS Code Copilot Chat
.github/copilot-instructions.md:
(通用规则)
## Project-specific
- ORM: Drizzle
- Migrations: `drizzle-kit generate`
- Tests use `mariadb:11.4` container per test suite几个共通建议
- 不要让一个 rules 文件过 200 行 —— LLM 在长 prompt 下会"漏看"规则;超长就拆分
- 越严格越好 —— "should" 不如 "must",模型对 strict 词更敏感
- 写"反例"比写"正例"更有效 —— 例:
Never use SELECT * — use explicit columns - 定期检视 —— 看 git log 里 AI 写的代码是不是真的遵守了规则;不遵守的规则要么删要么改写
- 配合 MCP —— rules 告诉模型怎么写、MCP 让模型能查 schema,两者搭配效果最大
参考
- MCP Server 实战
- Text-to-SQL 方法论
- 事故合集 —— 看完事故再来写 rules
- Cursor 官方 rules 文档