# SQL editor

> Write your scripts in .sql files, run them with the transaction mode you choose and get help from the AI.

Source: https://www.opendb.tools/en/docs/sql-editor

![The SQL editor: files, script and result](/images/docs/en/sql-editor.webp)

## Files

Each tab of the editor is a **`.sql` file** saved in your workspace:

```text
{project folder}/.opendb/users/{user}/sql/
```

- The file is saved while you type.
- You can create folders, rename, move and delete.
- The content of your files never goes through OpenDB's database.

If a file is changed outside the editor, OpenDB detects it and lets you choose between **Reload the file** and **Keep my version**.

The left panel has four views: **Files**, **Tables**, **History** and **Snippets**.

## Write

- SQL highlighting and **autocompletion** of tables and columns.
- **Format** (`Shift` + `Alt` + `F`) tidies up the script, with keywords in upper case.

### Query snippets

A snippet is inserted by typing its name. Each `:field` of the snippet becomes a placeholder that you step through with `Tab`.

OpenDB ships snippets, and you can create your own. A built-in snippet can be hidden, or copied to be changed.

## Run

The **Run** button (`Ctrl` + `Enter`) runs:

- the **selection**, if some text is selected;
- otherwise **the whole script**.

`Ctrl` + `Shift` + `Enter` runs only the **statement under the cursor**.

Statements run one after the other, until the first error. A read returns its first 1,000 rows. A write returns the number of rows changed.

### Transaction modes

| Mode | Behaviour |
|---|---|
| Auto-commit | Each statement is committed as soon as it succeeds |
| All or nothing | If one statement fails, everything is rolled back |
| Dry run | Everything runs so you can see the result, then is rolled back |

If the database cannot roll back what you run (a structure statement, or a MyISAM table on MySQL), the last two modes are refused with an explanation.

### Parameters

A query can contain `:name` parameters. A dialog asks for their values before the query runs.

```sql
SELECT * FROM orders WHERE status = :status AND total > :minimum;
```

### Confirmations

OpenDB asks for confirmation before running:

- an `UPDATE` or a `DELETE` without a `WHERE` clause;
- a `DROP` or a `TRUNCATE`.

### Stop a query

A long-running query can be stopped on MySQL, MariaDB and PostgreSQL. This is not possible on SQLite.

### Read-only

The **Read-only** switch, at the top right, limits the project to reads. Turn it on for a production database.

## Results

- **Table** or **Chart**.
- Scrolling beyond the first 1,000 rows. Without an `ORDER BY`, OpenDB warns you that the order of the pages is not guaranteed.
- **Copy**, including as a Markdown table.
- **Export** every row of the result.
- **Analyze in the chat** opens a conversation with the query and its first rows.

The result is **editable** when the query reads a single table as it is, primary key included: no join, grouping, `DISTINCT` or `UNION`. When in doubt, OpenDB leaves the result read-only.

## History

Every run goes into your personal history. You can search it for a query, insert it back into the tab, copy it, or clear the history.

## AI help

### Write with AI

The **Write with AI** button opens a popover where you describe the query you need, on several lines if you like: `Enter` sends it, `Shift` + `Enter` starts a new line. Your request stays there if you close the popover.

```text
the 10 customers who ordered the most
```

The AI checks the tables and columns, then adds the query at the end of your script, with an explanation, and the popover closes. The query is **selected, never run**: you read it before running it.

### Review with AI

| Mode | What the AI does |
|---|---|
| Fix | Reads the error returned by the database, finds the cause and writes the corrected version |
| Explain | Explains the query step by step, and warns if it changes data |
| Optimize | Reads the execution plan, rewrites the query if useful and suggests indexes |

Suggested indexes are never created without you.

## Execution plan

The **Plan** button shows the plan of the query without running it. See [Execution plans](/en/docs/execution-plans).
