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