Is it possible to delete a column from an SQLlite database. I have Googled this and it seems to be impossible.
From the ALTER TABLE manual;
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a column, remove a column, or add or remove constraints from a table.
So, no, not without dropping and recreating the table.
According to the docs:
It is not possible to rename a column, remove a column, or add or
remove constraints from a table.
I guess it has to be done manually.
Related
ALTER TABLE a add (OWNER_NAME VARCHAR2,OWNER_PARENT VARCHAR2);
Is it possible to alter table add MULTIPLE columns in a single statement in sqlite3?
The SQLite documentation provides the following picture to illustrate how the ALTER TABLE is understood by SQLite.
So, it does not seem possible to add multiple columns in a single ALTER TABLE command.
Reference: SQLite Query Language: ALTER TABLE
EDIT:
SQLite is a bit rigid when it comes to modifying existing tables and has limited support for the ALTER TABLE query.
Some more information can be found following this link: How do I add or delete columns from an existing table in SQLite.
The link also provides a workaround to carry out complex table modifications.
In a nutshell (emphasis mine):
If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
If I alter the table and add a new column will add the data be wiped off from that table.
ALTER TABLE MyTable ADD COLUMN noOfDays integer default 0 NOT NULL
Will the above SQL command drop MyTable and then ALTER it with the noOfDays column or will it simply add the noOfDays column without dropping it.
No, add just adds the column. Neither its data will be deleted nor the table be dropped.
p.s. from the documentation
"Note: also that when adding a CHECK constraint, the CHECK constraint is not tested against preexisting rows of the table." (check SQLite manual for more)
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.
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.
SQLite database browser allows you to drop columns, so you could drop the column with it, and then manually add the column with the default using the sqlite3 command line tool.
Create a new database with the same schema (but your new default value), attach both, and migrate.
For deleting columns or other changes that aren't supported by the "ALTER TABLE" syntax, I create a new table, migrate date into it, drop the old table, and rename the new table to the original name.
https://stackoverflow.com/a/998652/1020467
https://www.sqlite.org/lang_altertable.html
I tried INSERT OR REPLACE INTO, but it doesn't preserve the row id when it replaces the record to update it. Another option is do it in two steps: INSERT OR IGNORE INTO then UPDATE, but I would prefer a one step solution. So I am wondering if SQLite has something like the MERGE keyword or other simple solutions?
No, SQLite doesn't support MERGE or upsert.
You can use your two-step solution, but what you probably really want is for the ROWID to be a first-class column in your table. If you declare a column as INTEGER PRIMARY KEY, it will be an alias for the ROWID. Then INSERT OR REPLACE will work fine.
I want to remove a column from a table.
How can I do this?
As per the documentation, this is not possible without creating a new table. The method involves creating a new table (either temporary or the new table, depending on the complexity of changes), copying the data you need into it, deleting the old table, and remaking the table (if necessary).
You need simply to copy data you need to another table. Check out an example here:
robbiebow