Tables, Columns and Indexes

In DbSchema, you create and change tables in dialogs instead of writing SQL. DbSchema writes the SQL for your database, such as an ALTER TABLE statement, and runs it. You work on tables from the diagram in the middle of the window, or from the tree on the left. The tree lists each schema with its tables, views and other objects. Use the diagram to explain database schema relationships by following the lines between tables.

DbSchema with the shop schema: the tree of schemas and tables on the left, the tables customers, orders, products and the others on the diagram in the middle

The pictures on this page use a small shop database in PostgreSQL 17: customers, orders, products and the tables around them. Database Terms explains table, column, primary key, index and the other words on this page.

Choose where your changes go

Two words are used in every task on this page. Your DbSchema project holds the tables, columns and diagrams that you see in DbSchema. This page calls it the design; DbSchema also calls it the model. The database is the real database on the server, such as PostgreSQL.

When DbSchema is connected, pressing OK in a dialog applies your change to both the design and the database immediately. The toolbar then shows the name of the connection, such as PostgreSQL. When you work offline, your changes stay in the design, and the database doesn't change.

To work offline, choose Database → Offline (connect later). The toolbar then shows Offline in place of the connection name.

The DbSchema toolbar with Offline shown in place of the connection name

Later, you can compare the design with the database, review your changes and apply them. This is called synchronization, and it needs DbSchema Pro.

What OK and Cancel do

Double-click a table to open the Edit Table dialog. From there, you open a separate dialog for each column, index or key. In a table that already exists, OK in one of those dialogs applies its change at once. If you are connected, DbSchema runs that change in the database right then.

Cancel in the Edit Table dialog only cancels the changes made directly in that dialog, such as a new table comment. It does not undo changes to columns or indexes that you have already confirmed with OK.

Edit → Undo reverses only changes to the diagram, such as moved tables. It doesn't reverse a change to a table. To reverse a change, edit the table again: for example, drop the column that you added.

The design is stored in a .dbs file. Save it with File → Save Model to File, or Ctrl+S. Saving the design to a file needs DbSchema Pro.

Create a table

  1. Right-click an empty spot on the diagram, where the new table should go, and choose Create Table. You can also press Table in the toolbar and choose Create Table, or press Ctrl+T.
  2. Type the name in Table Name.
  3. In the Columns tab, press Add for each column, as Add a column shows.
  4. Press OK.

The new table shows on the diagram and in the tree. If you are connected, OK creates the table in the database at once.

A table needs at least one column. If you press OK without one, DbSchema shows "Please add at least one column by pressing the '+' button." The '+' button is Add, in the Columns tab.

If the design holds one schema, the new table goes into it. If it holds several, DbSchema asks which schema the table goes into.

Edit a table

Double-click the table header on the diagram to open the Edit Table dialog. Each tab holds one part of the table:

TabWhat it holds
ColumnsThe columns, with their data types and comments
Primary Key / Unique / IndexesThe primary key, the unique keys and the indexes
Foreign KeysThe links from this table to other tables
Check ConstraintsThe conditions that each row must meet
OptionsClauses added to CREATE TABLE, such as TABLESPACE
ScriptsThe Prepend Script and Append Script, SQL that runs before and after the CREATE TABLE
The Edit Table dialog of DbSchema for customers, with the comment Everyone who created an account and the Columns tab open

Foreign Keys covers the Foreign Keys tab.

The Comment box above the tabs describes the table. If you are connected, DbSchema writes the comment to the database as a COMMENT ON TABLE statement. The HTML5 documentation shows it too.

The Options tab only affects the creation of a table. Changing these options on an existing table doesn't change the database.

Tick Virtual, at the top, to keep the table only in the design. DbSchema never creates a virtual table in the database. If you are connected and the table already exists in the database, you cannot tick Virtual.

Add a column

  1. Double-click the table header.
  2. In the Columns tab, press Add.
  3. Type the column name. DbSchema suggests a data type from the name.
  4. Check the data type, such as varchar, integer or date, and choose another one if you need to.
  5. Press OK in the column dialog, then OK in the table dialog.

DbSchema suggests the data type in this order:

  1. If another column of the schema has the same name, DbSchema suggests its data type. For email, it suggests varchar(120), because customers.email has that data type.
  2. Otherwise, DbSchema reads the meaning of the name. It suggests integer for supplier_id, date for birth_date and boolean for is_active.
  3. If DbSchema cannot tell the data type from the name, it suggests the data type that you chose last, or varchar(255).

When you choose a data type yourself, DbSchema stops suggesting one for that column.

In a table that already exists, OK in the column dialog adds the column at once, as What OK and Cancel do explains. For the clip above, DbSchema ran this in PostgreSQL:

ALTER TABLE shop.customers ADD birth_date date;
COMMENT ON COLUMN shop.customers.birth_date IS 'Used for birthday offers';

The column dialog holds the settings of one column:

The New Column dialog of DbSchema for the column birth_date of the customers table, with the suggested data type date and a comment
FieldWhat it sets
Length, or PrecisionThe size: Length for text, such as 100 in varchar(100), Precision for numbers
DecimalThe digits after the decimal point
Primary KeyWhether the column belongs to the primary key
Mandatory ( Not Null )Whether every row must have a value
UnsignedWhether negative numbers are refused, where the database has unsigned types
Identity...Whether the database numbers new rows, such as GENERATED BY DEFAULT AS IDENTITY
DefaultThe value that a new row gets when the insert gives none

Unsigned stays disabled on PostgreSQL, which has no unsigned types. The Now() button writes the database's function for the current date or time into Default. For a date column, it writes CURRENT_DATE. The database then fills in the date when a row is inserted.

Edit or drop a column

To edit a column, double-click it on the diagram. The Edit Column dialog opens, with the same fields as above. You can also select the column in the Columns tab of the table dialog and press Edit.

To drop a column:

  1. Right-click the column on the diagram and choose Drop.
  2. Confirm with Drop, then press OK in the warning about data loss.

In the table dialog, you can also select the column in the Columns tab, press Drop, then OK.

The Columns tab of the DbSchema table dialog for customers, with the column phone selected and the buttons Add, Edit and Drop below the list

If you are connected, DbSchema drops the column in the database at once, with all its data. For PostgreSQL, DbSchema runs ALTER TABLE … DROP COLUMN … CASCADE. This also deletes the objects that use the column, such as a view.

Set the primary key

A primary key is the column, or set of columns, that identifies each row. No two rows can share its value, and the value can never be NULL.

To make one column the primary key, open its column dialog and tick Primary Key. A primary key can also use several columns, such as order_id and product_id in order_items. To create one, choose the index type Primary Key:

  1. Double-click the table header and open the Primary Key / Unique / Indexes tab.
  2. Press Add.
  3. Choose Primary Key.
  4. Press Add under Columns for each column of the key.
  5. Press OK in both dialogs.
The Edit Index dialog of DbSchema for order_items_pkey, a primary key over the columns order_id and product_id

A key icon marks the primary key columns on the diagram. What the icons mean lists the other column icons.

Create an index

An index lets the database find rows by a column without reading the whole table. A unique index also refuses a second row with the same value.

  1. Double-click the table header.
  2. Open the Primary Key / Unique / Indexes tab.
  3. Press Add.
  4. Choose Primary Key, Unique Key, Unique Index or Index.
  5. Press Add under Columns and pick a column. Repeat for each column of the index.
  6. Press OK in the index dialog, then OK in the table dialog.
The Primary Key / Unique / Indexes tab of the DbSchema table dialog for customers, with the primary key customers_pkey and the unique key customers_email_key

On the diagram, a magnifying glass marks an indexed column, such as customer_id in orders. A magnifying glass in a frame marks a column of a unique key or a unique index, such as email in customers.

A Unique Key is a constraint, a rule that the values must follow. The database creates an index for this rule. A Unique Index is only an index, with no constraint. In PostgreSQL 17, both prevent duplicate values. However, both allow any number of NULL values. Only the unique key appears in the table's list of constraints. Suppose that products_sku_key is a unique key on products.sku. If you insert 'A-1' a second time, PostgreSQL shows this error:

ERROR:  duplicate key value violates unique constraint "products_sku_key"
DETAIL:  Key (sku)=(A-1) already exists.

Use advanced index options

The index dialog has more options for special cases: an index on an expression, a sort order, and a condition. The pictures show PostgreSQL; the options that the dialog offers depend on your database.

Index an expression

An index can hold an expression instead of a column. A unique index on lower(email) prevents duplicate email addresses, even if they use different uppercase or lowercase letters.

  1. In the index dialog, open the arrow next to Add under Columns.
  2. Choose Add Expression.
  3. Type the expression, such as lower(email), and press OK.
The Create Index dialog of DbSchema with the unique index customers_email_lower on the expression lower(email)

DbSchema creates the index with:

CREATE UNIQUE INDEX customers_email_lower ON shop.customers ( lower(email) );

With that index in place, PostgreSQL 17 refuses '[email protected]' when '[email protected]' is already there:

ERROR:  duplicate key value violates unique constraint "customers_email_lower"
DETAIL:  Key (lower(email::text))=([email protected]) already exists.

Sort or filter an index

For the type Index, the index dialog has an Options (editable) column and a WHERE Clause & Storage Options field.

To set the sort order of a column, enter ASC or DESC in its Options (editable) cell. To index only some rows, enter a WHERE clause in the field below the columns, such as WHERE status = 'NEW'. The index then holds only the rows that match the condition.

The Create Index dialog of DbSchema for orders_new_recent on orders, with ordered_at sorted DESC and the condition WHERE status = 'NEW'

For the index in the picture, DbSchema runs:

CREATE INDEX orders_new_recent ON shop.orders ( ordered_at DESC ) WHERE status = 'NEW';

See the SQL that DbSchema runs

To read the table's SQL, press SQL at the bottom of the table dialog. The View SQL Statement window shows the complete CREATE TABLE statement and the table's comments.

The View SQL Statement window of DbSchema with the CREATE TABLE statement of customers and its comments

For a table that already exists, DbSchema doesn't run this CREATE TABLE. DbSchema runs only the change, such as the ALTER TABLE in Add a column. It doesn't show this statement before running it. The History pane at the bottom left lists every statement that DbSchema ran.

Drop a table

Drop and Exclude from Diagram both remove a table from the diagram. Only Drop deletes the table itself.

Menu itemWhat it removes
DropThe table, from the design and, if you are connected, from the database, with all its rows
Exclude from DiagramThe table's box on this diagram only. The table stays in the design, in the tree and in the database.

Drop uses CASCADE in PostgreSQL, so PostgreSQL also removes the other objects that use the table, such as views or foreign keys from other tables. Check these objects before you confirm.

To drop a table:

  1. Right-click the table header on the diagram.
  2. Choose Drop, or press Ctrl+Delete.
  3. Press Drop, then OK when DbSchema warns that the operation may lead to data loss.
The DbSchema confirmation dialog asking Drop Table 'wishlists' ?, with the buttons Drop and Cancel

For wishlists, DbSchema ran:

DROP TABLE shop.wishlists CASCADE;

To exclude a table instead, choose Exclude from Diagram in the same menu, or press Delete.

Find the other objects of a schema

The tree lists more than tables. Under each schema, a folder holds each kind of object, such as Views, Sequences, Procedures and Triggers. Double-click an object to open its dialog.

The page Views, Procedures and Triggers explains how to edit views, procedures and triggers, and how to rename them. User Data Types covers the folder Type or Domains. Foreign keys show as lines on the diagram, as Foreign Keys shows.

The DbSchema tree with the folders of the shop schema, and the Server node open with the tablespaces pg_default and pg_global

Below the schemas is the Server node. It lists objects that come directly from the database server, not from your design. You must be connected to see them. DbSchema loads each list when you open its folder. For PostgreSQL, the Server node holds the Tablespaces, the Collations and the Character Sets. For MySQL, it also holds the engines. For Oracle, each schema has a Server node of its own, with the packages and package bodies of that schema. For TimescaleDB, it holds the hypertables of each schema.

Common problems

What do the icons next to the columns mean?

A key marks a primary key column. A magnifying glass in a frame marks a column of a unique key or unique index. A plain magnifying glass marks an indexed column. What the icons mean lists every icon.

I pressed Cancel, but the new column is still there

OK in the column dialog adds the column at once, before you close the table dialog. Cancel in the table dialog doesn't remove it. To remove the column, drop it, as Edit or drop a column shows.

The Primary Key box is disabled

The table already has a primary key on another column. To add the column to that key, edit the key in the Primary Key / Unique / Indexes tab, as Set the primary key shows.

Next steps