SQLite Delete Column: How to Remove a Column

Do you have an SQLite database table with a column you no longer need? Maybe your analytics table has an old “pageviews” column that you want to get rid of. Removing unnecessary columns can help keep your database clean and efficient. In this article, we’ll walk through exactly how to delete a column in SQLite, complete with examples using a sample analytics table.

To delete a column in SQLite, you need to create a new table without the unwanted column, copy over the data from the original table except for that column, drop the original table, and rename the new table to the original name. SQLite does not have an ALTER TABLE DROP COLUMN statement like some other databases, so removing a column requires these extra steps.

Let’s start by creating a simple analytics table and populating it with some sample data. Fire up your SQLite command-line tool and enter:

CREATE TABLE analytics (
  id INTEGER PRIMARY KEY,
  user_id INTEGER,
  event_type TEXT,
  timestamp TEXT,
  pageviews INTEGER
);

INSERT INTO analytics VALUES 
  (1, 100, 'click', '2023-05-01 10:00:00', 10),
  (2, 101, 'purchase', '2023-05-01 11:30:00', 5),
  (3, 100, 'impression', '2023-05-01 12:15:00', 20),
  (4, 102, 'click', '2023-05-01 14:45:00', 8);

This creates an analytics table with columns for an ID, user ID, event type, timestamp, and pageviews. We’ve inserted a few rows of dummy data.

Now let’s say we want to remove that “pageviews” column. Here are the steps:

1. Create a New Table Without the Unwanted Column

The first step is creating a new table that looks just like our existing table but without the column we want to delete. Use a CREATE TABLE statement like:

CREATE TABLE analytics_new (
  id INTEGER PRIMARY KEY,
  user_id INTEGER, 
  event_type TEXT,
  timestamp TEXT
);

Notice how this is the same as our original table definition but without the “pageviews” column.

2. Copy Data to the New Table

Next, we need to copy all the data from the original table to the new one, omitting the unwanted column. An INSERT INTO SELECT statement does the trick:

INSERT INTO analytics_new (id, user_id, event_type, timestamp)
SELECT id, user_id, event_type, timestamp 
FROM analytics;

This selects all columns except “pageviews” from the original table and inserts those values into the corresponding columns in the new table.

We can confirm the data copied over correctly with:

SELECT * FROM analytics_new;

Output:

id | user_id | event_type | timestamp           
---+---------+------------+--------------------
 1 |     100 | click      | 2023-05-01 10:00:00
 2 |     101 | purchase   | 2023-05-01 11:30:00
 3 |     100 | impression | 2023-05-01 12:15:00
 4 |     102 | click      | 2023-05-01 14:45:00

3. Drop the Original Table

Now that we have the data moved over, we can safely drop the original table using:

DROP TABLE analytics;

This deletes the table completely, so make absolutely sure you’ve copied everything you need to the new table first!

4. Rename the New Table

The last step is renaming our new table to the original name. A simple ALTER TABLE RENAME does this:

ALTER TABLE analytics_new RENAME TO analytics;

And we’re done! If we SELECT from the analytics table now, we’ll see it has all the original data but without the deleted “pageviews” column:

SELECT * FROM analytics;

Output:

id | user_id | event_type | timestamp           
---+---------+------------+--------------------
 1 |     100 | click      | 2023-05-01 10:00:00 
 2 |     101 | purchase   | 2023-05-01 11:30:00
 3 |     100 | impression | 2023-05-01 12:15:00
 4 |     102 | click      | 2023-05-01 14:45:00

Real-World Example

Let’s consider a realistic scenario where you might need to delete a column. Imagine you work for a social media company that tracks user interactions in an analytics table. The table has columns for the user ID, interaction type (like, comment, share), content ID, and timestamp.

CREATE TABLE interactions (
  id INTEGER PRIMARY KEY,
  user_id INTEGER,
  interaction_type TEXT,
  content_id INTEGER,
  content_type TEXT,
  timestamp TEXT
);

Initially, you track the content type (post, video, poll, etc.) in a “content_type” column. But after a while, you realize you can derive this information by joining the content ID to your contents table. The “content_type” column becomes redundant.

To remove it, you’d follow the same steps:

CREATE TABLE interactions_new (
  id INTEGER PRIMARY KEY,
  user_id INTEGER,
  interaction_type TEXT, 
  content_id INTEGER,
  timestamp TEXT
);

INSERT INTO interactions_new (id, user_id, interaction_type, content_id, timestamp) 
SELECT id, user_id, interaction_type, content_id, timestamp
FROM interactions;

DROP TABLE interactions;

ALTER TABLE interactions_new RENAME TO interactions;  

Deleting the unnecessary column simplifies your schema without losing any information.

Caveats and Alternatives

While the process outlined above works, it can be cumbersome, especially for large tables. Copying millions of rows to a new table can be slow and disk space intensive.

Some alternatives to consider:

  • If you don’t care about keeping the column name, you can skip renaming the new table. Just drop the old table and use the new one as is.
  • If disk space is a concern, you can drop the original table first (after confirming you don’t need it!) and then create the new table in its place. This avoids having two copies of the data at once.
  • For very large tables, it may be faster to use a tool like SQLite’s command-line backup API to make a copy of the database file, modify the schema in the copy, and then replace the original file.

That covers everything you need to know about deleting columns in SQLite! While not as straightforward as an ALTER TABLE DROP COLUMN, the process of creating a new table, copying data over, and dropping the old table is effective. Keep it in mind next time you’re cleaning up an SQLite database schema.