1- <!-- $Header: /cvsroot/pgsql/doc/src/sgml/array.sgml,v 1.24 2002/11/11 20:14:02 petere Exp $ -->
1+ <!-- $Header: /cvsroot/pgsql/doc/src/sgml/array.sgml,v 1.25 2003/03/13 01:30:26 petere Exp $ -->
22
33<sect1 id="arrays">
44 <title>Arrays</title>
1010 <para>
1111 <productname>PostgreSQL</productname> allows columns of a table to be
1212 defined as variable-length multidimensional arrays. Arrays of any
13- built-in type or user-defined type can be created. To illustrate
14- their use, we create this table:
13+ built-in type or user-defined type can be created.
14+ </para>
15+
16+ <sect2>
17+ <title>Declaration of Array Types</title>
18+
19+ <para>
20+ To illustrate the use of array types, we create this table:
1521<programlisting>
1622CREATE TABLE sal_emp (
1723 name text,
@@ -20,24 +26,27 @@ CREATE TABLE sal_emp (
2026);
2127</programlisting>
2228 As shown, an array data type is named by appending square brackets
23- (<literal>[]</>) to the data type name of the array elements.
24- The above command will create a table named
25- <structname>sal_emp</structname> with columns including
26- a <type>text</type> string (<structfield>name</structfield>),
27- a one-dimensional array of type
28- <type>integer</type> (<structfield>pay_by_quarter</structfield>),
29- which represents the employee's salary by quarter, and a
30- two-dimensional array of <type>text</type>
31- (<structfield>schedule</structfield>), which represents the
32- employee's weekly schedule.
29+ (<literal>[]</>) to the data type name of the array elements. The
30+ above command will create a table named
31+ <structname>sal_emp</structname> with a column of type
32+ <type>text</type> (<structfield>name</structfield>), a
33+ one-dimensional array of type <type>integer</type>
34+ (<structfield>pay_by_quarter</structfield>), which represents the
35+ employee's salary by quarter, and a two-dimensional array of
36+ <type>text</type> (<structfield>schedule</structfield>), which
37+ represents the employee's weekly schedule.
3338 </para>
39+ </sect2>
40+
41+ <sect2>
42+ <title>Array Value Input</title>
3443
3544 <para>
36- Now wedo some <command>INSERT</command>s .Observe that to write an array
45+ Now wecan show some <command>INSERT</command> statements .To write an array
3746 value, we enclose the element values within curly braces and separate them
3847 by commas. If you know C, this is not unlike the syntax for
3948 initializing structures. (More details appear below.)
40-
49+
4150<programlisting>
4251INSERT INTO sal_emp
4352 VALUES ('Bill',
@@ -51,8 +60,21 @@ INSERT INTO sal_emp
5160</programlisting>
5261 </para>
5362
63+ <note>
64+ <para>
65+ A limitation of the present array implementation is that individual
66+ elements of an array cannot be SQL null values. The entire array can be set
67+ to null, but you can't have an array with some elements null and some
68+ not. Fixing this is on the to-do list.
69+ </para>
70+ </note>
71+ </sect2>
72+
73+ <sect2>
74+ <title>Array Value References</title>
75+
5476 <para>
55- Now, we can run some queries on<structname>sal_emp</structname> .
77+ Now, we can run some queries onthe table .
5678 First, we show how to access a single element of an array at a time.
5779 This query retrieves the names of the employees whose pay changed in
5880 the second quarter:
@@ -91,7 +113,7 @@ SELECT pay_by_quarter[3] FROM sal_emp;
91113 We can also access arbitrary rectangular slices of an array, or
92114 subarrays. An array slice is denoted by writing
93115 <literal><replaceable>lower-bound</replaceable>:<replaceable>upper-bound</replaceable></literal>
94- for one or more array dimensions.This query retrieves the first
116+ for one or more array dimensions.For example, this query retrieves the first
95117 item on Bill's schedule for the first two days of the week:
96118
97119<programlisting>
@@ -109,7 +131,7 @@ SELECT schedule[1:2][1:1] FROM sal_emp WHERE name = 'Bill';
109131SELECT schedule[1:2][1] FROM sal_emp WHERE name = 'Bill';
110132</programlisting>
111133
112- with the same result. An array subscripting operation is taken to
134+ with the same result. An array subscripting operation isalways taken to
113135 represent an array slice if any of the subscripts are written in the
114136 form
115137 <literal><replaceable>lower</replaceable>:<replaceable>upper</replaceable></literal>.
@@ -199,10 +221,15 @@ SELECT array_dims(schedule) FROM sal_emp WHERE name = 'Carol';
199221 array_lower</function> return the upper/lower bound of the
200222 given array dimension, respectively.
201223 </para>
224+ </sect2>
225+
226+ <sect2>
227+ <title>Searching in Arrays</title>
202228
203229 <para>
204230 To search for a value in an array, you must check each value of the
205- array. This can be done by hand (if you know the size of the array):
231+ array. This can be done by hand (if you know the size of the array).
232+ For example:
206233
207234<programlisting>
208235SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR
@@ -212,8 +239,8 @@ SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR
212239</programlisting>
213240
214241 However, this quickly becomes tedious for large arrays, and is not
215- helpful if the size of the array is unknown. Although it is notpart
216- of the primary <productname>PostgreSQL</productname> distribution ,
242+ helpful if the size of the array is unknown. Although it is notbuilt
243+ into <productname>PostgreSQL</productname>,
217244 there is an extension available that defines new functions and
218245 operators for iterating over array values. Using this, the above
219246 query could be:
@@ -222,7 +249,7 @@ SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR
222249SELECT * FROM sal_emp WHERE pay_by_quarter[1:4] *= 10000;
223250</programlisting>
224251
225- To search the entire array (not just specifiedcolumns ), you could
252+ To search the entire array (not just specifiedslices ), you could
226253 use:
227254
228255<programlisting>
@@ -249,18 +276,11 @@ SELECT * FROM sal_emp WHERE pay_by_quarter **= 10000;
249276 Tables can obviously be searched easily.
250277 </para>
251278 </tip>
279+ </sect2>
252280
253- <note>
254- <para>
255- A limitation of the present array implementation is that individual
256- elements of an array cannot be SQL null values. The entire array can be set
257- to null, but you can't have an array with some elements null and some
258- not. Fixing this is on the to-do list.
259- </para>
260- </note>
281+ <sect2>
282+ <title>Array Input and Output Syntax</title>
261283
262- <formalpara>
263- <title>Array input and output syntax.</title>
264284 <para>
265285 The external representation of an array value consists of items that
266286 are interpreted according to the I/O conversion rules for the array's
@@ -280,10 +300,11 @@ SELECT * FROM sal_emp WHERE pay_by_quarter **= 10000;
280300 is not ignored, however: after skipping leading whitespace, everything
281301 up to the next right brace or delimiter is taken as the item value.
282302 </para>
283- </formalpara>
303+ </sect2>
304+
305+ <sect2>
306+ <title>Quoting Array Elements</title>
284307
285- <formalpara>
286- <title>Quoting array elements.</title>
287308 <para>
288309 As shown above, when writing an array value you may write double
289310 quotes around any individual array
@@ -295,7 +316,6 @@ SELECT * FROM sal_emp WHERE pay_by_quarter **= 10000;
295316 Alternatively, you can use backslash-escaping to protect all data characters
296317 that would otherwise be taken as array syntax or ignorable white space.
297318 </para>
298- </formalpara>
299319
300320 <para>
301321 The array output routine will put double quotes around element values
@@ -308,7 +328,7 @@ SELECT * FROM sal_emp WHERE pay_by_quarter **= 10000;
308328 <productname>PostgreSQL</productname> releases.)
309329 </para>
310330
311- <tip >
331+ <note >
312332 <para>
313333 Remember that what you write in an SQL command will first be interpreted
314334 as a string literal, and then as an array. This doubles the number of
@@ -325,6 +345,7 @@ INSERT ... VALUES ('{"\\\\","\\""}');
325345 <type>bytea</> for example, we might need as many as eight backslashes
326346 in the command to get one backslash into the stored array element.)
327347 </para>
328- </tip>
348+ </note>
349+ </sect2>
329350
330351</sect1>