SQLite Create, Alter, Drop Table with Examples

⚡ Smart Summary

SQLite table management relies on three data definition commands, CREATE TABLE to define a table, ALTER TABLE to rename it or add and change columns, and DROP TABLE to remove it completely from the database.

  • 📐 Create tables: The CREATE TABLE statement defines a table name and its columns, and each column needs a name and a data type.
  • 🛡️ Avoid errors: Adding IF NOT EXISTS to CREATE TABLE, or IF EXISTS to DROP TABLE, prevents failures when a table already exists or is missing.
  • ✏️ Alter tables: ALTER TABLE renames a table, adds a column, and in modern SQLite renames or drops a single column.
  • 🗑️ Drop tables: The DROP TABLE command permanently removes a table and every row and index that belongs to it.
  • 📑 Copy tables: CREATE TABLE AS SELECT builds a new table from the columns and rows returned by a query.
  • 🤖 AI assistance: AI text-to-SQL assistants and GitHub Copilot generate CREATE, ALTER, and DROP statements from plain English prompts.

SQLite Create Alter Drop Table

The sections below show how to create tables, modify their structure, and drop tables in SQLite3 with examples, covering the CREATE TABLE, ALTER TABLE, and DROP TABLE commands.

SQLite Create table

Syntax

Below is the syntax of CREATE TABLE statement.

CREATE TABLE table_name(
column1 datatype,
column1 datatype
);

To create a table, you should use the “CREATE TABLE” Query as follows:

CREATE TABLE guru99 (
  Id Int,
  Name Varchar
);

Within the two brackets after the table name, you define the tables’ columns, each column should have the following properties:

  • A name, the column name it should be unique among the table’s columns.
  • A data type – the column data type.
  • Optional column constraints as we will explain in the later sections in this tutorial.

SQLite CREATE TABLE IF NOT EXISTS

When a script runs more than once, a plain CREATE TABLE statement fails if the table already exists and stops with an error. SQLite solves this with the optional IF NOT EXISTS clause, which creates the table only when it is missing and does nothing when it is already there.

CREATE TABLE IF NOT EXISTS guru99 (
  Id Int,
  Name Varchar
);

The statement above never raises the “table already exists” error, which makes it safe to keep at the top of a setup or migration script.

The same idea works in reverse when you remove a table. Adding IF EXISTS to DROP TABLE avoids an error when the table is already gone:

DROP TABLE IF EXISTS guru99;

Both clauses are common in automated scripts, because they let the same file run repeatedly without manual clean-up between runs.

Drop table

To drop a table, use the “DROP TABLE” command followed by the table name as follows:

DROP TABLE guru99;

Alter table

You can use “ALTER TABLE” command to rename a table as follows:

ALTER TABLE guru99 RENAME TO guru100;

To verify that the table’s name is changed, you can use the command “.tables” to show the list of tables, and the table name should be changed now as following:

SQLite .tables output showing the table renamed from guru99 to guru100

As you can see the table name “guru99” is changed to “guru100” after the “alter table” command.

SQLite add columns- Using ALTER TABLE Command

You can also use the “ALTER TABLE” command to add columns:

ALTER TABLE guru100 ADD COLUMN Age INT;

This will alter the table “guru100” and add a new column Age to it.

If you didn’t see any output, this means that the statement was successful, and the column was added. No output means that the cursor will be positioned after “sqlite>” with no text after it like this:

SQLite command line showing no output after a successful ALTER TABLE ADD COLUMN

However, to verify that the column was added, we can use the command “.schema guru100”. This will give you the table definition, and you should see the new column we have just added:

SQLite .schema guru100 output showing the new Age column added to the table

SQLite ALTER TABLE: Rename and Drop a Column

Beyond adding a column, modern versions of SQLite can also rename a single column and remove one. Renaming a column has been available since SQLite 3.25, and it updates the name inside the table definition as well as in every index, trigger, and view that references the column.

ALTER TABLE guru100 RENAME COLUMN Name TO FullName;

To remove a column, SQLite 3.35 and later support a direct DROP COLUMN clause:

ALTER TABLE guru100 DROP COLUMN Age;

A column cannot be dropped when it is part of a primary key, a unique constraint, or an index. On older SQLite versions that lack DROP COLUMN, the classic workaround is to create a new table with the columns you want, copy the rows across, drop the old table, and finally rename the new one.

How to Copy a Table in SQLite Using CREATE TABLE AS SELECT

Sometimes you need a copy of a table, either as a backup before a risky change or as the starting point for a new table. SQLite builds this in one step with CREATE TABLE AS SELECT, which creates a new table whose columns come from a query and fills it with the rows that query returns.

CREATE TABLE guru100_backup AS SELECT * FROM guru100;

The new table “guru100_backup” receives the same columns and every row from “guru100”. To copy only some rows, add a WHERE clause to the SELECT:

CREATE TABLE known_ages AS SELECT * FROM guru100 WHERE Age IS NOT NULL;

One point to remember is that CREATE TABLE AS SELECT copies the column names, types, and data, but it does not copy primary keys, other constraints, or indexes. You need to add those to the new table yourself if the copy must enforce the same rules as the original.

SQLite Insert value into a table

To insert values into a table, we use the “INSERT INTO” statement as follow:

INSERT INTO Tablename(colname1, colname2, โ€ฆ.) VALUES(valu1, value2, โ€ฆ.);

You can omit the columns names after the table name and write it as follows:

INSERT INTO Tablename VALUES(value1, value2, โ€ฆ.);

In such case, where you are omitting the columns names from the tables, the number of inserted values must be the same exact number of the table’s columns. Then each value will be inserted in the correspondence column. For example, for the following insert statement:

INSERT INTO guru100 VALUES(1, 'Mike', 25);

The result of this statement will be as following:

SQLite INSERT INTO statement adding a row of values into the guru100 table

  • The value 1 will be inserted in the column “id“.
  • The value ‘Mike’ will be inserted in the column “Name“, and
  • The value 25 will be inserted in the column “Age“.

INSERT โ€ฆ DEFAULT VALUES statement

You can populate the table with the default values for the columns at once as follows:

INSERT INTO Tablename DEFAULT VALUES;

If a column doesn’t allow a null value nor a default value, you will get an error that “NOT NULL constraint failed” for that column. As following:

SQLite NOT NULL constraint failed error from INSERT DEFAULT VALUES

FAQs

No. SQLite has no TRUNCATE TABLE command. To empty a table, run DELETE FROM table_name without a WHERE clause, then use VACUUM to reclaim the freed disk space. The table structure and its columns stay in place.

DELETE removes rows from a table but keeps the table, its columns, and its structure ready for new data. DROP TABLE removes the entire table, including its definition, indexes, and triggers, so the table no longer exists in the database.

At the SQLite command line, the .tables dot command prints every table name. From SQL, query the built-in schema table with SELECT name FROM sqlite_master WHERE type = ‘table’; which also works from any application connected to the database.

A temporary table is created with CREATE TEMP TABLE or CREATE TEMPORARY TABLE. It is visible only to the database connection that made it and is dropped automatically when that connection closes, which makes it useful for holding intermediate query results.

Yes. A primary key is optional in SQLite. When you omit one, SQLite still keeps a hidden 64-bit rowid that identifies each row. Column constraints such as PRIMARY KEY, UNIQUE, and NOT NULL are all optional when you define a table.

By default no. SQLite uses dynamic typing with type affinity, so a column accepts values of almost any type regardless of its declared type. To enforce strict checking, create the table as a STRICT table, added in SQLite 3.37.

Yes. AI text-to-SQL assistants turn a plain-English description of your data into CREATE TABLE statements with columns, data types, and keys. Accuracy improves when you give the model your existing schema, since it then reuses real table and column names.

Yes. GitHub Copilot suggests ALTER TABLE, DROP TABLE, and other SQLite statements inline in editors such as VS Code. It reads your migration files and existing queries, so its completions match your real table and column names.

Summarize this post with: