~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/html/dml-insert.html

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
2
<HTML
 
3
><HEAD
 
4
><TITLE
 
5
>Inserting Data</TITLE
 
6
><META
 
7
NAME="GENERATOR"
 
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
 
9
REV="MADE"
 
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
 
11
REL="HOME"
 
12
TITLE="PostgreSQL 9.1beta1 Documentation"
 
13
HREF="index.html"><LINK
 
14
REL="UP"
 
15
TITLE="Data Manipulation"
 
16
HREF="dml.html"><LINK
 
17
REL="PREVIOUS"
 
18
TITLE="Data Manipulation"
 
19
HREF="dml.html"><LINK
 
20
REL="NEXT"
 
21
TITLE="Updating Data"
 
22
HREF="dml-update.html"><LINK
 
23
REL="STYLESHEET"
 
24
TYPE="text/css"
 
25
HREF="stylesheet.css"><META
 
26
HTTP-EQUIV="Content-Type"
 
27
CONTENT="text/html; charset=ISO-8859-1"><META
 
28
NAME="creation"
 
29
CONTENT="2011-04-27T21:20:33"></HEAD
 
30
><BODY
 
31
CLASS="SECT1"
 
32
><DIV
 
33
CLASS="NAVHEADER"
 
34
><TABLE
 
35
SUMMARY="Header navigation table"
 
36
WIDTH="100%"
 
37
BORDER="0"
 
38
CELLPADDING="0"
 
39
CELLSPACING="0"
 
40
><TR
 
41
><TH
 
42
COLSPAN="5"
 
43
ALIGN="center"
 
44
VALIGN="bottom"
 
45
><A
 
46
HREF="index.html"
 
47
>PostgreSQL 9.1beta1 Documentation</A
 
48
></TH
 
49
></TR
 
50
><TR
 
51
><TD
 
52
WIDTH="10%"
 
53
ALIGN="left"
 
54
VALIGN="top"
 
55
><A
 
56
TITLE="Data Manipulation"
 
57
HREF="dml.html"
 
58
ACCESSKEY="P"
 
59
>Prev</A
 
60
></TD
 
61
><TD
 
62
WIDTH="10%"
 
63
ALIGN="left"
 
64
VALIGN="top"
 
65
><A
 
66
TITLE="Data Manipulation"
 
67
HREF="dml.html"
 
68
>Fast Backward</A
 
69
></TD
 
70
><TD
 
71
WIDTH="60%"
 
72
ALIGN="center"
 
73
VALIGN="bottom"
 
74
>Chapter 6. Data Manipulation</TD
 
75
><TD
 
76
WIDTH="10%"
 
77
ALIGN="right"
 
78
VALIGN="top"
 
79
><A
 
80
TITLE="Data Manipulation"
 
81
HREF="dml.html"
 
82
>Fast Forward</A
 
83
></TD
 
84
><TD
 
85
WIDTH="10%"
 
86
ALIGN="right"
 
87
VALIGN="top"
 
88
><A
 
89
TITLE="Updating Data"
 
90
HREF="dml-update.html"
 
91
ACCESSKEY="N"
 
92
>Next</A
 
93
></TD
 
94
></TR
 
95
></TABLE
 
96
><HR
 
97
ALIGN="LEFT"
 
98
WIDTH="100%"></DIV
 
99
><DIV
 
100
CLASS="SECT1"
 
101
><H1
 
102
CLASS="SECT1"
 
103
><A
 
104
NAME="DML-INSERT"
 
105
>6.1. Inserting Data</A
 
106
></H1
 
107
><P
 
108
>   When a table is created, it contains no data.  The first thing to
 
109
   do before a database can be of much use is to insert data.  Data is
 
110
   conceptually inserted one row at a time.  Of course you can also
 
111
   insert more than one row, but there is no way to insert less than
 
112
   one row.  Even if you know only some column values, a
 
113
   complete row must be created.
 
114
  </P
 
115
><P
 
116
>   To create a new row, use the <A
 
117
HREF="sql-insert.html"
 
118
>INSERT</A
 
119
>
 
120
   command.  The command requires the
 
121
   table name and column values.  For
 
122
   example, consider the products table from <A
 
123
HREF="ddl.html"
 
124
>Chapter 5</A
 
125
>:
 
126
</P><PRE
 
127
CLASS="PROGRAMLISTING"
 
128
>CREATE TABLE products (
 
129
    product_no integer,
 
130
    name text,
 
131
    price numeric
 
132
);</PRE
 
133
><P>
 
134
   An example command to insert a row would be:
 
135
</P><PRE
 
136
CLASS="PROGRAMLISTING"
 
137
>INSERT INTO products VALUES (1, 'Cheese', 9.99);</PRE
 
138
><P>
 
139
   The data values are listed in the order in which the columns appear
 
140
   in the table, separated by commas.  Usually, the data values will
 
141
   be literals (constants), but scalar expressions are also allowed.
 
142
  </P
 
143
><P
 
144
>   The above syntax has the drawback that you need to know the order
 
145
   of the columns in the table.  To avoid this you can also list the
 
146
   columns explicitly.  For example, both of the following commands
 
147
   have the same effect as the one above:
 
148
</P><PRE
 
149
CLASS="PROGRAMLISTING"
 
150
>INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', 9.99);
 
151
INSERT INTO products (name, price, product_no) VALUES ('Cheese', 9.99, 1);</PRE
 
152
><P>
 
153
   Many users consider it good practice to always list the column
 
154
   names.
 
155
  </P
 
156
><P
 
157
>   If you don't have values for all the columns, you can omit some of
 
158
   them.  In that case, the columns will be filled with their default
 
159
   values.  For example:
 
160
</P><PRE
 
161
CLASS="PROGRAMLISTING"
 
162
>INSERT INTO products (product_no, name) VALUES (1, 'Cheese');
 
163
INSERT INTO products VALUES (1, 'Cheese');</PRE
 
164
><P>
 
165
   The second form is a <SPAN
 
166
CLASS="PRODUCTNAME"
 
167
>PostgreSQL</SPAN
 
168
>
 
169
   extension.  It fills the columns from the left with as many values
 
170
   as are given, and the rest will be defaulted.
 
171
  </P
 
172
><P
 
173
>   For clarity, you can also request default values explicitly, for
 
174
   individual columns or for the entire row:
 
175
</P><PRE
 
176
CLASS="PROGRAMLISTING"
 
177
>INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', DEFAULT);
 
178
INSERT INTO products DEFAULT VALUES;</PRE
 
179
><P>
 
180
  </P
 
181
><P
 
182
>   You can insert multiple rows in a single command:
 
183
</P><PRE
 
184
CLASS="PROGRAMLISTING"
 
185
>INSERT INTO products (product_no, name, price) VALUES
 
186
    (1, 'Cheese', 9.99),
 
187
    (2, 'Bread', 1.99),
 
188
    (3, 'Milk', 2.99);</PRE
 
189
><P>
 
190
  </P
 
191
><DIV
 
192
CLASS="TIP"
 
193
><BLOCKQUOTE
 
194
CLASS="TIP"
 
195
><P
 
196
><B
 
197
>Tip: </B
 
198
>    When inserting a lot of data at the same time, considering using
 
199
    the <A
 
200
HREF="sql-copy.html"
 
201
>COPY</A
 
202
> command.
 
203
    It is not as flexible as the <A
 
204
HREF="sql-insert.html"
 
205
>INSERT</A
 
206
>
 
207
    command, but is more efficient. Refer
 
208
    to <A
 
209
HREF="populate.html"
 
210
>Section 14.4</A
 
211
> for more information on improving
 
212
    bulk loading performance.
 
213
   </P
 
214
></BLOCKQUOTE
 
215
></DIV
 
216
></DIV
 
217
><DIV
 
218
CLASS="NAVFOOTER"
 
219
><HR
 
220
ALIGN="LEFT"
 
221
WIDTH="100%"><TABLE
 
222
SUMMARY="Footer navigation table"
 
223
WIDTH="100%"
 
224
BORDER="0"
 
225
CELLPADDING="0"
 
226
CELLSPACING="0"
 
227
><TR
 
228
><TD
 
229
WIDTH="33%"
 
230
ALIGN="left"
 
231
VALIGN="top"
 
232
><A
 
233
HREF="dml.html"
 
234
ACCESSKEY="P"
 
235
>Prev</A
 
236
></TD
 
237
><TD
 
238
WIDTH="34%"
 
239
ALIGN="center"
 
240
VALIGN="top"
 
241
><A
 
242
HREF="index.html"
 
243
ACCESSKEY="H"
 
244
>Home</A
 
245
></TD
 
246
><TD
 
247
WIDTH="33%"
 
248
ALIGN="right"
 
249
VALIGN="top"
 
250
><A
 
251
HREF="dml-update.html"
 
252
ACCESSKEY="N"
 
253
>Next</A
 
254
></TD
 
255
></TR
 
256
><TR
 
257
><TD
 
258
WIDTH="33%"
 
259
ALIGN="left"
 
260
VALIGN="top"
 
261
>Data Manipulation</TD
 
262
><TD
 
263
WIDTH="34%"
 
264
ALIGN="center"
 
265
VALIGN="top"
 
266
><A
 
267
HREF="dml.html"
 
268
ACCESSKEY="U"
 
269
>Up</A
 
270
></TD
 
271
><TD
 
272
WIDTH="33%"
 
273
ALIGN="right"
 
274
VALIGN="top"
 
275
>Updating Data</TD
 
276
></TR
 
277
></TABLE
 
278
></DIV
 
279
></BODY
 
280
></HTML
 
281
>
 
 
b'\\ No newline at end of file'