Change the structure of a table
See what a table is made of, and change its columns, indexes, relations and rules. Each change waits until you read its SQL and run it.
On this page
Read the structure
Open a table and press Structure. It begins with the table in words: what names each row, which tables it points to, what a new row has to bring. Then come its columns, indexes, relations and rules, and the SQL that makes it.
Press the name of another table in Relations to open it.
Prepare your changes
| To | Press |
|---|---|
| Add a column | Column |
| Change the name, the type, the default or the NULL of a column | The pencil of its row |
| Add an index | Index. Its columns go in the order in which you mark them |
| Point to another table | Relation. Choose the columns, the other table, and what happens when the row pointed to goes away |
| Add a condition that every row must pass | Rule. Write it as SQL: price >= 0 |
| Drop any of them | The bin of its row |
Each change goes to a list above the columns. The cross of a change takes it out.
Save them
- Press Review and save.
- Read the statements. They run in that order.
- Press Run.
In PostgreSQL, SQL Server and SQLite, if one fails, none is kept. MySQL and MariaDB keep each change of structure as soon as it runs, so those before the one that fails stay.
The list stays when you go to Data and back. If you close the tab before you run it, TotalSQL asks. When changes of rows wait too, it asks before you change the structure, because the rows are read again after it: save them first.
Dropping a column loses its values for good.
What happens when the row goes away
| Choice | What the database does |
|---|---|
| Refuse to drop it (the usual) | The row cannot be dropped while others point to it |
| Drop these rows with it | Drops the rows that point to it |
| Empty the column here | Puts NULL in the rows that point to it |
| Refuse, at once | Refuses without waiting for the end of the transaction, in the databases that tell them apart |
| Put the default here | Puts the default of the column |
What needs a query
- The key of the table, and its columns, cannot be dropped here.
- The type of a column that the database fills.
- The comments of columns, which are only shown.
- The definition of a view.
- In SQLite, the type, the default and the NULL of a column, and the relations and the rules. SQLite needs the table to be made again for that. A new column that cannot be NULL needs a default.