Help › Change a database's structure
Create and change tables
Add, rename and remove columns, keys, indexes, foreign keys and checks in a form, read the SQL before it runs, and apply it safely.
Querlyn 0.1.0 is not released yet. This help describes it ahead of release.
The table designer lets you create a table or change one without writing
the CREATE TABLE and ALTER TABLE statements yourself. You edit a form;
Querlyn writes the SQL, shows it to you, and runs it only when you choose
Apply.
It works on every database Querlyn supports. What each database cannot change is listed further down.
Open the designer
| To | Do this |
|---|---|
| Change a table | Select the table in the sidebar and choose , or right-click it and choose Edit Structure…. |
| Change the table your cursor is on in the editor | |
| Create a table | Select a schema, database, connection or table in the sidebar and choose , or right-click and choose New Table…. |
The designer opens in a tab of its own. It reads the table's definition
fresh from the database each time it opens. A new table starts with one
column, id, as the primary key. Where the database can number rows
itself, the column is set to do so.
The parts of the tab
- At the top: the table's name and comment, and the buttons Apply, Open in Editor and Revert. For a table that exists, on a database with users, Privileges… opens who may do what on it; see Manage users, roles and privileges.
- Sections: Columns, Keys and Indexes, Foreign Keys and Checks, each with a count. ⌃⇟ on macOS or Ctrl+PageDown on Windows and Linux and ⌃⇞ on macOS or Ctrl+PageUp on Windows and Linux go from section to section.
- The list of the section's items, with the buttons to add, Remove, Move Up and Move Down above it. New and changed items are marked.
- The form of the item selected in the list.
- The preview: the statements Querlyn will run, numbered, each with a sentence saying what it does.
Edit the table
| To | Do this |
|---|---|
| Add an item to the section shown | The add button above the list (Add Column, Add Index, Add Foreign Key, Add Check), or ⌘N on macOS or Ctrl+N on Windows and Linux. In Keys and Indexes there are also Add Primary Key and Add Unique. |
| Add an item of one kind, whatever section is shown | , , , , , |
| Change an item | Select it in the list and edit its form. |
| Remove the selected item | Remove, or ⌘⌫ on macOS or Delete on Windows and Linux in the list |
| Move a column up or down | Move Up and Move Down, or ⌥↑ on macOS or Alt+Up on Windows and Linux and ⌥↓ on macOS or Alt+Down on Windows and Linux in the list |
| Rename the table | Edit its name at the top. |
What the forms hold:
| Section | Fields |
|---|---|
| Columns | Name, Type (pick one or type any), Length / precision, Scale, Allow NULL, Default, Identity, Collation, Comment |
| Keys and Indexes | Name (leave empty for an automatic name), Columns, Unique, the index Method, and Only rows where for an index on part of the rows |
| Foreign Keys | Name, Columns, Refers to table, Referenced columns, On delete, On update |
| Checks | Name, Condition |
Fields a database has no use for are not shown.
A list of columns is typed with commas, for example customer_id, created_at DESC.
In an index, an entry in parentheses is an expression: (lower(email)).
Things to know while editing:
- A rename is a rename. Changing a column's name renames the column and keeps its data. It is not written as a drop and an add.
- Removing a column removes what uses it: keys, indexes, foreign keys and checks that name it. A message says which.
- Choosing an identity turns Allow NULL off and clears the default.
- Problems are shown under the field they concern, for example a name used twice, or a foreign key that does not match a key of the table it refers to. While there is a problem, Apply is disabled.
- Revert drops all your changes and shows the table as it is in the database ().
Read the preview
The preview changes with every edit. Each statement can carry warnings:
| Warning | Meaning |
|---|---|
| Drops table, Drops column, Drops constraint | Something is deleted, with its data. |
| Narrows type, Converts type | Values that do not fit the new type make the statement fail, or are converted. |
| May fail on existing data | Rows already in the table may break the new rule. |
| Rewrites table, Locks table | Other sessions wait while the database works on the table's rows; see Migration lock warnings. |
| Renames | SQL elsewhere that uses the old name must change. |
| Can't be rolled back | The statement takes effect at once on this database, whatever happens to the statements after it. |
Open in Editor puts the statements in a new query tab without running them (), each after its description as a comment. Use it to adjust what Querlyn wrote, or to keep the script. The tab is on the table's database, so the script runs where the table is.
Apply the changes
Choose Apply, or press ⌘S on macOS or Ctrl+S on Windows and Linux.
- Querlyn reads the table again. If someone changed it since the designer opened, it asks: Apply Anyway applies your changes to the table as it is now, Reload Table drops your changes and shows the new definition.
- On a Production connection Querlyn shows the statements that delete something, or that it cannot classify, and asks; the button reads Apply Changes. On Testing and Staging it asks about statements that delete something. See Confirm risky statements.
- The statements run on a session of their own, not on a query tab's.
What happens if a statement fails depends on the database:
| Database | If a statement fails |
|---|---|
| PostgreSQL, SQL Server, SQLite, DuckDB, Amazon Redshift | Nothing is changed: the statements run in one transaction. (On Redshift a few kinds of change run before the transaction and stay; the preview marks them.) |
| MySQL, MariaDB, Oracle Database, ClickHouse, Snowflake | Each statement takes effect at once. The statements that ran stay; the preview marks them Applied and the failed one Failed here. |
On PostgreSQL, MySQL, MariaDB, SQL Server and Oracle Database, a change waits at most 30 seconds for a lock another session holds, for example a transaction left open in a query tab; on SQLite, 5 seconds. Then it fails like any other statement. Once it has its locks there is no time limit: rewriting a large table can take long.
A read-only connection refuses Apply; see Read-only connections.
Closing with changes not applied
If you close the tab with changes not applied, Querlyn asks: Apply… applies them and then closes, Discard Changes closes without, and Cancel keeps the tab. Quitting asks once whether to discard them.
What depends on the database
Where a database cannot make a change, the designer says so under the field, with what to do instead. It does not work around it silently.
| Database | Notes |
|---|---|
| PostgreSQL | Columns cannot be reordered; new columns go last. A change of type is written without a conversion rule (USING); if PostgreSQL needs one it says so, and you can add it after Open in Editor. |
| MySQL, MariaDB | The changes to one table are written as one ALTER TABLE, with foreign keys in statements of their own. Unique keys are unique indexes. |
| SQL Server | Columns cannot be reordered. Identity cannot be added to or removed from an existing column. |
| SQLite | Changes SQLite cannot make in place are done by building the table again and copying its rows; the preview shows every step. |
| DuckDB | No identity columns. Keys, unique constraints and checks cannot be added to or changed on an existing table. |
| Amazon Redshift | A Distribution and Sort Key section and a compression encoding for each column; no indexes or checks. Most changes to existing columns are refused. Keys are recorded but not enforced. |
| ClickHouse | No foreign keys, unique constraints or identity columns. An existing table's engine and keys are kept as they are. |
| Oracle Database | Plain names are folded to upper case. Only type changes that keep the rows are allowed. Identity cannot be added to an existing column. |
| Snowflake | Keys are recorded but not enforced. No indexes, checks or computed columns. Few changes to existing columns are possible. |
What the designer does not edit
Computed columns, partitions, storage options such as a MySQL table's engine or a tablespace, and dropping a table. For those, write the SQL in a query tab. To drop a table from a picture of the schema, see Draw an ER diagram.
Help for Querlyn 0.1.0. All help topics