Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I have a problem: I need to delete a column from my SQLite database. I wrote this query

alter table table_name drop column column_name 

but it does not work. Please help me.

1 Answer

0 votes
by (40.7k points)

Assume you have the table named "t1" with columns names "a", "b", and "c" and you want to delete column "c" from the table. 

The below steps will illustrate how to do this:

BEGIN TRANSACTION;

CREATE TEMPORARY TABLE t1_backup(a,b);

INSERT INTO t1_backup SELECT a,b FROM t1;

DROP TABLE t1;

CREATE TABLE t1(a,b);

INSERT INTO t1 SELECT a,b FROM t1_backup;

DROP TABLE t1_backup;

COMMIT;

For adding or deleting columns from an existing table in SQLite:

SQLite has limited ALTER TABLE support that can be used to add a column to the end of the table or to change the name of a table. But, if you wish to make more complex changes in the structure of the table, then you should recreate the table. Try saving the existing data to the temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.

For more information, you can refer to https://www.sqlite.org/faq.html

Industry-recognized Microsoft SQL Server database certifications can help you master SQL statements, queries and become proficient in SQL queries .

Related questions

0 votes
1 answer
asked Jul 17, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...