{"id":8141,"date":"2020-04-08T22:03:32","date_gmt":"2020-04-08T12:03:32","guid":{"rendered":"https:\/\/database.guide\/?p=8141"},"modified":"2025-11-15T11:56:27","modified_gmt":"2025-11-15T01:56:27","slug":"create-table-sqlite","status":"publish","type":"post","link":"https:\/\/database.guide\/create-table-sqlite\/","title":{"rendered":"Create a Table in SQLite"},"content":{"rendered":"\n<p class=\"\">To create a <a href=\"https:\/\/database.guide\/what-is-a-table\/\" class=\"aioseop-link\">table<\/a> in <a href=\"https:\/\/database.guide\/what-is-sqlite\/\">SQLite<\/a>, use the <code>CREATE TABLE<\/code> statement.<\/p>\n\n\n\n<p class=\"\">This statement accepts the table name, the <a href=\"https:\/\/database.guide\/what-is-a-column\/\" class=\"aioseop-link\">column<\/a> names and their definitions, as well as some other options.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\"> Example<\/h2>\n\n\n\n<p class=\"\">Here&#8217;s a basic example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE Products(\n  ProductId INTEGER PRIMARY KEY AUTOINCREMENT, \n  ProductName TEXT NOT NULL,\n  Price NUMERIC\n);<\/code><\/pre>\n\n\n\n<p class=\"\">So in this case, <code>Products<\/code> is the name of the table, and it contains three columns; <code>ProductId<\/code>, <code>ProductName<\/code>, and <code>Price<\/code>.  <\/p>\n\n\n\n<p class=\"\">In this example, I have added each column&#8217;s data type as well as some constraints, but these are all optional.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\">Specify the Schema<\/h2>\n\n\n\n<p class=\"\">The table&#8217;s name can (optionally) be prefixed with the schema name. When doing this, the schema name must be either <code>main<\/code>, <code>temp<\/code>, or the name of an attached database.<\/p>\n\n\n\n<p class=\"\">So I could do this instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE Store.Products(\n  ProductId, \n  ProductName,\n  Price\n);<\/code><\/pre>\n\n\n\n<p class=\"\">In this case, <code>Store<\/code> is the name of the attached database that I want to create the table in. <\/p>\n\n\n\n<p class=\"\">The column name can be followed by the data type and any constraints. <\/p>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Data Type is Optional<\/h2>\n\n\n\n<p class=\"\">Yes, you read that right &#8211; the data type is actually optional. <\/p>\n\n\n\n<p class=\"\">SQLite uses dynamic typing and so the data type&nbsp;of a value is associated with the value itself, not with its container (column). This is in contrast to most other <a href=\"https:\/\/database.guide\/what-is-sql\/\" class=\"aioseop-link\">SQL<\/a> database systems, where you must specify the data type when you create the column.<\/p>\n\n\n\n<p class=\"\">So I could do this instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE Products(\n  ProductId, \n  ProductName,\n  Price\n);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Constraints &amp; Other Options<\/h2>\n\n\n\n<p class=\"\">You have the option of specifying any constraints or other options you&#8217;d like to be applied against each column. These include the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><code>DEFAULT<\/code> clause. This specifies a default value or expression for each column in the table. <\/li>\n\n\n\n<li class=\"\">The <code>COLLATE<\/code> clause to specify the name of a collating sequence to use as the default collation sequence for the column. The default value is BINARY.<\/li>\n\n\n\n<li class=\"\"><code>PRIMARY KEY<\/code> clause. You can optionally specify that a column is a <a href=\"https:\/\/database.guide\/what-is-a-primary-key\/\">primary key<\/a>. Both single column and composite (multiple column) primary keys are supported in SQLite.<\/li>\n\n\n\n<li class=\"\">SQLite also supports UNIQUE, NOT NULL, CHECK, and <a href=\"https:\/\/database.guide\/what-is-a-foreign-key\/\">FOREIGN KEY<\/a> constraints.<\/li>\n\n\n\n<li class=\"\">A generated column constraint (also called a computed column). These are columns whose values are a function of other columns in the same <a class=\"aioseop-link\" href=\"https:\/\/database.guide\/what-is-a-row\/\">row<\/a>.<\/li>\n\n\n\n<li class=\"\">Whether the table is a <code>WITHOUT ROWID<\/code> table. This is a performance optimisation technique that omits the &#8220;rowid&#8221; column that is a special column that SQLite uses by default. For more information on this technique, see the <a aria-label=\"SQLite documentation (opens in a new tab)\" class=\"aioseop-link\" href=\"https:\/\/www.sqlite.org\/withoutrowid.html\" target=\"_blank\" rel=\"noreferrer noopener\">SQLite documentation<\/a>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Temporary Tables<\/h2>\n\n\n\n<p class=\"\">You can specify that a table is a <a href=\"https:\/\/database.guide\/what-is-a-temp-table\/\" data-type=\"post\" data-id=\"46127\">temporary table<\/a> by using either the <code>TEMP<\/code> or <code>TEMPORARY<\/code> keyword.<\/p>\n\n\n\n<p class=\"\">If using one of these keywords, they must be inserted between the <code>CREATE<\/code> and <code>TABLE<\/code>. <\/p>\n\n\n\n<p class=\"\">Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TEMP TABLE Products(\n  ProductId, \n  ProductName,\n  Price\n);<\/code><\/pre>\n\n\n\n<p class=\"\">You can also add the <code>temp<\/code> schema if you wish. <\/p>\n\n\n\n<p class=\"\">See <a href=\"https:\/\/database.guide\/create-temporary-table-sqlite\/\" class=\"aioseop-link\">How to Create a Temporary Table<\/a> for more examples of creating temporary tables in SQLite.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a Table from Another Table<\/h2>\n\n\n\n<p class=\"\">You can also use a <code>CREATE TABLE ... AS SELECT<\/code> statement to create a new table based on another table. When you do this, the new table is populated with the data from the <code>SELECT<\/code> statement (which selects data from another table or tables).<\/p>\n\n\n\n<p class=\"\">Here&#8217;s a basic example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE Store.Products2 AS \nSELECT * FROM Store.Products;<\/code><\/pre>\n\n\n\n<p class=\"\">This example creates a new table called <code>Products2<\/code> and populates it with all data from the <code>Products<\/code> table. <\/p>\n\n\n\n<p class=\"\">All column names are the same as in the original table.<\/p>\n\n\n\n<p class=\"\">It&#8217;s important to note that tables created in this manner have no PRIMARY KEY and no constraints of any kind. Also, the default value of each column is <code>NULL<\/code>. Also, the default collation sequence for each column of the new table is BINARY.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To create a table in SQLite, use the CREATE TABLE statement. This statement accepts the table name, the column names and their definitions, as well as some other options.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,100],"tags":[9],"class_list":["post-8141","post","type-post","status-publish","format-standard","hentry","category-sql","category-sqlite","tag-create-table"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/8141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=8141"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/8141\/revisions"}],"predecessor-version":[{"id":46209,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/8141\/revisions\/46209"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=8141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=8141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=8141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}