# Table designer

> Create a table or change its structure with a form, and review the SQL before running it.

Source: https://www.opendb.tools/en/docs/table-designer

## Open the designer

- **Create a table**: the `+` button at the top of the table list.
- **Change a table**: the table's **Structure** view, then **Edit the structure**.

Creating a table requires the `CREATE` privilege. Changing one requires `ALTER`.

## Describe the table to the assistant

When you create a table, the **Describe the table** button, or `Ctrl` + `I`, opens an assistant next to the form. Describe the table in your own words:

```text
the bookings of a hotel: the room, the guest, the arrival and departure dates
```

The assistant reads your existing tables and follows their naming conventions. It suggests the name, the columns, the primary key, and the useful indexes and foreign keys, summing up what changes in the form.

- **Accept** keeps the proposal in the form, where you can still change it.
- **Reject** puts the form back as it was.
- A new request refines the proposal: "add an index on the arrival date".

Each proposal is checked by the same rules as the form. Nothing is created until you run the SQL yourself.

## Columns

For each column:

| Setting | Detail |
|---|---|
| Name | The column name |
| Type | One of 25 types: integers, decimals, text, dates, boolean, JSON, enum... |
| Size, precision, scale | Depending on the type |
| Unsigned | For numeric types |
| Nullable | Whether the column accepts `NULL` |
| Default value | None, a value, the current date or an expression |
| Auto-increment | For the primary key |
| Values | For an enum |

One option adds the creation and update date columns in a click.

A column whose type the designer does not offer is kept as it is, as long as you do not change it.

### Change the order of the columns

Drag a column by its handle to move it, with the mouse or the keyboard. In the designer, the new order is applied with the rest of the change.

In the **Structure** view of an existing table, moving a column first shows the statements that will run. The behavior depends on the engine:

| Engine | Moving a column |
|---|---|
| MySQL, MariaDB | The table is rebuilt and the change committed at once, with no way back. It can take a while on a large table. |
| SQLite | The table is created again in the new order, with its rows, indexes and triggers, in a transaction. |
| PostgreSQL | Not supported: the columns stay in the order they were added. |

## Indexes

Add a **plain** or **unique** index, on one or several columns.

## Foreign keys

For each foreign key:

- the columns of the table;
- the referenced table and columns;
- the action when the referenced row is deleted or updated.

## Review the SQL

Before running, click **Check the SQL**. OpenDB shows the statements written for your engine. What is shown is exactly what will run.

## What happens when it runs

The behaviour depends on the engine.

| Engine | Behaviour |
|---|---|
| PostgreSQL | The whole change runs in a transaction. A refusal rolls everything back. |
| MySQL, MariaDB | Each statement is committed at once. If one fails, the earlier ones stay applied. |
| SQLite | The table is rebuilt. The change cannot be rolled back. |

The interface warns you before running when the change cannot be rolled back.

> [!WARNING]
> On MySQL, MariaDB and SQLite, back up the table before a major structure change. You can duplicate it from the **Table actions** menu.

## Other ways to create a table

- **Describe the table** to the [assistant of the designer](#describe-the-table-to-the-assistant), or to the [design assistant](/en/docs/design-assistant) of the ERD schema, which also takes a photo of a schema.
- **Write the `CREATE TABLE`** in the [SQL editor](/en/docs/sql-editor).
- **Ask the agent** in the chat, in read-write mode.
