]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/insert.sgml
Update SQL-command reference pages for schema features.
[postgresql] / doc / src / sgml / ref / insert.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/insert.sgml,v 1.17 2002/04/23 02:07:16 tgl Exp $
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-INSERT">
7  <refmeta>
8   <refentrytitle id="SQL-INSERT-TITLE">INSERT</refentrytitle>
9   <refmiscinfo>SQL - Language Statements</refmiscinfo>
10  </refmeta>
11  <refnamediv>
12   <refname>
13    INSERT
14   </refname>
15   <refpurpose>
16    create new rows in a table
17   </refpurpose>
18  </refnamediv>
19  <refsynopsisdiv>
20   <refsynopsisdivinfo>
21    <date>2000-08-08</date>
22   </refsynopsisdivinfo>
23   <synopsis>
24 INSERT INTO <replaceable class="PARAMETER">table</replaceable> [ ( <replaceable class="PARAMETER">column</replaceable> [, ...] ) ]
25     { DEFAULT VALUES | VALUES ( <replaceable class="PARAMETER">expression</replaceable> [, ...] ) | SELECT <replaceable class="PARAMETER">query</replaceable> }
26   </synopsis>
27   
28   <refsect2 id="R2-SQL-INSERT-1">
29    <title>
30     Inputs
31    </title>
32
33    <para>
34
35     <variablelist>
36      <varlistentry>
37       <term><replaceable class="PARAMETER">table</replaceable></term>
38       <listitem>
39        <para>
40         The name (optionally schema-qualified) of an existing table.
41        </para>
42       </listitem>
43      </varlistentry>
44
45      <varlistentry>
46       <term><replaceable class="PARAMETER">column</replaceable></term>
47       <listitem>
48        <para>
49         The name of a column in <replaceable class="PARAMETER">table</replaceable>.
50        </para>
51       </listitem>
52      </varlistentry>
53
54      <varlistentry>
55       <term>DEFAULT VALUES</term>
56       <listitem>
57        <para>
58         All columns will be filled by NULLs or by values specified
59         when the table was created using DEFAULT clauses.
60        </para>
61       </listitem>
62      </varlistentry>
63
64      <varlistentry>
65       <term><replaceable class="PARAMETER">expression</replaceable></term>
66       <listitem>
67        <para>
68         A valid expression or value to assign to <replaceable
69          class="PARAMETER">column</replaceable>.
70        </para>
71       </listitem>
72      </varlistentry>
73
74      <varlistentry>
75       <term><replaceable class="PARAMETER">query</replaceable></term>
76       <listitem>
77        <para>
78         A valid query. Refer to the SELECT statement for a further description
79         of valid arguments.
80        </para>
81       </listitem>
82      </varlistentry>
83     </variablelist>
84    </para>
85   </refsect2>
86   
87   <refsect2 id="R2-SQL-INSERT-2">
88    <title>
89     Outputs
90    </title>
91    <para>
92
93     <variablelist>
94      <varlistentry>
95       <term><computeroutput>
96 INSERT <replaceable>oid</replaceable> 1
97        </computeroutput></term>
98       <listitem>
99        <para>
100         Message returned if only one row was inserted.
101         <returnvalue><replaceable>oid</replaceable></returnvalue>
102         is the numeric <acronym>OID</acronym> of the inserted row.
103        </para>
104       </listitem>
105      </varlistentry>
106      <varlistentry>
107       <term><computeroutput>
108 INSERT 0 <replaceable>#</replaceable>
109        </computeroutput></term>
110       <listitem>
111        <para>
112         Message returned if more than one rows were inserted.
113         <returnvalue><replaceable>#</replaceable></returnvalue>
114         is the number of rows inserted.
115        </para>
116       </listitem>
117      </varlistentry>
118     </variablelist>
119    </para>
120   </refsect2>
121  </refsynopsisdiv>
122
123  <refsect1 id="R1-SQL-INSERT-1">
124   <title>
125    Description
126   </title>
127
128   <para>
129    <command>INSERT</command> allows one to insert new rows into a
130    table. One can insert
131    a single row at a time or several rows as a result of a query.
132    The columns in the target list may be listed in any order.
133   </para>
134
135   <para>
136    Each column not present in the target list will be inserted 
137    using a default value, either a declared DEFAULT value
138    or NULL. <productname>PostgreSQL</productname> will reject the new
139    column if a NULL is inserted into a column declared NOT NULL.
140   </para>
141
142   <para>
143    If the expression for each column
144    is not of the correct data type, automatic type coercion will be
145    attempted.
146   </para>
147
148   <para>
149    You must have insert privilege to a table in order to append
150    to it, as well as select privilege on any table specified
151    in a WHERE clause.
152   </para>
153  </refsect1>
154
155  <refsect1 id="R1-SQL-INSERT-2">
156   <title>
157    Usage
158   </title>
159   <para>
160    Insert a single row into table <literal>films</literal>:
161
162    <programlisting>
163 INSERT INTO films VALUES
164     ('UA502','Bananas',105,'1971-07-13','Comedy',INTERVAL '82 minute');
165    </programlisting>
166   </para>
167
168   <para>
169    In this second example the last column <literal>len</literal> is
170    omitted and therefore it will have the default value of NULL:
171
172    <programlisting>
173 INSERT INTO films (code, title, did, date_prod, kind)
174     VALUES ('T_601', 'Yojimbo', 106, DATE '1961-06-16', 'Drama');
175    </programlisting>
176   </para>
177
178   <para>
179    Insert a single row into table distributors; note that
180    only column <literal>name</literal> is specified, so the omitted
181    column <literal>did</literal> will be assigned its default value:
182
183    <programlisting>
184 INSERT INTO distributors (name) VALUES ('British Lion');
185    </programlisting>
186   </para>
187
188   <para>
189    Insert several rows into table films from table <literal>tmp</literal>:
190
191    <programlisting>
192 INSERT INTO films SELECT * FROM tmp;
193    </programlisting>
194   </para>
195
196   <para>
197    Insert into arrays (refer to the
198    <citetitle>PostgreSQL User's Guide</citetitle> for further
199    information about arrays):
200                 
201    <programlisting>
202 -- Create an empty 3x3 gameboard for noughts-and-crosses
203 -- (all of these queries create the same board attribute)
204 INSERT INTO tictactoe (game, board[1:3][1:3])
205     VALUES (1,'{{"","",""},{},{"",""}}');
206 INSERT INTO tictactoe (game, board[3][3])
207     VALUES (2,'{}');
208 INSERT INTO tictactoe (game, board)
209     VALUES (3,'{{,,},{,,},{,,}}');
210    </programlisting>
211   </para>
212  </refsect1>
213
214  <refsect1 id="R1-SQL-INSERT-3">
215   <title>
216    Compatibility
217   </title>
218         
219   <refsect2 id="R2-SQL-INSERT-4">
220    <title>
221     SQL92
222    </title>
223    <para>
224     <command>INSERT</command> is fully compatible with <acronym>SQL92</acronym>.
225     Possible limitations in features of the 
226     <replaceable class="PARAMETER">query</replaceable>
227     clause are documented for
228     <xref linkend="sql-select" endterm="sql-select-title">.
229    </para>
230   </refsect2>
231  </refsect1>
232 </refentry>
233
234 <!-- Keep this comment at the end of the file
235 Local variables:
236 mode: sgml
237 sgml-omittag:nil
238 sgml-shorttag:t
239 sgml-minimize-attributes:nil
240 sgml-always-quote-attributes:t
241 sgml-indent-step:1
242 sgml-indent-data:t
243 sgml-parent-document:nil
244 sgml-default-dtd-file:"../reference.ced"
245 sgml-exposed-tags:nil
246 sgml-local-catalogs:"/usr/lib/sgml/catalog"
247 sgml-local-ecat-files:nil
248 End:
249 -->