OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I686LINUX / util / I686LINUX / doc / postgresql / html / dml.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Data Manipulation</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
9 REV="MADE"
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
11 REL="HOME"
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
14 REL="UP"
15 TITLE="The SQL Language"
16 HREF="sql.html"><LINK
17 REL="PREVIOUS"
18 TITLE="Dependency Tracking"
19 HREF="ddl-depend.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 NAME="creation"
27 CONTENT="2003-12-22T03:48:47"></HEAD
28 ><BODY
29 CLASS="CHAPTER"
30 ><DIV
31 CLASS="NAVHEADER"
32 ><TABLE
33 SUMMARY="Header navigation table"
34 WIDTH="100%"
35 BORDER="0"
36 CELLPADDING="0"
37 CELLSPACING="0"
38 ><TR
39 ><TH
40 COLSPAN="5"
41 ALIGN="center"
42 VALIGN="bottom"
43 >PostgreSQL 7.4.1 Documentation</TH
44 ></TR
45 ><TR
46 ><TD
47 WIDTH="10%"
48 ALIGN="left"
49 VALIGN="top"
50 ><A
51 HREF="ddl-depend.html"
52 ACCESSKEY="P"
53 >Prev</A
54 ></TD
55 ><TD
56 WIDTH="10%"
57 ALIGN="left"
58 VALIGN="top"
59 ><A
60 HREF="ddl.html"
61 >Fast Backward</A
62 ></TD
63 ><TD
64 WIDTH="60%"
65 ALIGN="center"
66 VALIGN="bottom"
67 ></TD
68 ><TD
69 WIDTH="10%"
70 ALIGN="right"
71 VALIGN="top"
72 ><A
73 HREF="queries.html"
74 >Fast Forward</A
75 ></TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="top"
80 ><A
81 HREF="dml-update.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="CHAPTER"
92 ><H1
93 ><A
94 NAME="DML"
95 ></A
96 >Chapter 6. Data Manipulation</H1
97 ><DIV
98 CLASS="TOC"
99 ><DL
100 ><DT
101 ><B
102 >Table of Contents</B
103 ></DT
104 ><DT
105 >6.1. <A
106 HREF="dml.html#DML-INSERT"
107 >Inserting Data</A
108 ></DT
109 ><DT
110 >6.2. <A
111 HREF="dml-update.html"
112 >Updating Data</A
113 ></DT
114 ><DT
115 >6.3. <A
116 HREF="dml-delete.html"
117 >Deleting Data</A
118 ></DT
119 ></DL
120 ></DIV
121 ><P
122 >  The previous chapter discussed how to create tables and other
123   structures to hold your data.  Now it is time to fill the tables
124   with data.  This chapter covers how to insert, update, and delete
125   table data.  We also introduce ways to effect automatic data changes
126   when certain events occur: triggers and rewrite rules.  The chapter
127   after this will finally explain how to extract your long-lost data
128   back out of the database.
129  </P
130 ><DIV
131 CLASS="SECT1"
132 ><H1
133 CLASS="SECT1"
134 ><A
135 NAME="DML-INSERT"
136 >6.1. Inserting Data</A
137 ></H1
138 ><A
139 NAME="AEN2440"
140 ></A
141 ><A
142 NAME="AEN2442"
143 ></A
144 ><P
145 >   When a table is created, it contains no data.  The first thing to
146    do before a database can be of much use is to insert data.  Data is
147    conceptually inserted one row at a time.  Of course you can also
148    insert more than one row, but there is no way to insert less than
149    one row at a time.  Even if you know only some column values, a
150    complete row must be created.
151   </P
152 ><P
153 >   To create a new row, use the <TT
154 CLASS="LITERAL"
155 >INSERT</TT
156 > command.
157    The command requires the table name and a value for each of the
158    columns of the table.  For example, consider the products table
159    from <A
160 HREF="ddl.html"
161 >Chapter 5</A
162 >:
163 </P><PRE
164 CLASS="PROGRAMLISTING"
165 >CREATE TABLE products (
166     product_no integer,
167     name text,
168     price numeric
169 );</PRE
170 ><P>
171    An example command to insert a row would be:
172 </P><PRE
173 CLASS="PROGRAMLISTING"
174 >INSERT INTO products VALUES (1, 'Cheese', 9.99);</PRE
175 ><P>
176    The data values are listed in the order in which the columns appear
177    in the table, separated by commas.  Usually, the data values will
178    be literals (constants), but scalar expressions are also allowed.
179   </P
180 ><P
181 >   The above syntax has the drawback that you need to know the order
182    of the columns in the table.  To avoid that you can also list the
183    columns explicitly.  For example, both of the following commands
184    have the same effect as the one above:
185 </P><PRE
186 CLASS="PROGRAMLISTING"
187 >INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', 9.99);
188 INSERT INTO products (name, price, product_no) VALUES ('Cheese', 9.99, 1);</PRE
189 ><P>
190    Many users consider it good practice to always list the column
191    names.
192   </P
193 ><P
194 >   If you don't have values for all the columns, you can omit some of
195    them.  In that case, the columns will be filled with their default
196    values.  For example,
197 </P><PRE
198 CLASS="PROGRAMLISTING"
199 >INSERT INTO products (product_no, name) VALUES (1, 'Cheese');
200 INSERT INTO products VALUES (1, 'Cheese');</PRE
201 ><P>
202    The second form is a <SPAN
203 CLASS="PRODUCTNAME"
204 >PostgreSQL</SPAN
205 >
206    extension.  It fills the columns from the left with as many values
207    as are given, and the rest will be defaulted.
208   </P
209 ><P
210 >   For clarity, you can also request default values explicitly, for
211    individual columns or for the entire row:
212 </P><PRE
213 CLASS="PROGRAMLISTING"
214 >INSERT INTO products (product_no, name, price) VALUES (1, 'Cheese', DEFAULT);
215 INSERT INTO products DEFAULT VALUES;</PRE
216 ><P>
217   </P
218 ><DIV
219 CLASS="TIP"
220 ><BLOCKQUOTE
221 CLASS="TIP"
222 ><P
223 ><B
224 >Tip: </B
225 >    To do <SPAN
226 CLASS="QUOTE"
227 >"bulk loads"</SPAN
228 >, that is, inserting a lot of data,
229     take a look at the <A
230 HREF="sql-copy.html"
231 ><I
232 >COPY</I
233 ></A
234 > command.  It is not as flexible as the
235     <TT
236 CLASS="COMMAND"
237 >INSERT</TT
238 > command, but is more efficient.
239    </P
240 ></BLOCKQUOTE
241 ></DIV
242 ></DIV
243 ></DIV
244 ><DIV
245 CLASS="NAVFOOTER"
246 ><HR
247 ALIGN="LEFT"
248 WIDTH="100%"><TABLE
249 SUMMARY="Footer navigation table"
250 WIDTH="100%"
251 BORDER="0"
252 CELLPADDING="0"
253 CELLSPACING="0"
254 ><TR
255 ><TD
256 WIDTH="33%"
257 ALIGN="left"
258 VALIGN="top"
259 ><A
260 HREF="ddl-depend.html"
261 ACCESSKEY="P"
262 >Prev</A
263 ></TD
264 ><TD
265 WIDTH="34%"
266 ALIGN="center"
267 VALIGN="top"
268 ><A
269 HREF="index.html"
270 ACCESSKEY="H"
271 >Home</A
272 ></TD
273 ><TD
274 WIDTH="33%"
275 ALIGN="right"
276 VALIGN="top"
277 ><A
278 HREF="dml-update.html"
279 ACCESSKEY="N"
280 >Next</A
281 ></TD
282 ></TR
283 ><TR
284 ><TD
285 WIDTH="33%"
286 ALIGN="left"
287 VALIGN="top"
288 >Dependency Tracking</TD
289 ><TD
290 WIDTH="34%"
291 ALIGN="center"
292 VALIGN="top"
293 ><A
294 HREF="sql.html"
295 ACCESSKEY="U"
296 >Up</A
297 ></TD
298 ><TD
299 WIDTH="33%"
300 ALIGN="right"
301 VALIGN="top"
302 >Updating Data</TD
303 ></TR
304 ></TABLE
305 ></DIV
306 ></BODY
307 ></HTML
308 >