Alter Table

Raise a dialog for table changing. This dialog looks quite similar to the "Create Table" one except for a few things:

Important

The built-in sqlite ALTER TABLE command is very limited since it can only rename the table or rename or add or drop a single column at a time.

Sqliteman provides a more general ALTER TABLE by creating a new table, copying the data, and dropping the old table. If necessary the old table is renamed out of the way first. However sqliteman has limited understanding of the many possible table structures and some information is lost in some cases.

Triggers on the the table itself are preserved (but cannot be modified) as long as any columns to which they refer are not deleted or renamed. Indexes on the table are preserved (but cannot be modified) unless a column to which an index refers is deleted: renaming indexed columns is handled.

If the table is renamed without any other changes and you are using sqlite library version 3.25.0 or later, and PRAGMA legacy_alter_table is OFF, views and triggers referencing the table will be re-attached to it with its new name. If the table is modified without changing its name, views and triggers referencing it will remain attached to it. However a view or a trigger may no longer be valid if it refers to a column which has had its name changed or has been deleted. In all other cases, views and triggers referencing the table will no longer reference it.

If the table is changed other than by just renaming it, Constraint names, conflict clauses, foreign key clauses, DESC tags, UNIQUE constraints, CHECK constraints, and COLLATE constraints are all lost. These can be preserved by manually renaming the old table, doing "Describe Table", copying and pasting the description into the SQL editor, modifying it as required and creating a new table, copying the data, and then dropping the old table.

If the table is changed other than by just renaming it, and the table is a parent table (a foreign key constraint in some other table references it), the Alter Table function may fail.

In this dialog you can Add and Remove table columns and set the options for new ones. You can also reorder the columns using the up and down arrows.

The Alter button is enabled only when at least something has changed from the original table definition, and the table name is nonempty, and the table name is not the same (ignoring case) at the name of any index, view, or other table, and all column names are different (ignoring case), and if there is an AUTOINCREMENT column its type is INTEGER and no other column is a PRIMARY KEY, and if Without rowid is checked there is at least one PRIMARY KEY column, and any column default values are admissible (see Appendix B).

Sqlite will permit a table to be created with a table name which contains non-alphanumeric characters or with an empty string as a column name, so sqliteman allows these. However in both of these cases it asks if you really want to do it, since the underlying library doesn't display such tables correctly.

Alter table dialog

Steps to alter table

  1. Remember triggers and indexes from the original table

  2. Rename the original table with temporary name if it is a case-insensitive match for the new name

  3. Create new table with new name containing new column structure

  4. Perform INSERT INTO new table SELECT FROM (possibly renamed) old table

  5. Drop old table

  6. Try to recreate original indexes and triggers

When there is no change other than the name, only the rename is performed.

The whole process is performed within a SAVEPOINT: if any error occurs the database is rolled back to the savepoint, reinstating the original table.