]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/insert.sgml
Add more appropriate markup.
[postgresql] / doc / src / sgml / ref / insert.sgml
1 <!--
2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/insert.sgml,v 1.19 2002/09/21 18:32:54 petere 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> | DEFAULT } [, ...] ) | 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 null values or by values specified
59         when the table was created using <literal>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">DEFAULT</replaceable></term>
76       <listitem>
77        <para>
78         This column will be filled in by the column DEFAULT clause, or NULL if
79         a default is not available.
80        </para>
81       </listitem>
82      </varlistentry>
83
84      <varlistentry>
85       <term><replaceable class="PARAMETER">query</replaceable></term>
86       <listitem>
87        <para>
88         A valid query. Refer to the SELECT statement for a further description
89         of valid arguments.
90        </para>
91       </listitem>
92      </varlistentry>
93     </variablelist>
94    </para>
95   </refsect2>
96
97   <refsect2 id="R2-SQL-INSERT-2">
98    <title>
99     Outputs
100    </title>
101    <para>
102
103     <variablelist>
104      <varlistentry>
105       <term><computeroutput>
106 INSERT <replaceable>oid</replaceable> 1
107        </computeroutput></term>
108       <listitem>
109        <para>
110         Message returned if only one row was inserted.
111         <returnvalue><replaceable>oid</replaceable></returnvalue>
112         is the numeric <acronym>OID</acronym> of the inserted row.
113        </para>
114       </listitem>
115      </varlistentry>
116      <varlistentry>
117       <term><computeroutput>
118 INSERT 0 <replaceable>#</replaceable>
119        </computeroutput></term>
120       <listitem>
121        <para>
122         Message returned if more than one rows were inserted.
123         <returnvalue><replaceable>#</replaceable></returnvalue>
124         is the number of rows inserted.
125        </para>
126       </listitem>
127      </varlistentry>
128     </variablelist>
129    </para>
130   </refsect2>
131  </refsynopsisdiv>
132
133  <refsect1 id="R1-SQL-INSERT-1">
134   <title>
135    Description
136   </title>
137
138   <para>
139    <command>INSERT</command> allows one to insert new rows into a
140    table. One can insert
141    a single row at a time or several rows as a result of a query.
142    The columns in the target list may be listed in any order.
143   </para>
144
145   <para>
146    Each column not present in the target list will be inserted
147    using a default value, either a declared DEFAULT value
148    or NULL. <productname>PostgreSQL</productname> will reject the new
149    column if a NULL is inserted into a column declared NOT NULL.
150   </para>
151
152   <para>
153    If the expression for each column
154    is not of the correct data type, automatic type coercion will be
155    attempted.
156   </para>
157
158   <para>
159    You must have insert privilege to a table in order to append
160    to it, as well as select privilege on any table specified
161    in a WHERE clause.
162   </para>
163  </refsect1>
164
165  <refsect1 id="R1-SQL-INSERT-2">
166   <title>
167    Usage
168   </title>
169   <para>
170    Insert a single row into table <literal>films</literal>:
171
172    <programlisting>
173 INSERT INTO films VALUES
174     ('UA502','Bananas',105,'1971-07-13','Comedy',INTERVAL '82 minute');
175    </programlisting>
176   </para>
177
178   <para>
179    In this second example the last column <literal>len</literal> is
180    omitted and therefore it will have the default value of NULL:
181
182    <programlisting>
183 INSERT INTO films (code, title, did, date_prod, kind)
184     VALUES ('T_601', 'Yojimbo', 106, DATE '1961-06-16', 'Drama');
185    </programlisting>
186   </para>
187
188   <para>
189    In the third example, we use the DEFAULT values for the date columns
190    rather than specifying an entry.
191
192    <programlisting>
193 INSERT INTO films VALUES
194     ('UA502','Bananas',105,DEFAULT,'Comedy',INTERVAL '82 minute');
195 INSERT INTO films (code, title, did, date_prod, kind)
196     VALUES ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama');
197    </programlisting>
198   </para>
199
200   <para>
201    Insert a single row into table distributors; note that
202    only column <literal>name</literal> is specified, so the omitted
203    column <literal>did</literal> will be assigned its default value:
204
205    <programlisting>
206 INSERT INTO distributors (name) VALUES ('British Lion');
207    </programlisting>
208   </para>
209
210   <para>
211    Insert several rows into table films from table <literal>tmp</literal>:
212
213    <programlisting>
214 INSERT INTO films SELECT * FROM tmp;
215    </programlisting>
216   </para>
217
218   <para>
219    Insert into arrays (refer to the
220    <citetitle>PostgreSQL User's Guide</citetitle> for further
221    information about arrays):
222
223    <programlisting>
224 -- Create an empty 3x3 gameboard for noughts-and-crosses
225 -- (all of these queries create the same board attribute)
226 INSERT INTO tictactoe (game, board[1:3][1:3])
227     VALUES (1,'{{"","",""},{},{"",""}}');
228 INSERT INTO tictactoe (game, board[3][3])
229     VALUES (2,'{}');
230 INSERT INTO tictactoe (game, board)
231     VALUES (3,'{{,,},{,,},{,,}}');
232    </programlisting>
233   </para>
234  </refsect1>
235
236  <refsect1 id="R1-SQL-INSERT-3">
237   <title>
238    Compatibility
239   </title>
240
241   <refsect2 id="R2-SQL-INSERT-4">
242    <title>
243     SQL92
244    </title>
245    <para>
246     <command>INSERT</command> is fully compatible with <acronym>SQL92</acronym>.
247     Possible limitations in features of the
248     <replaceable class="PARAMETER">query</replaceable>
249     clause are documented for
250     <xref linkend="sql-select" endterm="sql-select-title">.
251    </para>
252   </refsect2>
253  </refsect1>
254 </refentry>
255
256 <!-- Keep this comment at the end of the file
257 Local variables:
258 mode: sgml
259 sgml-omittag:nil
260 sgml-shorttag:t
261 sgml-minimize-attributes:nil
262 sgml-always-quote-attributes:t
263 sgml-indent-step:1
264 sgml-indent-data:t
265 sgml-parent-document:nil
266 sgml-default-dtd-file:"../reference.ced"
267 sgml-exposed-tags:nil
268 sgml-local-catalogs:"/usr/lib/sgml/catalog"
269 sgml-local-ecat-files:nil
270 End:
271 -->