SQLite Modify Column

SqliteAlter Table

Sqlite Problem Overview


I need to modify a column in a SQLite database but I have to do it programatically due to the database already being in production. From my research I have found that in order to do this I must do the following.

  • Create a new table with new schema
  • Copy data from old table to new table
  • Drop old table
  • Rename new table to old tables name

That seems like a ridiculous amount of work for something that should be relatively easy. Is there not an easier way? All I need to do is change a constraint on a existing column and give it a default value.

Sqlite Solutions


Solution 1 - Sqlite

That's one of the better-known drawbacks of SQLite (no MODIFY COLUMN support on ALTER TABLE), but it's on the list of SQL features that SQLite does not implement.

edit: Removed bit that mentioned it may being supported in a future release as the page was updated to indicate that is no longer the case

Solution 2 - Sqlite

If the modification is not too big (e.g. change the length of a varchar), you can dump the db, manually edit the database definition and import it back again:

echo '.dump' | sqlite3 test.db > test.dump

then open the file with a text editor, search for the definition you want to modify and then:

cat test.dump | sqlite3 new-test.db

Solution 3 - Sqlite

As said here, these kind of features are not implemented by SQLite.

As a side note, you could make your two first steps with a create table with select:

CREATE TABLE tmp_table AS SELECT id, name FROM src_table

Solution 4 - Sqlite

When I ran "CREATE TABLE tmp_table AS SELECT id, name FROM src_table", I lost all the column type formatting (e.g., time field turned into a integer field

As initially stated seems like it should be easier, but here is what I did to fix. I had this problem b/c I wanted to change the Not Null field in a column and Sqlite doesnt really help there.

Using the 'SQLite Manager' Firefox addon browser (use what you like). I created the new table by copying the old create statement, made my modification, and executed it. Then to get the data copied over, I just highlighted the rows, R-click 'Copy Row(s) as SQL', replaced "someTable" with my table name, and executed the SQL.

Solution 5 - Sqlite

Various good answers already given to this question, but I also suggest taking a look at the sqlite.org page on ALTER TABLE which covers this issue in some detail: What (few) changes are possible to columns (RENAME|ADD|DROP) but also detailed workarounds for other operations in the section Making Other Kinds Of Table Schema Changes and background info in Why ALTER TABLE is such a problem for SQLite. In particular the workarounds point out some pitfalls when working with more complex tables and explain how to make changes safely.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNathanView Question on Stackoverflow
Solution 1 - SqliteDaniel DiPaoloView Answer on Stackoverflow
Solution 2 - SqliteDavide VernizziView Answer on Stackoverflow
Solution 3 - SqliteJhonny D. Cano -Leftware-View Answer on Stackoverflow
Solution 4 - SqliterichView Answer on Stackoverflow
Solution 5 - SqliteAndrew RichardsView Answer on Stackoverflow