How do I drop a column from a sqlite database table? - sqlite

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

Related

how to alter table multiple column in sqlite3

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.

copy sqlite index from one database to another

I have a massive database (~800 GB) with several indexed tables. I need to copy one table (including indexes) to a new database. Copying the table itself is pretty straightforward.
$ sqlite3 newDB
> attach database 'oldDB.db' as oldDB
> create table newTable as select * from oldDB.oldTable
But I can't seem to find any information on a way to also copy over an index. Is there any way to do this? Since the tables are so large I'd really like to avoid having to re-index them.
SQLite has no mechanism to copy index contents.
If this particular table would be the majority of the data in the database, the fastest way to copy it would be to copy the database file and then to drop all other tables.
But otherwise, there you cannot avoid the reindex operation.
Please note that CREATE TABLE ... AS ... does copy only the contents of the table, but not the complete table definition (such as column types or constraints).
Copying large table in a single transaction is not a good idea. If you really have to you should turn off journaling first (destination database):
PRAGMA journal_mode=OFF;
As the others have stated, the index cannot be broken out. I suspect that time spent copying the database and then dropping a very large table would be longer than just -> 1. creating the new destination database, 2. determining the original CREATE TABLE statement (from the SQLITE_MASTER table of the source database) and recreating the table in the destination database. Then 3. just ATTACH your destination database to the source database and INSERT INTO destinationdb.tablename SELECT * FROM sourcedb.tablename;* to get the copy rolling.

xcode delete a column for a sql database

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.

sqlite3 change column default value

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

SQLite Modify Column

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.
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
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
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
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.
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.

Resources