SQLite CREATE TABLE Statement
The CREATE TABLE Statement
The CREATE TABLE statement creates a new table.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
...
);
The column names are the names of the columns, and the data types tell what kind of data each column holds. Read more in the SQLite Data Types chapter.
Example
This example creates a table named Persons, with five columns:
Example
CREATE TABLE Persons (
PersonID INTEGER,
LastName TEXT,
FirstName TEXT,
Address TEXT,
City TEXT
);
Try it Yourself »
The table is empty. You can fill it with the INSERT INTO statement.
IF NOT EXISTS
If the table already exists, CREATE TABLE gives an error. Add IF NOT EXISTS to create the table only if it does not exist:
Example
CREATE TABLE IF NOT EXISTS Persons (
PersonID INTEGER PRIMARY KEY,
LastName TEXT,
FirstName TEXT
);
Try it Yourself »
Create a Table From Another Table
CREATE TABLE ... AS SELECT creates a new table with the columns and the data from a query:
Example
CREATE TABLE ContactList AS
SELECT CustomerName, ContactName
FROM Customers;
Try it Yourself »
See How a Table Was Created
SQLite keeps the CREATE TABLE statement of every table in a special table named sqlite_master:
Tip: In the sqlite3 command line program, .schema Customers shows the same.