📄 lang_createtable.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>SQLite Query Language: CREATE TABLE</title><style type="text/css">body { margin: auto; font-family: "Verdana" "sans-serif"; padding: 8px 1%;}a { color: #45735f }a:visited { color: #734559 }.logo { position:absolute; margin:3px; }.tagline { float:right; text-align:right; font-style:italic; width:240px; margin:12px; margin-top:58px;}.toolbar { font-variant: small-caps; text-align: center; line-height: 1.6em; margin: 0; padding:1px 8px;}.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }.toolbar a:visited { color: white; }.toolbar a:hover { color: #80a796; background: white; }.content { margin: 5%; }.content dt { font-weight:bold; }.content dd { margin-bottom: 25px; margin-left:20%; }.content ul { padding:0px; padding-left: 15px; margin:0px; }/* rounded corners */.se { background: url(images/se.png) 100% 100% no-repeat #80a796}.sw { background: url(images/sw.png) 0% 100% no-repeat }.ne { background: url(images/ne.png) 100% 0% no-repeat }.nw { background: url(images/nw.png) 0% 0% no-repeat }</style><meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head><body><div><!-- container div to satisfy validator --><a href="index.html"><img class="logo" src="images/SQLite.gif" alt="SQLite Logo" border="0"></a><div><!-- IE hack to prevent disappearing logo--></div><div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div><table width=100% style="clear:both"><tr><td> <div class="se"><div class="sw"><div class="ne"><div class="nw"> <div class="toolbar"> <a href="about.html">About</a> <a href="sitemap.html">Sitemap</a> <a href="docs.html">Documentation</a> <a href="download.html">Download</a> <a href="copyright.html">License</a> <a href="news.html">News</a> <a href="http://www.sqlite.org/cvstrac/index">Developers</a> <a href="support.html">Support</a> </div></div></div></div></div></td></tr></table> <a href="lang.html"> <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 src="images/syntax/create-table-stmt.gif"></img> </blockquote><h4><a href="syntaxdiagrams.html#column-def">column-def:</a></h4><blockquote> <img src="images/syntax/column-def.gif"></img> </blockquote><h4><a href="syntaxdiagrams.html#type-name">type-name:</a></h4><blockquote> <img src="images/syntax/type-name.gif"></img> </blockquote><h4><a href="syntaxdiagrams.html#column-constraint">column-constraint:</a></h4><blockquote> <img src="images/syntax/column-constraint.gif"></img> </blockquote><h4><a href="syntaxdiagrams.html#table-constraint">table-constraint:</a></h4><blockquote> <img src="images/syntax/table-constraint.gif"></img> </blockquote><h4><a href="syntaxdiagrams.html#foreign-key-clause">foreign-key-clause:</a></h4><blockquote> <img src="images/syntax/foreign-key-clause.gif"></img> </blockquote><p>A CREATE TABLE statement is basically the keywords "CREATE TABLE"followed by the name of a new table and a parenthesized list of columndefinitions and constraints. Tables names that begin with "<b>sqlite_</b>" are reservedfor use by the engine.</p><p>Each column definition is the name of the column optionally followed by the<a href="datatype3.html">datatype</a> for that column, then one or more optional column constraints.SQLite uses <a href="datatype3.html">dynamic typing</a>; the datatype for the column does not restrict what data may be putin that column.The UNIQUE constraint causes an unique index to be created on the specifiedcolumns. All NULL values are considered different from each other and fromall other values for the purpose of determining uniqueness, hence a UNIQUEcolumn may contain multiple entries with the value of NULL.The COLLATE clause specifies what text <a href="datatype3.html#collation">collating function</a> to use when comparing text entries for the column. The built-in <a href="datatype3.html#collation">BINARY</a> collating function is used by default.<p>The DEFAULT constraint specifies a default value to use when doing an <a href="lang_insert.html">INSERT</a>.The value may be NULL, a string constant or a number.The default value may also be one of the special case-independantkeywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP. If the value isNULL, a string constant or number, it is literally inserted into the columnwhenever an INSERT statement that does not specify a value for the column isexecuted. If the value is CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, thenthe current UTC date and/or time is inserted into the columns. ForCURRENT_TIME, the format is HH:MM:SS. For CURRENT_DATE, YYYY-MM-DD. The formatfor CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS".</p><p>The PRIMARY KEY attribute normally creates a UNIQUE index onthe column or columns that are specified as the PRIMARY KEY. The onlyexception to this behavior is special <a href="lang_createtable.html#rowid">INTEGER PRIMARY KEY</a> column,described below.According to the SQL standard, PRIMARY KEY should imply NOT NULL.Unfortunately, due to a long-standing coding oversight, this is not the case in SQLite. SQLite allows NULL valuesin a PRIMARY KEY column. We could change SQLite to conform to thestandard (and we might do so in the future), but by the time theoversight was discovered, SQLite was in such wide use that we fearedbreaking legacy code if we fixed the problem. So for now we havechosen to continue allowing NULLs in PRIMARY KEY columns.Developers should be aware, however, that we may change SQLite toconform to the SQL standard in future and should design new programsaccordingly.</p><p>SQLite uses <a href="datatype3.html">dynamic typing</a> instead of static typing. Except for thespecial case of <a href="lang_createtable.html#rowid">INTEGER PRIMARY KEY</a>, SQLite will allow values of anytype to be stored in any column regardless of the declared datatype ofthat column. The declared datatype is a <a href="datatype3.html#affinity">type affinity</a> thatSQLite attempts to comply with, but the operation will proceed even ifcompliance is not possible.</p><p>If the "TEMP" or "TEMPORARY" keyword occurs in between "CREATE"and "TABLE" then the table that is created is only visiblewithin that same database connectionand is automatically deleted whenthe database connection is closed. Any indices created on a temporary tableare also temporary. Temporary tables and indices are stored in aseparate file distinct from the main database file.</p><p> If a <database-name> is specified, then the table is created in the named database. It is an error to specify both a <database-name>and the TEMP keyword, unless the <database-name> is "temp". If nodatabase name is specified, and the TEMP keyword is not present,the table is created in the main database.</p><p>The optional <a href="lang_conflict.html">conflict clause</a> following each constraintallows the specification of an alternative defaultconstraint conflict resolution algorithm for that constraint.The default is abort ABORT. Different constraints within the sametable may have different default conflict resolution algorithms.If an <a href="lang_insert.html">INSERT</a> or <a href="lang_update.html">UPDATE</a> statement specifies a different conflictresolution algorithm, then that algorithm is used in place of thedefault algorithm specified in the CREATE TABLE statement.See the section titled<a href="lang_conflict.html">ON CONFLICT</a> for additional information.</p><p>CHECK constraints are supported as of <a href="releaselog/3_3_0.html">version 3.3.0</a>. Priorto version 3.3.0, CHECK constraints were parsed but not enforced.</p><p>The number of columns in a table is limited by the<a href="limits.html#max_column">SQLITE_MAX_COLUMN</a> compile-time parameter.A single row of a table cannot store more than<a href="limits.html#max_length">SQLITE_MAX_LENGTH</a> bytes of data.Both of these limits can be lowered at runtime using the<a href="c3ref/limit.html">sqlite3_limit()</a> C/C++ interface.</p><p>The CREATE TABLE AS form defines the table to bethe result set of a query. The names of the table columns arethe names of the columns in the result.</p><p>The textof each CREATE TABLE statement is stored in the <b>sqlite_master</b>table. Every time the database is opened, all CREATE TABLE statementsare read from the <b>sqlite_master</b> table and used to regenerateSQLite's internal representation of the table layout.If the original command was a CREATE TABLE AS then then an equivalentCREATE TABLE statement is synthesized and store in <b>sqlite_master</b>in place of the original command.The text of CREATE TEMPORARY TABLE statements are stored in the<b>sqlite_temp_master</b> table.</p><p>If the optional IF NOT EXISTS clause is present and another tablewith the same name aleady exists, then this command becomes a no-op.</p><p>Tables are removed using the <a href="lang_droptable.html">DROP TABLE</a> statement. </p><a name="rowid"></a><h3>ROWIDs and the INTEGER PRIMARY KEY</h3><p>Every row of every SQLite table has a 64-bit signed integer key that is unique within the same table.This integer is usually called the "rowid". The rowid is the actual key usedin the B-Tree that implements an SQLite table. Rows are stored inrowid order. Therowid value can be accessed using one of the special names"<b>ROWID</b>", "<b>OID</b>", or "<b>_ROWID_</b>".</p><p>If a column is declared to be an INTEGER PRIMARY KEY, then that column is nota "real" database column but instead becomesan alias for the rowid. Unlike normal SQLite columns, the rowidmust be a non-NULL integer value. The rowid is not able to holdfloating point values, strings, BLOBs, or NULLs.</p><blockquote><i>An INTEGER PRIMARY KEY column is an alias for the 64-bit signed integer rowid.</i></blockquote><p>An INTEGER PRIMARY KEY column can also include thekeyword <a href="autoinc.html">AUTOINCREMENT</a>. The <a href="autoinc.html">AUTOINCREMENT</a> keyword modified the waythat B-Tree keys are automatically generated. Additional detailon automatic B-Tree key generation is available<a href="autoinc.html">separately</a>.</p><p>The special behavior of INTEGER PRIMARY KEYis only available if the type name is exactly "INTEGER" (in any mixtureof upper and lower case.) Other integer type nameslike "INT" or "BIGINT" or "SHORT INTEGER" or "UNSIGNED INTEGER"causes the primary key column to behave as an ordinary table column withinteger <a href="datatype3.html#affinity">affinity</a> and a unique index, not as an alias for the rowid.The special behavior of INTEGER PRIMARY KEY is only available if theprimary key is a single column. Multi-column primary keys do not becomealiases for the rowid.The AUTOINCREMENT keyword only works on a column that is an aliasfor the rowid.</p><p>Note that searches against a rowid are generally about twice asfast as searches against any other PRIMARY KEY or indexed value.</p><hr><small><i>This page last modified 2009/01/02 16:47:13 UTC</i></small></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -