1
- <!-- $Header: /cvsroot/pgsql/doc/src/sgml/array.sgml,v 1.19 2002/01/20 22: 19:55 petere Exp $ -->
1
+ <!-- $Header: /cvsroot/pgsql/doc/src/sgml/array.sgml,v 1.20 2002/03/17 19:59:57 tgl Exp $ -->
2
2
3
3
<chapter id="arrays">
4
4
<title>Arrays</title>
@@ -22,8 +22,9 @@ CREATE TABLE sal_emp (
22
22
As shown, an array data type is named by appending square brackets
23
23
(<literal>[]</>) to the data type name of the array elements.
24
24
The above query will create a table named
25
- <structname>sal_emp</structname> with a <type>text</type> string
26
- (<structfield>name</structfield>), a one-dimensional array of type
25
+ <structname>sal_emp</structname> with columns including
26
+ a <type>text</type> string (<structfield>name</structfield>),
27
+ a one-dimensional array of type
27
28
<type>integer</type> (<structfield>pay_by_quarter</structfield>),
28
29
which represents the employee's salary by quarter, and a
29
30
two-dimensional array of <type>text</type>
@@ -35,7 +36,7 @@ CREATE TABLE sal_emp (
35
36
Now we do some <command>INSERT</command>s. Observe that to write an array
36
37
value, we enclose the element values within curly braces and separate them
37
38
by commas. If you know C, this is not unlike the syntax for
38
- initializing structures.
39
+ initializing structures. (More details appear below.)
39
40
40
41
<programlisting>
41
42
INSERT INTO sal_emp
@@ -66,7 +67,7 @@ SELECT name FROM sal_emp WHERE pay_by_quarter[1] <> pay_by_quarter[2];
66
67
</programlisting>
67
68
68
69
The array subscript numbers are written within square brackets.
69
- <productname>PostgreSQL</productname> uses the
70
+ By default <productname>PostgreSQL</productname> uses the
70
71
<quote>one-based</quote> numbering convention for arrays, that is,
71
72
an array of <replaceable>n</> elements starts with <literal>array[1]</literal> and
72
73
ends with <literal>array[<replaceable>n</>]</literal>.
@@ -99,7 +100,7 @@ SELECT schedule[1:2][1:1] FROM sal_emp WHERE name = 'Bill';
99
100
100
101
schedule
101
102
--------------------
102
- {{" meeting" },{""}}
103
+ {{meeting},{""}}
103
104
(1 row)
104
105
</programlisting>
105
106
@@ -144,11 +145,17 @@ UPDATE sal_emp SET pay_by_quarter[1:2] = '{27000,27000}'
144
145
those already present, or by assigning to a slice that is adjacent
145
146
to or overlaps the data already present. For example, if an array
146
147
value currently has 4 elements, it will have five elements after an
147
- update that assigns to array[5]. Currently, enlargement in this
148
- fashion is only allowed for one-dimensional arrays, not
148
+ update that assigns to <literal> array[5]</> . Currently, enlargement in
149
+ this fashion is only allowed for one-dimensional arrays, not
149
150
multidimensional arrays.
150
151
</para>
151
152
153
+ <para>
154
+ Array slice assignment allows creation of arrays that do not use one-based
155
+ subscripts. For example one might assign to <literal>array[-2:7]</> to
156
+ create an array with subscript values running from -2 to 7.
157
+ </para>
158
+
152
159
<para>
153
160
The syntax for <command>CREATE TABLE</command> allows fixed-length
154
161
arrays to be defined:
@@ -168,7 +175,9 @@ CREATE TABLE tictactoe (
168
175
Actually, the current implementation does not enforce the declared
169
176
number of dimensions either. Arrays of a particular element type are
170
177
all considered to be of the same type, regardless of size or number
171
- of dimensions.
178
+ of dimensions. So, declaring number of dimensions or sizes in
179
+ <command>CREATE TABLE</command> is simply documentation, it does not
180
+ affect runtime behavior.
172
181
</para>
173
182
174
183
<para>
@@ -248,19 +257,55 @@ SELECT * FROM sal_emp WHERE pay_by_quarter **= 10000;
248
257
</para>
249
258
</note>
250
259
260
+ <formalpara>
261
+ <title>Array input and output syntax.</title>
262
+ <para>
263
+ The external representation of an array value consists of items that
264
+ are interpreted according to the I/O conversion rules for the array's
265
+ element type, plus decoration that indicates the array structure.
266
+ The decoration consists of curly braces (<literal>{</> and <literal>}</>)
267
+ around the array value plus delimiter characters between adjacent items.
268
+ The delimiter character is usually a comma (<literal>,</>) but can be
269
+ something else: it is determined by the <literal>typdelim</> setting
270
+ for the array's element type. (Among the standard datatypes provided
271
+ in the <productname>PostgreSQL</productname> distribution, type
272
+ <literal>box</> uses a semicolon (<literal>;</>) but all the others
273
+ use comma.) In a multidimensional array, each dimension (row, plane,
274
+ cube, etc.) gets its own level of curly braces, and delimiters
275
+ must be written between adjacent curly-braced entities of the same level.
276
+ You may write whitespace before a left brace, after a right
277
+ brace, or before any individual item string. Whitespace after an item
278
+ is not ignored, however: after skipping leading whitespace, everything
279
+ up to the next right brace or delimiter is taken as the item value.
280
+ </para>
281
+ </formalpara>
282
+
251
283
<formalpara>
252
284
<title>Quoting array elements.</title>
253
285
<para>
254
- As shown above, when writing an array literal value you may write double
286
+ As shown above, when writing an array value you may write double
255
287
quotes around any individual array
256
288
element. You <emphasis>must</> do so if the element value would otherwise
257
289
confuse the array-value parser. For example, elements containing curly
258
- braces, commas, double quotes, backslashes, or white space must be
259
- double-quoted. To put a double quote or backslash in an array element
260
- value, precede it with a backslash.
290
+ braces, commas (or whatever the delimiter character is), double quotes,
291
+ backslashes, or leading white space must be double-quoted. To put a double
292
+ quote or backslash in an array element value, precede it with a backslash.
293
+ Alternatively, you can use backslash-escaping to protect all data characters
294
+ that would otherwise be taken as array syntax or ignorable white space.
261
295
</para>
262
296
</formalpara>
263
297
298
+ <para>
299
+ The array output routine will put double quotes around element values
300
+ if they are empty strings or contain curly braces, delimiter characters,
301
+ double quotes, backslashes, or white space. Double quotes and backslashes
302
+ embedded in element values will be backslash-escaped. For numeric
303
+ datatypes it is safe to assume that double quotes will never appear, but
304
+ for textual datatypes one should be prepared to cope with either presence
305
+ or absence of quotes. (This is a change in behavior from pre-7.2
306
+ <productname>PostgreSQL</productname> releases.)
307
+ </para>
308
+
264
309
<tip>
265
310
<para>
266
311
Remember that what you write in an SQL query will first be interpreted
0 commit comments