lang_createtable.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head>
  3. <title>SQLite Query Language: CREATE TABLE</title>
  4. <style type="text/css">
  5. body {
  6. margin: auto;
  7. font-family: Verdana, sans-serif;
  8. padding: 8px 1%;
  9. }
  10. a { color: #45735f }
  11. a:visited { color: #734559 }
  12. .logo { position:absolute; margin:3px; }
  13. .tagline {
  14. float:right;
  15. text-align:right;
  16. font-style:italic;
  17. width:240px;
  18. margin:12px;
  19. margin-top:58px;
  20. }
  21. .toolbar {
  22. font-variant: small-caps;
  23. text-align: center;
  24. line-height: 1.6em;
  25. margin: 0;
  26. padding:1px 8px;
  27. }
  28. .toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
  29. .toolbar a:visited { color: white; }
  30. .toolbar a:hover { color: #80a796; background: white; }
  31. .content { margin: 5%; }
  32. .content dt { font-weight:bold; }
  33. .content dd { margin-bottom: 25px; margin-left:20%; }
  34. .content ul { padding:0px; padding-left: 15px; margin:0px; }
  35. /* rounded corners */
  36. .se { background: url(images/se.png) 100% 100% no-repeat #80a796}
  37. .sw { background: url(images/sw.png) 0% 100% no-repeat }
  38. .ne { background: url(images/ne.png) 100% 0% no-repeat }
  39. .nw { background: url(images/nw.png) 0% 0% no-repeat }
  40. </style>
  41. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  42. </head>
  43. <body>
  44. <div><!-- container div to satisfy validator -->
  45. <a href="lang.html">
  46. <h2 align="center">SQL As Understood By SQLite</h2></a><h1>CREATE TABLE</h1><h4><a href="syntaxdiagrams.html#create-table-stmt">create-table-stmt:</a></h4><blockquote> <img alt="syntax diagram create-table-stmt" src="images/syntax/create-table-stmt.gif"></img> </blockquote>
  47. <h4><a href="syntaxdiagrams.html#column-def">column-def:</a></h4><blockquote> <img alt="syntax diagram column-def" src="images/syntax/column-def.gif"></img> </blockquote>
  48. <h4><a href="syntaxdiagrams.html#type-name">type-name:</a></h4><blockquote> <img alt="syntax diagram type-name" src="images/syntax/type-name.gif"></img> </blockquote>
  49. <h4><a href="syntaxdiagrams.html#column-constraint">column-constraint:</a></h4><blockquote> <img alt="syntax diagram column-constraint" src="images/syntax/column-constraint.gif"></img> </blockquote>
  50. <h4><a href="syntaxdiagrams.html#table-constraint">table-constraint:</a></h4><blockquote> <img alt="syntax diagram table-constraint" src="images/syntax/table-constraint.gif"></img> </blockquote>
  51. <h4><a href="syntaxdiagrams.html#foreign-key-clause">foreign-key-clause:</a></h4><blockquote> <img alt="syntax diagram foreign-key-clause" src="images/syntax/foreign-key-clause.gif"></img> </blockquote>
  52. <p>A CREATE TABLE statement is basically the keywords "CREATE TABLE"
  53. followed by the name of a new table and a parenthesized list of column
  54. definitions and constraints.
  55. Tables names that begin with "<b>sqlite_</b>" are reserved
  56. for use by SQLite itself and cannot appears in a CREATE TABLE statement.</p>
  57. <p>Each column definition is the name of the column optionally followed by the
  58. datatype for that column, then one or more optional column constraints.
  59. SQLite uses dynamic typing;
  60. the datatype for the column does not restrict what data may be put
  61. in that column.
  62. The UNIQUE constraint causes an unique index to be created on the specified
  63. columns. All NULL values are considered different from each other and from
  64. all other values for the purpose of determining uniqueness, hence a UNIQUE
  65. column may contain multiple entries with the value of NULL.
  66. The COLLATE clause specifies what text
  67. collating function to use when comparing text entries for the column.
  68. The built-in BINARY collating function is used by default.
  69. <p>
  70. The DEFAULT constraint specifies a default value to use when doing an <a href="lang_insert.html">INSERT</a>.
  71. The value may be NULL, a string constant, a number, or a constant expression
  72. enclosed in parentheses.
  73. The default value may also be one of the special case-independant
  74. keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP. If the value is
  75. NULL, a string constant or number, it is literally inserted into the column
  76. whenever an INSERT statement that does not specify a value for the column is
  77. executed. If the value is CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then
  78. the current UTC date and/or time is inserted into the columns. For
  79. CURRENT_TIME, the format is HH:MM:SS. For CURRENT_DATE, YYYY-MM-DD. The format
  80. for CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS".
  81. </p>
  82. <p>The PRIMARY KEY attribute normally creates a UNIQUE index on
  83. the column or columns that are specified as the PRIMARY KEY. The only
  84. exception to this behavior is special <a href="lang_createtable.html#rowid">INTEGER PRIMARY KEY</a> column,
  85. described below.
  86. According to the SQL standard, PRIMARY KEY should imply NOT NULL.
  87. Unfortunately, due to a long-standing coding oversight, this is not
  88. the case in SQLite. SQLite allows NULL values
  89. in a PRIMARY KEY column. We could change SQLite to conform to the
  90. standard (and we might do so in the future), but by the time the
  91. oversight was discovered, SQLite was in such wide use that we feared
  92. breaking legacy code if we fixed the problem. So for now we have
  93. chosen to continue allowing NULLs in PRIMARY KEY columns.
  94. Developers should be aware, however, that we may change SQLite to
  95. conform to the SQL standard in future and should design new programs
  96. accordingly.</p>
  97. <p>SQLite uses dynamic typing instead of static typing. Except for the
  98. special case of <a href="lang_createtable.html#rowid">INTEGER PRIMARY KEY</a>, SQLite will allow values of any
  99. type to be stored in any column regardless of the declared datatype of
  100. that column. The declared datatype is a type affinity that
  101. SQLite attempts to comply with, but the operation will proceed even if
  102. compliance is not possible.</p>
  103. <p>If the "TEMP" or "TEMPORARY" keyword occurs in between "CREATE"
  104. and "TABLE" then the table that is created is only visible
  105. within that same database connection
  106. and is automatically deleted when
  107. the database connection is closed. Any indices created on a temporary table
  108. are also temporary. Temporary tables and indices are stored in a
  109. separate file distinct from the main database file.</p>
  110. <p> If a &lt;database-name&gt; is specified, then the table is created in
  111. the named database. It is an error to specify both a &lt;database-name&gt;
  112. and the TEMP keyword, unless the &lt;database-name&gt; is "temp". If no
  113. database name is specified, and the TEMP keyword is not present,
  114. the table is created in the main database.</p>
  115. <p>The optional <a href="lang_conflict.html">conflict clause</a> following each constraint
  116. allows the specification of an alternative default
  117. constraint conflict resolution algorithm for that constraint.
  118. If no conflict clause is specified, the ABORT algorithm is used.
  119. Different constraints within the same
  120. table may have different conflict resolution algorithms.
  121. If an <a href="lang_insert.html">INSERT</a> or <a href="lang_update.html">UPDATE</a> statement specifies a conflict
  122. resolution algorithm, then the algorithm specified on the INSERT or
  123. UPDATE statement overrides the algorithm specified in the
  124. CREATE TABLE statement.
  125. See the section titled
  126. <a href="lang_conflict.html">ON CONFLICT</a> for additional information.</p>
  127. <p>CHECK constraints are supported as of version 3.3.0. Prior
  128. to version 3.3.0, CHECK constraints were parsed but not enforced.</p>
  129. <p>The number of columns in a table is limited by the
  130. SQLITE_MAX_COLUMN compile-time parameter.
  131. A single row of a table cannot store more than
  132. SQLITE_MAX_LENGTH bytes of data.
  133. Both of these limits can be lowered at runtime using the
  134. sqlite3_limit() C/C++ interface.</p>
  135. <p>The CREATE TABLE AS form defines the table to be
  136. the result set of a query. The names of the table columns are
  137. the names of the columns in the result.</p>
  138. <p>If the optional IF NOT EXISTS clause is present and another table
  139. with the same name aleady exists, then this command becomes a no-op.</p>
  140. <p>Tables are removed using the <a href="lang_droptable.html">DROP TABLE</a>
  141. statement. </p>
  142. <a name="rowid"></a>
  143. <h3>ROWIDs and the INTEGER PRIMARY KEY</h3>
  144. <p>Every row of every SQLite table has a 64-bit signed integer key
  145. that is unique within the same table.
  146. This integer is usually called the "rowid". The rowid is the actual key used
  147. in the B-Tree that implements an SQLite table. Rows are stored in
  148. rowid order. The
  149. rowid value can be accessed using one of the special names
  150. "<b>ROWID</b>", "<b>OID</b>", or "<b>_ROWID_</b>".</p>
  151. <p>
  152. If a column is declared to be an INTEGER PRIMARY KEY, then that column is not
  153. a "real" database column but instead becomes
  154. an alias for the rowid. Unlike normal SQLite columns, the rowid
  155. must be a non-NULL integer value. The rowid is not able to hold
  156. floating point values, strings, BLOBs, or NULLs.</p>
  157. <blockquote><i>
  158. An INTEGER PRIMARY KEY column is an alias for the 64-bit signed integer rowid.
  159. </i></blockquote>
  160. <p>An INTEGER PRIMARY KEY column can also include the
  161. keyword AUTOINCREMENT. The AUTOINCREMENT keyword modified the way
  162. that B-Tree keys are automatically generated. Additional detail
  163. on automatic B-Tree key generation is available
  164. separately.</p>
  165. <p>The special behavior of INTEGER PRIMARY KEY
  166. is only available if the type name is exactly "INTEGER" (in any mixture
  167. of upper and lower case.) Other integer type names
  168. like "INT" or "BIGINT" or "SHORT INTEGER" or "UNSIGNED INTEGER"
  169. causes the primary key column to behave as an ordinary table column with
  170. integer affinity and a unique index, not as an alias for the rowid.
  171. The special behavior of INTEGER PRIMARY KEY is only available if the
  172. primary key is a single column. Multi-column primary keys do not become
  173. aliases for the rowid.
  174. The AUTOINCREMENT keyword only works on a column that is an alias
  175. for the rowid.</p>
  176. <p>Note that searches against a rowid are generally about twice as
  177. fast as searches against any other PRIMARY KEY or indexed value.
  178. </p>
  179. <p><b>Goofy behavior alert:</b> The following three declarations all cause
  180. the column "x" be an alias for the rowid:</p>
  181. <ul>
  182. <li><tt>CREATE TABLE t(x INTEGER PRIMARY KEY ASC, y, z);</tt>
  183. <li><tt>CREATE TABLE t(x INTEGER, y, z, PRIMARY KEY(x ASC));</tt>
  184. <li><tt>CREATE TABLE t(x INTEGER, y, z, PRIMARY KEY(x DESC));</tt>
  185. </ul>
  186. <p>But, in contrast, the following declaration does <u>not</u> result in
  187. "x" being an alias for the rowid:</p>
  188. <ul>
  189. <li><tt>CREATE TABLE t(x INTEGER PRIMARY KEY DESC, y, z);</tt>
  190. </ul>
  191. <p>This asymmetrical behavior is unfortunate and is really due to a bug
  192. in the parser in early versions of SQLite. But fixing the bug would
  193. result in very serious backwards incompatibilities. The SQLite developers
  194. feel that goofy behavior in an corner case is far better than
  195. a compatibility break, so the original behavior is retained.</p>