SQLite - Create a Table
To create a table in SQLite, use the CREATE TABLE statement.
Running this statement creates the table in the current database. When you create a table, you also specify the columns that the table will contain, as well as their data types.
The following code creates a table called Artists:
CREATE TABLE Artists(
ArtistId INTEGER PRIMARY KEY,
ArtistName TEXT NOT NULL
);
This creates a table with two columns (called ArtistId and ArtistName).
The ArtistId column has a data type of INTEGER and the ArtistName column has a data type of TEXT. By using NOT NULL we are specifying that the column must contain a value for each record.
We have also specified that the ArtistId is the primary key. This will be the unique identifier column for the table.
Verify that the Table was Created
You can check whether the table was created by using the .tables command.
Running that command results in the following:
sqlite> .tablesArtists
View Table Details
You can also use the .schema command to view the details of the table.
To use this command, simply .schema followed by the table name. Like this:
.schema Artists
And this should result in the following:
sqlite> .schema ArtistsCREATE TABLE Artists(ArtistId INTEGER PRIMARY KEY, ArtistName TEXT NOT NULL);
Now that we've created a table, we can insert data.