Cockroachdb Sql

作者 cockroachdb6c96c6394a61無授權條款4 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫2 個月前更新

Use when writing, generating, or optimizing SQL for CockroachDB, designing CockroachDB schemas, or when the user asks about CockroachDB-specific SQL patterns, type mappings, and distributed database best practices. Also use when encountering CockroachDB anti-patterns like missing primary keys, sequential ID hotspots, or incorrect type usage.

AI 產生的概覽

為 CockroachDB 產生並最佳化符合規範的 SQL,涵蓋結構定義、查詢與資料操作。

功能
此技能會把自然語言需求轉換成符合 CockroachDB 最佳實務的 SQL,涵蓋結構定義、DML、查詢模式、最佳化與維運工作。它會套用隨附的規則參考,並在有資料庫連線時以 EXPLAIN 驗證產生的語句。輸出包含加上註解的 SQL、所用的 CockroachDB 特性、效能考量,以及引用的規則。
適用情境
適用於為 CockroachDB 撰寫、產生或最佳化 SQL、設計 CockroachDB 結構,或詢問 CockroachDB 特有的 SQL 模式、型別對應與分散式資料庫實務時。也適用於檢查缺少主鍵、循序 ID 熱點或型別使用不當等反模式。
執行需求
僅為說明文件,不含指令碼。不需資料庫連線即可產生 SQL 與連線指引。使用連線時需要對目標資料庫與資料表具備相應權限(SELECT、INSERT、UPDATE、DELETE 或管理員權限),並提供連線字串、COCKROACH_URL 環境變數或 cockroach-cloud MCP 伺服器之一;查詢執行使用 cockroach CLI。

CockroachDB SQL Skill

Converts natural language questions into CockroachDB-compliant SQL queries, following CockroachDB best practices. Use it for schema design, writing queries and optimizing query.

How to Apply this Skill

  1. Connection Detection — already performed on skill invocation; reuse active connection.

  2. Parse Natural Language Intent

    • Identify the operation type (SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, etc.)
  3. Context Gathering

    • Check for existing schema context in conversation
    • If connected to DB, query existing schema:
      • SHOW TABLES; to see existing tables
      • SHOW CREATE TABLE table_name; for existing structure
    • Ask clarifying questions if needed:
      • Table structure if not provided
      • Data types for columns
      • Index requirements
      • Multi-region needs
      • Performance characteristics
  4. Apply CockroachDB Rules

    • Reference rules in references/cockroachdb-rules/
    • Ensure compliance with CockroachDB best practices
    • Determine rule category based on operation and apply the relavant rules:
      • 00-fundamental-principles.md - Always apply these first
      • 01-schema-design.md - Table creation and structure
      • 02-dml-operations.md - Data modification
      • 03-query-patterns.md - Query construction
      • 04-optimization.md - Performance, Optimization and anti-patterns
      • 05-operational.md - Admin and maintenance
    • Validate against anti-patterns in 04-optimization.md
  5. Validate against DB(MANDATORY)

    • ALWAYS run EXPLAIN on every generated SQL query when connected to DB.
    • If EXPLAIN returns a parsing/syntax error, fix the query and re-run EXPLAIN until it passes.
    • Include the EXPLAIN output in the response.

Response Behavior

Initial Response

When skill is invoked:

  1. Check for an available connection so queries run against the right cluster:

    • Check if a connection string is provided in the prompt (postgresql://...).
      • If provided, use cockroach sql --url "<provided-url>" -e "SQL" to run queries. Do not use psql.
    • Else check the COCKROACH_URL environment variable (echo $COCKROACH_URL).
      • If set, use cockroach sql --url $COCKROACH_URL -e "SQL" to run queries. Do not use psql.
    • Else check for cockroach-cloud MCP server availability.
    • If none of these are available or it is unclear which cluster to use, ask the user before running anything.
  2. Focus exclusively on CockroachDB

  3. Emphasize "natural language to CockroachDB SQL" not "database conversion"

  4. Present CockroachDB-specific syntax even when a rule derives from PostgreSQL compatibility.

Output Format

  • Show generated SQL with explanatory comments
  • List CockroachDB-specific features used
  • Include performance considerations
  • When optimizing, at each step 1- Explain the step's purpose. 2- Execute the step and report the outcome. 3- Summarize all findings and actions taken.
  • Provide references used including the rules

Examples

Schema Design — UUID Primary Key (Avoid Hotspots)

sql
CREATE TABLE orders (  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),  customer_id UUID NOT NULL,  status STRING NOT NULL DEFAULT 'pending',  total DECIMAL(10,2) NOT NULL,  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),  INDEX idx_orders_customer (customer_id),  INDEX idx_orders_status_created (status, created_at DESC));

Batch Upsert with UNNEST

sql
UPSERT INTO inventory (sku, warehouse, quantity, updated_at)SELECT * FROM UNNEST(  ARRAY['SKU-001', 'SKU-002', 'SKU-003']::STRING[],  ARRAY['us-east', 'us-east', 'us-west']::STRING[],  ARRAY[100, 250, 75]::INT[],  ARRAY[now(), now(), now()]::TIMESTAMPTZ[]);

Keyset Pagination (Avoid OFFSET)

Use the previous page's last (created_at, id) as the cursor. In application code these are bound parameters; the literals here are placeholders.

sql
SELECT id, customer_id, created_atFROM ordersWHERE (created_at, id) > ('2025-01-01'::TIMESTAMPTZ, '00000000-0000-0000-0000-000000000000'::UUID)ORDER BY created_at, idLIMIT 50;

Supporting Documentation

  • references/cockroachdb-rules/ - CockroachDB SQL rules
  • references/EXAMPLES.md - SQL examples and patterns

來源與署名

來源:cockroachdb/claude-plugin位於skills/cockroachdb-query-and-schema-design/cockroachdb-sql提交6c96c63

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架