Prerequisite:
You have to complete the following tasks before practicing the SQLite statements of this tutorial:
A. Run the following command to create an SQLite file named “company.db” that contains the database tables:
B. Run the following SQLite command to check whether the database is created or not:
C. Run the following SQL statement to create a table named “products”. The table contains five fields and one primary key:
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
TYPE TEXT NOT NULL,
brand TEXT NOT NULL,
price INETEGER NOT NULL);
D. Run the following SQL statement to create a table named “suppliers”. The table contains five fields, one primary key, and one foreign key. So, the “products” table and “suppliers” table are connected with the foreign key:
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
address TEXT NOT NULL,
brand TEXT NOT NULL,
product_id INETEGER NOT NULL,
FOREIGN KEY(product_id) REFERENCES products(id));
E. Run the necessary commands to format the output and check the structure of the “products” table with header and tabular form.
The following command displays the header of the output:
The following command displays the column-based output:
The following command displays the structure of the “products” table in tabular form:
The structure of the “products” table is shown in the following image with the heading:
Syntax:
The syntax of the SQL command to drop the column of the table is given in the following. The ALTER TABLE statement is used to delete or add the column of the table.
Different Examples to Drop a Column from SQLite Table
The three ways to drop the column from an SQLite table are shown in this part of the tutorial.
Example 1: Drop the Column from the Table
Run the following ALTER TABLE statement to remove the “brand” field from the “products” table:
Run the following SQLite command to check whether the “brand” field is deleted or not from the “products” table:
The output shows that the “brand” field is removed from the “products” table.
Example 2: Drop the Column from the Table that is a Primary Key
The primary key is used in the table to identify each record separately. So, the primary can’t be dropped from the table and an error appears if the ALTER TABLE statement is used to remove the primary key field. Run the following ALTER TABLE statement to remove the “id” field from the “products” table which is the primary key of the table:
The following error appears if you want to drop the primary key of the table:
Example 3: Drop a Column from the Table that is a Foreign Key
Like the primary key, the foreign key of the table can’t be dropped until the relationship between the tables is removed. Run the following ALTER TABLE statement to remove the “product_id” field from the “suppliers” table which is foreign to the table:
The following error appears if you want to drop the foreign key of the table:
Conclusion
Three different types of examples are shown in this tutorial to drop a column from an SQLite table where it is examined that the primary and foreign key fields of the table can’t be dropped.