Skip to content

Explore and change a schema

The Database tool window shows each connection’s schema as a tree: tables with their columns, keys and indexes, views, and, where the database has them, sequences, routines, triggers and types. From there you read an object’s definition and change it, always with the statement shown before it runs. Schema tree, Show DDL and the database refactorings: Works.

Open the Database tool window from the right stripe. Each connection row says what it is, for example “SQLite · shop.db” or “PostgreSQL · db.example.com:5432/shop”; once the schema is read, a server connection ends in “· TLS” or “· TLS: no”.

Under the current schema the tree has folders tables and views with their counts, then, where present, sequences, functions, procedures, aggregates, triggers and types. A kind without objects gets no folder. Each table has columns, keys and indexes:

  • a column shows its type, “primary key”, “not null”, its default and “generated” columns;
  • a foreign key shows “(columns) → table(columns)” and its rules, such as “on delete cascade”;
  • an index shows its columns, “unique”, a partial condition (“where …”), collation and method.

What is read depends on the database:

System Object kinds in the tree
PostgreSQL tables, views, sequences, functions, procedures, aggregates, triggers, types
MySQL tables, views, functions, procedures, triggers
MariaDB as MySQL, plus sequences
SQLite tables, views, triggers
SQL Server, Oracle, DuckDB tables, views, sequences

Other schemas appear as further rows with their tables and views. The ▽ (Filter Schemas) button, or the connection’s Schemas option, limits which ones. Typing in the tree starts a speed search.

  • Refresh reads the schema again and keeps open nodes and the selection. After you run DDL, CALL or EXEC in a console, the connection is read again by itself.
  • A schema that cannot be read shows “Could not read the schema: …” with a Retry row.
  • Quick documentation: select a table, view, column or connection and press F1 or ⌃J, or click ?. For a table it lists the columns with keys, NOT NULL and defaults, the tables that reference it and its indexes; for a connection its system, URL (never the password), tunnel, TLS mode and options. It uses what the tree has read and asks the database nothing.
  • Show DDL: right-click and choose Show DDL, click DDL, or press ⌥⇧⌘D (CtrlAltShiftD). A read-only tab “customer (DDL).sql” opens with the CREATE statement, read fresh from the database. SQLite shows the statements it stored; the other systems get DDL built from the schema (columns, primary key, foreign keys, indexes; a view as CREATE VIEW … AS). Routines, triggers and types show the definition the database reports.
  1. Select a table, column, view, index, sequence or named constraint and press ⇧F6, or right-click and choose Rename….
  2. Type the new name. The dialog shows the statement in the connection’s dialect, notes on risk, and the places in the project’s .sql files that name the object.
  3. Press Refactor (↩). Nothing runs before that.

On PostgreSQL, SQLite, SQL Server and DuckDB the rename and the renames it implies run in one transaction; MySQL, MariaDB and Oracle commit each statement on its own. SQLite cannot rename views, indexes or constraints; routines can be renamed on PostgreSQL and SQL Server.

⌘Z afterwards undoes the change in your files. A popup then asks whether to Keep the database as it is or Rename back in the database…. Files bound to another connection are listed but never changed.

Select a column and press ⌘F6 (CtrlF6), or choose Change Column Type…. The dialog keeps NOT NULL and the default where the database allows it, shows the statement, and warns that values may be lost. It is not offered for views and on SQLite, which cannot change a type without rebuilding the table.

  1. Select a table and press F5. The Copy Table … To… dialog opens.
  2. Choose the target schema and name (<table>_copy by default).
  3. Keep or clear what to take along: primary key, unique constraints, NOT NULL, defaults, CHECK constraints and rows are on; foreign keys are off.
  4. Read the preview (CREATE TABLE and INSERT … SELECT) and confirm. Output reports “Table copied: N row(s)”.

On PostgreSQL, SQLite and SQL Server the copy is one transaction; on MySQL, MariaDB and Oracle a failed step drops the new table again. Copies stay within one connection.

The generate_ddl tool writes the CREATE script of a schema or of chosen tables (tables, keys, indexes, foreign keys). It is the script for an empty database; to bring an existing database up to date, compare the two: Compare schemas.

No DROP refactoring, no editing of nullability or defaults in a dialog, no rewriting of view definitions after a rename, and no copying between connections. To drop or alter anything else, write the statement in a console.

Keyboard of
ActionShortcut
Refactor › Rename⇧F6ShiftF6
Show ddlFerrisIDEA.Database.ShowDdl⌥⇧⌘DCtrlAltShiftD
View › Quick DocumentationF1 or ⌃JCtrlQ

In the Database tree: F1 or ⌃J documentation, F4 edit data or properties, F5 copy table, ⇧F6 rename, ⌘F6 change column type, ⌥⇧⌘U ER diagram.

Reading the schema needs the permission read-schema on the connection. The refactorings preview first and change the database only with apply: true and the permission modify-schema (copy_table with rows also needs modify-data).

  • change_column_typechangesdry run by defaultsshdatabase-permissiontrusted-projectChange a column's type: the statement in the connection's dialect, keeping NOT NULL and the default where the dialect restates the column (MySQL, MariaDB, SQL Server); refused for views and SQLite.
  • copy_tablechangesdry run by defaultsshdatabase-permissiontrusted-projectCopy a table to another schema or name, keeping primary key, unique constraints, NOT NULL, defaults and CHECK constraints by default (foreign keys only with foreign_keys=true), and its rows.
  • describe_tablereadssshdatabase-permissiontrusted-projectDescribe a table or view: columns (type, nullable, default), primary key, foreign keys and indexes, in the current schema or one from list_schemas.
  • generate_ddlreadssshdatabase-permissiontrusted-projectGenerate the CREATE script (tables, primary keys, indexes, foreign keys) of a connection's schema or of selected tables; nothing is run (needs the read-schema permission).
  • list_schemasreadssshdatabase-permissiontrusted-projectList the non-system schemas of a connection's database, the current one marked (needs the read-schema permission).
  • list_tablesreadssshdatabase-permissiontrusted-projectList the tables and views of a connection's current schema, or of another schema from list_schemas.
  • object_definitionreadssshdatabase-permissiontrusted-projectThe definition of a function, procedure, aggregate, trigger or type as the database states it: PostgreSQL pg_get_functiondef and pg_get_triggerdef, aggregates and types (enum, domain, range, composite) written from the catalogue; MySQL and MariaDB SHOW CREATE FUNCTION/PROCEDURE/TRIGGER; SQLite the stored CREATE TRIGGER.
  • object_docreadssshdatabase-permissiontrusted-projectQuick documentation of a table, view or column, as F1 / Ctrl+J in the Database tool window shows it: the kind and place, the columns with types, keys, NOT NULL and defaults, foreign keys, the tables that reference it and indexes; for a column its type, default, keys and indexes.
  • rename_sql_objectchangesdry run by defaultsshwritedatabase-permissiontrusted-projectRename a table, view, column, index, routine, sequence or constraint: returns the statement in the connection's dialect and edits for every SQL reference in the given text or the project's .sql files.
  • schema_objectsreadssshdatabase-permissiontrusted-projectThe kinds of objects a schema of a connection can hold, each with its count, and its objects besides tables and views: sequences, functions, procedures and aggregates (PostgreSQL with their arguments, so overloads differ), triggers with their table and when they fire, and types (enum, domain, range, composite) — PostgreSQL; functions, procedures and triggers on MySQL, also sequences on MariaDB; triggers on SQLite; sequences on DuckDB, Oracle and SQL Server.
  • table_ddlreadssshdatabase-permissiontrusted-projectThe definition of one table or view as SQL, as the Database tool window's Show DDL tab has it: CREATE TABLE with columns, types, NOT NULL, defaults, primary key and foreign keys, then its indexes; for a view CREATE VIEW with its stored query.