Mysql Query Assistant
Use this skill to turn a user's request into safe MySQL work against a live database.
Core workflow
For every request, follow this sequence:
- Inspect connection prerequisites from
references/connection-and-safety.md.
- Discover relevant schema first. Prefer column comments when available.
- Draft the SQL.
- Execute only read-only SQL with
scripts/run_read_query.py.
- Perform double validation:
- structural validation: tables, columns, joins, filters, grouping, and syntax match the request.
- result validation: returned rows and aggregates look semantically consistent with the user's intent.
- If validation fails, revise the SQL and run it again.
- Present the final answer using the output template below.
Default behavior
- Prefer
SELECT queries only.
- Never auto-execute
INSERT, UPDATE, DELETE, REPLACE, ALTER, DROP, TRUNCATE, CREATE, GRANT, or REVOKE.
- Keep result samples small by default.
- When the request is ambiguous, use schema inspection to narrow candidate tables before writing SQL.
- Prefer explicit column lists over
SELECT * unless schema exploration is the user's goal.
- Prefer bounded queries. Add
LIMIT when the user did not ask for a full extract.
Schema discovery workflow
Before generating SQL, inspect schema with scripts/introspect_schema.py.
Use this order:
- List candidate tables.
- Inspect columns, data types, keys, and column comments for the most relevant tables.
- Infer business meaning from comments and names.
- Only then draft SQL.
If comments are missing, fall back to table names, column names, keys, and a few small probing queries.
Read-only execution workflow
Use scripts/run_read_query.py to execute the SQL.
The script rejects non-read-only statements. It also blocks multi-statement execution.
When verifying a query:
- Run the first candidate SQL.
- Review row count, sample rows, and whether the columns answer the request.
- If the result is empty or suspicious, explain why and try a corrected query when appropriate.
- If multiple interpretations are plausible, prefer the query best supported by schema and results, and say what assumption you made.
Restricted write workflow
When the user asks for a write operation:
- Do not execute the write statement.
- First produce a preview
SELECT that shows exactly which rows would be affected.
- Then produce the write SQL separately.
- Clearly label the write SQL as not executed.
- Call out any missing safety condition such as a weak or absent
WHERE clause.
Output template
Use this structure unless the user asks for a different format.
Final SQL
[final sql]
Validation
- Structural check: [why the sql shape matches the request]
- Result check: [why the returned data seems correct, or why confidence is limited]
Sample results
Show 5 to 20 rows when available and useful. Keep wide tables compact.
Result summary
Provide a brief natural-language summary of what the query shows.
Notes
Include assumptions, caveats, and any schema uncertainties.
Execution details
- Use environment variables described in
references/connection-and-safety.md.
- Use
scripts/introspect_schema.py for schema discovery.
- Use
scripts/run_read_query.py for executing read-only SQL.
- If the python mysql driver is missing, install one of the documented options before running the scripts.
Examples
Example: analytics request
User request: 统计最近 7 天每天新增用户数
Expected approach:
- Inspect likely user table and created-at column.
- Confirm time column semantics from comments or names.
- Generate grouped date query.
- Run it.
- Verify the date buckets and counts look plausible.
Example: restricted write request
User request: 把 status = 'pending' 且 30 天前创建的订单改成 expired
Expected approach:
- Generate preview
SELECT for the target rows.
- Generate
UPDATE SQL separately.
- Do not execute the
UPDATE.
- Warn if the table lacks a reliable key or if the filter looks too broad.