Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

SQLite does not support ALTER TABLE ... ALTER COLUMN ... TYPE. To change a column’s declared type while retaining its rows, rebuild the table in a transaction: create a replacement with the intended schema, copy the rows with any required conversion, replace the original, and restore dependent schema objects. SQLite documents this generalized procedure in its ALTER TABLE reference.

Why changing a column type requires rebuilding the table

SQLite’s directly supported ALTER TABLE operations include renaming a table or column, adding a column, and dropping a column. A datatype change is not a direct alteration; it uses the generalized schema-change procedure. That procedure can accommodate changes to the information stored in a table, but it requires copying the rows into a newly defined table.

Changing the column’s declared type and converting its stored values are related but distinct tasks. The copy step is where you can apply a conversion, but the right expression depends on the actual values and the representation your application expects. There is no universal conversion that is safe for every database.

Before you start: inventory the schema and plan the conversion

  • Inspect the table definition, constraints, and column order. Decide which constraints and other columns the replacement must retain.
  • Record the SQL for the table’s indexes and triggers, and inspect views that refer to the table. SQLite suggests querying sqlite_schema, for example: SELECT type, sql FROM sqlite_schema WHERE tbl_name='X'; Replace X with the table name.
  • Choose an explicit mapping from old columns to new columns. Avoid relying on column order: an explicit destination column list makes the copy easier to verify.
  • Determine whether foreign-key enforcement is enabled on the connection. The available support can depend on how SQLite was built, so verify the actual environment rather than assuming enforcement is active.
  • Back up the database and rehearse the migration against a staging copy with the application’s real schema and representative data. Validate converted values and application behavior before using the migration on the live database.

Check the SQLite version used by the application and test the exact migration there. SQLite’s rename behavior changed in versions 3.25.0 (2018-09-15) and 3.26.0 (2018-12-01), which is relevant to how renames affect references in views, triggers, and foreign-key definitions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Safe table-rebuild procedure

Adapt the names, columns, constraints, conversion expression, and schema objects below to your database. This illustrates the sequence; it is not a tested migration for a particular schema.

  1. Record the foreign-key setting. If enforcement is enabled, turn it off before the transaction begins. Changing PRAGMA foreign_keys inside a transaction or savepoint has no effect, according to the SQLite PRAGMA reference.
  2. Begin a transaction. The schema changes and row copy should be part of the same transaction so they can be committed together or rolled back if a step fails.
  3. Create a replacement table. Give it a temporary name and define the intended column type, all other required columns, and the constraints the finished table needs.
  4. Copy and, if necessary, convert the rows. Specify destination columns explicitly and map each one from the old table. Put any conversion expression in the corresponding SELECT expression.
  5. Drop the old table, then rename the replacement. Do not rename the old table out of the way first. SQLite warns that a rename-first sequence can rewrite references in views, triggers, and foreign-key definitions.
  6. Restore dependent schema objects. Recreate the saved indexes and triggers, adjusting their definitions if needed. Drop and recreate affected views when their SQL no longer matches the changed table.
  7. Check foreign keys if they were originally enabled. Run PRAGMA foreign_key_check; before committing. Resolve any reported violations before proceeding.
  8. Commit, then restore enforcement. Commit the transaction. If enforcement was enabled originally, turn it back on after the transaction.

Illustrative SQL template

-- Only if enforcement was originally enabled, and before BEGIN:
PRAGMA foreign_keys = OFF;

BEGIN;

CREATE TABLE new_X (
  id INTEGER PRIMARY KEY,
  value TEXT
  -- Reproduce the intended constraints and other columns.
);

INSERT INTO new_X (id, value)
SELECT id, CAST(value AS TEXT)
FROM X;

DROP TABLE X;
ALTER TABLE new_X RENAME TO X;

-- Recreate the original indexes and triggers, adjusted as needed.
-- Recreate affected views as needed.

-- If enforcement was originally enabled:
PRAGMA foreign_key_check;

COMMIT;

-- Only after the transaction, if it was originally enabled:
PRAGMA foreign_keys = ON;

CAST(value AS TEXT) only illustrates where conversion logic belongs; it is not a recommendation for every migration. Confirm that the chosen conversion handles the actual stored values and yields the representation the application requires. The template’s comments also stand in for schema details that must be supplied from your own database.

Rank #2
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common mistakes and how to avoid them

Renaming the original table first

Use a new temporary table, copy into it, drop the original, and rename the replacement. SQLite’s documented rebuild procedure warns against renaming the original first because that can change references in views, triggers, and foreign-key definitions.

Copying rows but forgetting schema objects

A successful insert does not restore indexes, triggers, or views. Save the relevant definitions before the rebuild; recreate indexes and triggers afterward, and inspect views separately for definitions that need updating.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Changing foreign-key enforcement inside the transaction

Set PRAGMA foreign_keys before BEGIN, not after. SQLite documents that changing it during a transaction or savepoint is a no-op. Restore the original setting only after the transaction ends.

Dropping a referenced table without checking the consequences

With foreign-key enforcement enabled, dropping a table performs an implicit delete. That operation can invoke foreign-key actions or fail when constraints are violated. Follow the documented rebuild ordering and use PRAGMA foreign_key_check when enforcement was originally enabled.

Editing the schema catalog as a shortcut

SQLite documents a writable_schema approach for certain changes that do not alter on-disk content. It is not the general procedure for changing a datatype. SQLite warns that malformed edits to sqlite_schema can make a database corrupt or unreadable.

How to verify the migration

  • Confirm that the replacement table has the intended column declaration, remaining columns, and constraints.
  • Check that expected rows were copied and inspect values that were converted, especially values outside the ordinary cases your application expects.
  • Verify that the required indexes and triggers exist and that affected views still work.
  • If foreign-key enforcement was enabled before the rebuild, inspect the output of PRAGMA foreign_key_check; before committing.
  • Run application-level checks against the migrated database. A successful table copy alone does not prove the new representation behaves correctly for the application.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.