跳转到内容

Write SQL with completion and inspections

此内容尚不支持你的语言。

In a console or a .sql file bound to a connection, the editor knows that connection’s schema. It completes names, marks the ones that do not exist, warns about changes that would touch every row, and keeps the statements you ran. Completion, inspections and query history: Works.

A console or a .sql file bound to a connection: Query and edit data.

Press ⌃Space (CtrlSpace). After FROM, JOIN, INTO and UPDATE, the list also opens by itself when you type the space.

Where the caret is What is offered
After FROM, JOIN, INTO, UPDATE, TABLE Tables and views: the current schema first, then the others. System schemas only when you type their name.
After alias. The columns of that table only.
After JOIN Ready join clauses built from real foreign keys, with aliases, for example item i on m.sku = i.sku.
Elsewhere in a statement The columns of the statement’s tables, then keywords.
Inside a string or a comment Nothing.

Each entry names its kind and schema at the right, for example table · public. Names that need quotes are inserted with them. Before completing, the editor checks whether the schema changed (at most every 2 seconds), so a table created in another program shows up without a refresh.

Functions, parameters and the columns of a WITH query are not completed yet.

The editor checks each statement against the connection’s schema and underlines what it finds. The same problems are listed in the Problems tool window under “SQL”.

Problem Severity Example message
Unknown table error unknown table custmer
Unknown column error unknown column emial in customer
Change without WHERE warning DELETE without WHERE removes every row of stock; add a WHERE clause (or WHERE true …)
Foreign key MySQL ignores warning MySQL ignores REFERENCES in the definition of column customer_id; …
  • On an unknown name, ⌥↩ (AltEnter) offers Replace with name for up to three close matches.
  • Tables a script creates are known to its later statements, and the columns of a WITH query are checked against the query.
  • The warning about a change without WHERE also appears in orange after the statement. Write WHERE true when you mean every row.

Type checks and ambiguous column names are not inspected yet.

  1. Click History in the console bar, or press ⌥⌘E (CtrlAltE).
  2. Click a statement to insert it at the caret. Esc or a click outside closes the list.

The history is per connection and project, newest first, at most 500 entries; a repeated statement appears once, failed ones in the error colour. It lives in your settings folder under sql-history/ (folder mode 700, files mode 600), not in the project. Statements that contain a password are not kept.

Rename a table or column across the project

Section titled “Rename a table or column across the project”

Select the object in the Database tool window and press ⇧F6 (Rename…). The dialog shows the statement for the database and the places in the project’s .sql files that name the object. See Explore and change a schema.

Keyboard of
ActionShortcut
BrowseConsole.History.Browse⌥⌘ECtrlAltE
Code › Code Completion⌃Space or ⌥⎋CtrlSpace
Code › Show Context Actions⌥↩AltEnter

The same completion, inspections and history are tools. They need the permission read-schema on the connection (sql_history needs read-data, because past statements contain values).

  • complete_sqlreadssshdatabase-permissiontrusted-projectComplete SQL at a cursor position using the connection's schema: tables after FROM/JOIN, after JOIN also ready join clauses (kind join) built from foreign keys to the tables already in the statement, columns of referenced tables or aliases, keywords (needs the read-schema permission).
  • inspect_sqlreadssshdatabase-permissiontrusted-projectInspect SQL against the connection's schema: unknown tables and columns, UPDATE/DELETE without WHERE, MySQL column-level REFERENCES that create no foreign key (needs the read-schema permission).
  • sql_historyreadssshdatabase-permissiontrusted-projectThe query history of a connection: the statements its consoles in the window ran, newest first, with time (seconds since 1970) and whether they succeeded.
  • sql_refactorings_atreadssshdatabase-permissiontrusted-projectWhat the SQL editor's refactorings find at a caret: object is the table or column named there ({table, column}) as ⇧F6 would rename it, or object_error with the reason (unknown, an alias, a column of several tables); expand_wildcard is the edit and the resulting SQL of "Expand column list" when the caret is on a * of a select list, else null; inline is {name, edits, sql} of Inline on the WITH query named at the caret, else inline_error says why (recursive, column list, MATERIALIZED, written to, unused, no WITH query) or both are null on no name.