insert.sgml 7.0 KB
Newer Older
1
<!--
2
$Header: /cvsroot/pgsql/doc/src/sgml/ref/insert.sgml,v 1.18 2002/04/24 02:31:30 momjian Exp $
3
PostgreSQL documentation
4 5
-->

6 7
<refentry id="SQL-INSERT">
 <refmeta>
8
  <refentrytitle id="SQL-INSERT-TITLE">INSERT</refentrytitle>
9 10 11 12 13 14 15
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
 <refnamediv>
  <refname>
   INSERT
  </refname>
  <refpurpose>
16
   create new rows in a table
17
  </refpurpose>
18
 </refnamediv>
19 20
 <refsynopsisdiv>
  <refsynopsisdivinfo>
21
   <date>2000-08-08</date>
22 23 24
  </refsynopsisdivinfo>
  <synopsis>
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 29
  <refsect2 id="R2-SQL-INSERT-1">
   <title>
30
    Inputs
31
   </title>
32

33 34 35 36 37 38 39
   <para>

    <variablelist>
     <varlistentry>
      <term><replaceable class="PARAMETER">table</replaceable></term>
      <listitem>
       <para>
40
	The name (optionally schema-qualified) of an existing table.
41 42 43
       </para>
      </listitem>
     </varlistentry>
44

45 46 47 48 49 50 51 52 53
     <varlistentry>
      <term><replaceable class="PARAMETER">column</replaceable></term>
      <listitem>
       <para>
	The name of a column in <replaceable class="PARAMETER">table</replaceable>.
       </para>
      </listitem>
     </varlistentry>

54 55 56 57 58 59 60 61 62 63
     <varlistentry>
      <term>DEFAULT VALUES</term>
      <listitem>
       <para>
	All columns will be filled by NULLs or by values specified
	when the table was created using DEFAULT clauses.
       </para>
      </listitem>
     </varlistentry>

64 65 66 67 68 69 70 71 72 73
     <varlistentry>
      <term><replaceable class="PARAMETER">expression</replaceable></term>
      <listitem>
       <para>
	A valid expression or value to assign to <replaceable
	 class="PARAMETER">column</replaceable>.
       </para>
      </listitem>
     </varlistentry>

74 75 76 77 78 79 80 81 82 83
     <varlistentry>
      <term><replaceable class="PARAMETER">DEFAULT</replaceable></term>
      <listitem>
       <para>
	This column will be filled in by the column DEFAULT clause, or NULL if
	a default is not available.
       </para>
      </listitem>
     </varlistentry>

84 85 86 87 88 89 90 91 92 93 94 95
     <varlistentry>
      <term><replaceable class="PARAMETER">query</replaceable></term>
      <listitem>
       <para>
	A valid query. Refer to the SELECT statement for a further description
	of valid arguments.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </refsect2>
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  <refsect2 id="R2-SQL-INSERT-2">
   <title>
    Outputs
   </title>
   <para>

    <variablelist>
     <varlistentry>
      <term><computeroutput>
INSERT <replaceable>oid</replaceable> 1
       </computeroutput></term>
      <listitem>
       <para>
	Message returned if only one row was inserted.
	<returnvalue><replaceable>oid</replaceable></returnvalue>
	is the numeric <acronym>OID</acronym> of the inserted row.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><computeroutput>
INSERT 0 <replaceable>#</replaceable>
       </computeroutput></term>
      <listitem>
       <para>
	Message returned if more than one rows were inserted.
	<returnvalue><replaceable>#</replaceable></returnvalue>
	is the number of rows inserted.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </refsect2>
 </refsynopsisdiv>

 <refsect1 id="R1-SQL-INSERT-1">
  <title>
   Description
  </title>
137

138
  <para>
139
   <command>INSERT</command> allows one to insert new rows into a
140
   table. One can insert
B
Bruce Momjian 已提交
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 144 145
  </para>

  <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 150 151 152 153
   column if a NULL is inserted into a column declared NOT NULL.
  </para>

  <para>
   If the expression for each column
154 155 156
   is not of the correct data type, automatic type coercion will be
   attempted.
  </para>
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  <para>
   You must have insert privilege to a table in order to append
   to it, as well as select privilege on any table specified
   in a WHERE clause.
  </para>
 </refsect1>

 <refsect1 id="R1-SQL-INSERT-2">
  <title>
   Usage
  </title>
  <para>
   Insert a single row into table <literal>films</literal>:

   <programlisting>
INSERT INTO films VALUES
    ('UA502','Bananas',105,'1971-07-13','Comedy',INTERVAL '82 minute');
   </programlisting>
  </para>

  <para>
179
   In this second example the last column <literal>len</literal> is
180 181 182 183 184 185 186 187
   omitted and therefore it will have the default value of NULL:

   <programlisting>
INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, DATE '1961-06-16', 'Drama');
   </programlisting>
  </para>

188 189 190 191 192 193 194 195 196 197 198 199
  <para>
   In the third example, we use the DEFAULT values for the date columns
   rather than specifying an entry.

   <programlisting>
INSERT INTO films VALUES
    ('UA502','Bananas',105,DEFAULT,'Comedy',INTERVAL '82 minute');
INSERT INTO films (code, title, did, date_prod, kind)
    VALUES ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama');
   </programlisting>
  </para>

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  <para>
   Insert a single row into table distributors; note that
   only column <literal>name</literal> is specified, so the omitted
   column <literal>did</literal> will be assigned its default value:

   <programlisting>
INSERT INTO distributors (name) VALUES ('British Lion');
   </programlisting>
  </para>

  <para>
   Insert several rows into table films from table <literal>tmp</literal>:

   <programlisting>
INSERT INTO films SELECT * FROM tmp;
   </programlisting>
  </para>

  <para>
219 220
   Insert into arrays (refer to the
   <citetitle>PostgreSQL User's Guide</citetitle> for further
221
   information about arrays):
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
   <programlisting>
-- Create an empty 3x3 gameboard for noughts-and-crosses
-- (all of these queries create the same board attribute)
INSERT INTO tictactoe (game, board[1:3][1:3])
    VALUES (1,'{{"","",""},{},{"",""}}');
INSERT INTO tictactoe (game, board[3][3])
    VALUES (2,'{}');
INSERT INTO tictactoe (game, board)
    VALUES (3,'{{,,},{,,},{,,}}');
   </programlisting>
  </para>
 </refsect1>

 <refsect1 id="R1-SQL-INSERT-3">
  <title>
   Compatibility
  </title>
240

241 242 243 244 245 246
  <refsect2 id="R2-SQL-INSERT-4">
   <title>
    SQL92
   </title>
   <para>
    <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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
   </para>
  </refsect2>
 </refsect1>
</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
271
-->