Table of Contents

How to alter a table structure with SQLite?

About

The alter statement of SQLite is pretty limited. This page shows you:

What can you do?

You can:

and that's it as you can see in this railroad diagram

Alter Table Railroad Sqlite

What can't you do?

You can't modify

Solution

Full procedure

As you can't modify the structure via an alter statement, you need to:

The detailed procedure as stated in the documentation 1) is:

Simplified

For changes that do not affect the on-disk content such as:

the Sqlite documentation points to a simplified procedure.

Check the last paragraph of the alter section 7 documentation if you are interested

Example with primary key modification

An example where a name was taken as identifier and that a ID will take over.

PRAGMA foreign_keys=OFF
BEGIN TRANSACTION;
create table THINGS_TMP
(
    ID INTEGER PRIMARY KEY,
    NAME TEXT UNIQUE,
    DESCRIPTION TEXT
);
insert into THINGS_TMP (NAME, DESCRIPTION) from THINGS;
drop table THINGS;
alter table THINGS_TMP rename to THINGS;
PRAGMA foreign_key_check ;
Commit;
PRAGMA foreign_keys=ON