Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite3 change column default value

Tags:

sqlite

How do I change the default value of an existing column in a table in sqlite3?

I have a table named notes with a boolean column named hidden. The default is set to true, I want to set it to false.

like image 700
kentor Avatar asked Mar 30 '12 00:03

kentor


People also ask

How do I change the default value in SQLite?

To set default value for a column in SQLite, use DEFAULT :CREATE TABLE customers ( id INTEGER PRIMARY KEY, store_code TEXT DEFAULT "store1" NOT NULL, name TEXT );

Does ALTER TABLE work in SQLite?

Overview. SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows these alterations of an existing table: it can be renamed; a column can be renamed; a column can be added to it; or a column can be dropped from it.

How do I edit a table in SQLite?

Summary. Use the ALTER TABLE statement to modify the structure of an existing table. Use ALTER TABLE table_name RENAME TO new_name statement to rename a table. Use ALTER TABLE table_name ADD COLUMN column_definition statement to add a column to a table.

Can we change column name in SQLite?

Use the ALTER TABLE RENAME COLUMN to rename a column in a table. If you are using SQLite 3.25. 0, you should upgrade it and use the new syntax.


1 Answers

I don't think you can without replacing the whole table. From the fine manual:

SQL Features That SQLite Does Not Implement

Complete ALTER TABLE support
Only the RENAME TABLE and ADD COLUMN variants of the ALTER TABLE command are supported. Other kinds of ALTER TABLE operations such as DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted.

So there is no way to modify an existing column in SQLite. I think you'll have to create a new table with the appropriate default for hidden, copy all the data over, drop the original notes table, and then rename the new one.

SQLite stays lean by purposefully omitting a lot of features.

like image 116
mu is too short Avatar answered Nov 10 '22 00:11

mu is too short