This SQLite post explains how to use the SQLite ALTER TABLE statement to add a column, modify a column, drop a column, rename a column or rename a table (with syntax and examples).
The SQLite ALTER TABLE statement is used to add, modify, or drop/delete columns in a table. The SQLite ALTER TABLE statement is also used to rename a table.
The syntax to ADD A COLUMN in a table in SQLite (using the ALTER TABLE statement) is:
The name of the table to modify.
The name of the new column to add to the table.
The datatype and definition of the column (NULL or NOT NULL, etc).
Let's look at an example that shows how to add a column in a SQLite table using the ALTER TABLE statement.
For example:
This SQLite ALTER TABLE example will add a column called status to the employees table. It will be created as a column that allows NULL values.
You can not use the ALTER TABLE statement to modify a column in SQLite. Instead you will need to rename the table, create a new table, and copy the data into the new table.
The syntax to MODIFY A COLUMN in a table in SQLite is:
Let's look at an example that shows how to modify a column in a SQLite table.
For example, if we had an employees table that had a column called last_name that was defined as a CHAR datatype:
And we wanted to change the datatype of the last_name field to VARCHAR, we could do the following:
This example will rename our existing employees table to _employees_old. Then it will create the new employees table with the last_name field defined as a VARCHAR datatype. Then it will insert all of the data from the _employees_old table into the employees table.
You can not use the ALTER TABLE statement to drop a column in a table. Instead you will need to rename the table, create a new table, and copy the data into the new table.
The syntax to DROP A COLUMN in a table in SQLite is:
Let's look at an example that shows how to drop a column in a SQLite table.
For example, if we had an employees table that was defined as follows:
And we wanted to drop the column called hire_date, we could do the following:
This example will rename our existing employees table to _employees_old. Then it will create the new employees table with the hire_date field removed. Then it will insert all of the data (excluding the hire_date field) from the _employees_old table into the employees table.
You can not use the ALTER TABLE statement to rename a column in SQLite. Instead you will need to rename the table, create a new table, and copy the data into the new table.
The syntax to RENAME A COLUMN in a table in SQLite is:
Let's look at an example that shows how to rename a column in a SQLite table.
For example, if we had an employees table that was defined as follows:
And we wanted to rename the hire_date field to start_date, we could do the following:
This example will rename our existing employees table to _employees_old. Then it will create the new employees table with the hire_date field called start_date. Then it will insert all of the data from the _employees_old table into the employees table.
To rename a table, the SQLite ALTER TABLE syntax is:
The table to rename.
The new table name.
Let's look at an example that shows how to rename a table in SQLite using the ALTER TABLE statement.
For example:
This ALTER TABLE example will rename the employees table to staff.