~ubuntu-branches/ubuntu/utopic/pgadmin3/utopic-proposed

« back to all changes in this revision

Viewing changes to docs/en_US/pg/arrays.html

  • Committer: Bazaar Package Importer
  • Author(s): Raphael Enrici
  • Date: 2004-12-14 23:46:39 UTC
  • Revision ID: james.westby@ubuntu.com-20041214234639-tve0i5l49fq13jli
Tags: upstream-1.2.0
Import upstream version 1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html>
 
2
<head>
 
3
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 
4
<title>8.10.�Arrays</title>
 
5
<link rel="stylesheet" href="stylesheet.css" type="text/css">
 
6
<link rev="made" href="pgsql-docs@postgresql.org">
 
7
<meta name="generator" content="DocBook XSL Stylesheets V1.64.1">
 
8
<link rel="home" href="index.html" title="PostgreSQL 8.0.0beta5 Documentation">
 
9
<link rel="up" href="datatype.html" title="Chapter�8.�Data Types">
 
10
<link rel="previous" href="datatype-bit.html" title="8.9.�Bit String Types">
 
11
<link rel="next" href="rowtypes.html" title="8.11.�Composite Types">
 
12
</head>
 
13
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="sect1" lang="en">
 
14
<div class="titlepage">
 
15
<div><div><h2 class="title" style="clear: both">
 
16
<a name="arrays"></a>8.10.�Arrays</h2></div></div>
 
17
<div></div>
 
18
</div>
 
19
<a name="id2537522"></a><p>  <span class="productname">PostgreSQL</span> allows columns of a table to be
 
20
  defined as variable-length multidimensional arrays. Arrays of any
 
21
  built-in or user-defined base type can be created.  (Arrays of
 
22
  composite types or domains are not yet supported, however.)
 
23
 </p>
 
24
<div class="sect2" lang="en">
 
25
<div class="titlepage">
 
26
<div><div><h3 class="title">
 
27
<a name="id2537539"></a>8.10.1.�Declaration of Array Types</h3></div></div>
 
28
<div></div>
 
29
</div>
 
30
<p>  To illustrate the use of array types, we create this table:
 
31
</p>
 
32
<pre class="programlisting">CREATE TABLE sal_emp (
 
33
    name            text,
 
34
    pay_by_quarter  integer[],
 
35
    schedule        text[][]
 
36
);</pre>
 
37
<p>
 
38
  As shown, an array data type is named by appending square brackets
 
39
  (<tt class="literal">[]</tt>) to the data type name of the array elements.  The
 
40
  above command will create a table named
 
41
  <tt class="structname">sal_emp</tt> with a column of type
 
42
  <tt class="type">text</tt> (<tt class="structfield">name</tt>), a
 
43
  one-dimensional array of type <tt class="type">integer</tt>
 
44
  (<tt class="structfield">pay_by_quarter</tt>), which represents the
 
45
  employee's salary by quarter, and a two-dimensional array of
 
46
  <tt class="type">text</tt> (<tt class="structfield">schedule</tt>), which
 
47
  represents the employee's weekly schedule.
 
48
 </p>
 
49
<p>  The syntax for <tt class="command">CREATE TABLE</tt> allows the exact size of
 
50
  arrays to be specified, for example:
 
51
 
 
52
</p>
 
53
<pre class="programlisting">CREATE TABLE tictactoe (
 
54
    squares   integer[3][3]
 
55
);</pre>
 
56
<p>
 
57
 
 
58
  However, the current implementation does not enforce the array size
 
59
  limits [mdash ] the behavior is the same as for arrays of unspecified
 
60
  length.
 
61
 </p>
 
62
<p>  Actually, the current implementation does not enforce the declared
 
63
  number of dimensions either.  Arrays of a particular element type are
 
64
  all considered to be of the same type, regardless of size or number
 
65
  of dimensions.  So, declaring number of dimensions or sizes in
 
66
  <tt class="command">CREATE TABLE</tt> is simply documentation, it does not
 
67
  affect runtime behavior.
 
68
 </p>
 
69
<p>  An alternative, SQL99-standard syntax may be used for one-dimensional arrays.
 
70
  <tt class="structfield">pay_by_quarter</tt> could have been defined as:
 
71
</p>
 
72
<pre class="programlisting">    pay_by_quarter  integer ARRAY[4],</pre>
 
73
<p>
 
74
  This syntax requires an integer constant to denote the array size.
 
75
  As before, however, <span class="productname">PostgreSQL</span> does not enforce the
 
76
  size restriction.
 
77
 </p>
 
78
</div>
 
79
<div class="sect2" lang="en">
 
80
<div class="titlepage">
 
81
<div><div><h3 class="title">
 
82
<a name="id2537647"></a>8.10.2.�Array Value Input</h3></div></div>
 
83
<div></div>
 
84
</div>
 
85
<a name="id2537650"></a><p>   To write an array value as a literal constant, enclose the element
 
86
   values within curly braces and separate them by commas.  (If you
 
87
   know C, this is not unlike the C syntax for initializing
 
88
   structures.)  You may put double quotes around any element value,
 
89
   and must do so if it contains commas or curly braces.  (More
 
90
   details appear below.)  Thus, the general format of an array
 
91
   constant is the following:
 
92
</p>
 
93
<pre class="synopsis">'{ <i class="replaceable"><tt>val1</tt></i> <i class="replaceable"><tt>delim</tt></i> <i class="replaceable"><tt>val2</tt></i> <i class="replaceable"><tt>delim</tt></i> ... }'</pre>
 
94
<p>
 
95
   where <i class="replaceable"><tt>delim</tt></i> is the delimiter character
 
96
   for the type, as recorded in its <tt class="literal">pg_type</tt> entry.
 
97
   Among the standard data types provided in the
 
98
   <span class="productname">PostgreSQL</span> distribution, type
 
99
   <tt class="literal">box</tt> uses a semicolon (<tt class="literal">;</tt>) but all the others
 
100
   use comma (<tt class="literal">,</tt>). Each <i class="replaceable"><tt>val</tt></i> is
 
101
   either a constant of the array element type, or a subarray. An example
 
102
   of an array constant is
 
103
</p>
 
104
<pre class="programlisting">'{{1,2,3},{4,5,6},{7,8,9}}'</pre>
 
105
<p>
 
106
   This constant is a two-dimensional, 3-by-3 array consisting of
 
107
   three subarrays of integers.
 
108
  </p>
 
109
<p>   (These kinds of array constants are actually only a special case of
 
110
   the generic type constants discussed in <a href="sql-syntax.html#sql-syntax-constants-generic" title="4.1.2.5.�Constants of Other Types">Section�4.1.2.5, &#8220;Constants of Other Types&#8221;</a>.  The constant is initially
 
111
   treated as a string and passed to the array input conversion
 
112
   routine.  An explicit type specification might be necessary.)
 
113
  </p>
 
114
<p>   Now we can show some <tt class="command">INSERT</tt> statements.
 
115
 
 
116
</p>
 
117
<pre class="programlisting">INSERT INTO sal_emp
 
118
    VALUES ('Bill',
 
119
    '{10000, 10000, 10000, 10000}',
 
120
    '{{"meeting", "lunch"}, {"meeting"}}');
 
121
ERROR:  multidimensional arrays must have array expressions with matching dimensions</pre>
 
122
<p>
 
123
 
 
124
  Note that multidimensional arrays must have matching extents for each
 
125
  dimension. A mismatch causes an error report.
 
126
 
 
127
</p>
 
128
<pre class="programlisting">INSERT INTO sal_emp
 
129
    VALUES ('Bill',
 
130
    '{10000, 10000, 10000, 10000}',
 
131
    '{{"meeting", "lunch"}, {"training", "presentation"}}');
 
132
 
 
133
INSERT INTO sal_emp
 
134
    VALUES ('Carol',
 
135
    '{20000, 25000, 25000, 25000}',
 
136
    '{{"breakfast", "consulting"}, {"meeting", "lunch"}}');</pre>
 
137
<p>
 
138
  </p>
 
139
<p>   A limitation of the present array implementation is that individual
 
140
   elements of an array cannot be SQL null values.  The entire array
 
141
   can be set to null, but you can't have an array with some elements
 
142
   null and some not.
 
143
  </p>
 
144
<p>  The result of the previous two inserts looks like this:
 
145
</p>
 
146
<pre class="programlisting">SELECT * FROM sal_emp;
 
147
 name  |      pay_by_quarter       |                 schedule
 
148
-------+---------------------------+-------------------------------------------
 
149
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 
150
 Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
 
151
(2 rows)</pre>
 
152
<p>
 
153
 </p>
 
154
<p>  The <tt class="literal">ARRAY</tt> constructor syntax may also be used:
 
155
</p>
 
156
<pre class="programlisting">INSERT INTO sal_emp
 
157
    VALUES ('Bill',
 
158
    ARRAY[10000, 10000, 10000, 10000],
 
159
    ARRAY[['meeting', 'lunch'], ['training', 'presentation']]);
 
160
 
 
161
INSERT INTO sal_emp
 
162
    VALUES ('Carol',
 
163
    ARRAY[20000, 25000, 25000, 25000],
 
164
    ARRAY[['breakfast', 'consulting'], ['meeting', 'lunch']]);</pre>
 
165
<p>
 
166
  Notice that the array elements are ordinary SQL constants or
 
167
  expressions; for instance, string literals are single quoted, instead of
 
168
  double quoted as they would be in an array literal.  The <tt class="literal">ARRAY</tt>
 
169
  constructor syntax is discussed in more detail in
 
170
  <a href="sql-expressions.html#sql-syntax-array-constructors" title="4.2.10.�Array Constructors">Section�4.2.10, &#8220;Array Constructors&#8221;</a>.
 
171
 </p>
 
172
</div>
 
173
<div class="sect2" lang="en">
 
174
<div class="titlepage">
 
175
<div><div><h3 class="title">
 
176
<a name="id2537839"></a>8.10.3.�Accessing Arrays</h3></div></div>
 
177
<div></div>
 
178
</div>
 
179
<p>  Now, we can run some queries on the table.
 
180
  First, we show how to access a single element of an array at a time.
 
181
  This query retrieves the names of the employees whose pay changed in
 
182
  the second quarter:
 
183
     
 
184
</p>
 
185
<pre class="programlisting">SELECT name FROM sal_emp WHERE pay_by_quarter[1] &lt;&gt; pay_by_quarter[2];
 
186
 
 
187
 name
 
188
-------
 
189
 Carol
 
190
(1 row)</pre>
 
191
<p>
 
192
 
 
193
  The array subscript numbers are written within square brackets.
 
194
  By default <span class="productname">PostgreSQL</span> uses the
 
195
  one-based numbering convention for arrays, that is,
 
196
  an array of <i class="replaceable"><tt>n</tt></i> elements starts with <tt class="literal">array[1]</tt> and
 
197
  ends with <tt class="literal">array[<i class="replaceable"><tt>n</tt></i>]</tt>.
 
198
 </p>
 
199
<p>  This query retrieves the third quarter pay of all employees:
 
200
     
 
201
</p>
 
202
<pre class="programlisting">SELECT pay_by_quarter[3] FROM sal_emp;
 
203
 
 
204
 pay_by_quarter
 
205
----------------
 
206
          10000
 
207
          25000
 
208
(2 rows)</pre>
 
209
<p>
 
210
 </p>
 
211
<p>  We can also access arbitrary rectangular slices of an array, or
 
212
  subarrays.  An array slice is denoted by writing
 
213
  <tt class="literal"><i class="replaceable"><tt>lower-bound</tt></i>:<i class="replaceable"><tt>upper-bound</tt></i></tt>
 
214
  for one or more array dimensions.  For example, this query retrieves the first
 
215
  item on Bill's schedule for the first two days of the week:
 
216
     
 
217
</p>
 
218
<pre class="programlisting">SELECT schedule[1:2][1:1] FROM sal_emp WHERE name = 'Bill';
 
219
 
 
220
        schedule
 
221
------------------------
 
222
 {{meeting},{training}}
 
223
(1 row)</pre>
 
224
<p>
 
225
 
 
226
  We could also have written
 
227
 
 
228
</p>
 
229
<pre class="programlisting">SELECT schedule[1:2][1] FROM sal_emp WHERE name = 'Bill';</pre>
 
230
<p>
 
231
 
 
232
  with the same result.  An array subscripting operation is always taken to
 
233
  represent an array slice if any of the subscripts are written in the form
 
234
  <tt class="literal"><i class="replaceable"><tt>lower</tt></i>:<i class="replaceable"><tt>upper</tt></i></tt>.
 
235
  A lower bound of 1 is assumed for any subscript where only one value
 
236
  is specified, as in this example:
 
237
</p>
 
238
<pre class="programlisting">SELECT schedule[1:2][2] FROM sal_emp WHERE name = 'Bill';
 
239
 
 
240
                 schedule
 
241
-------------------------------------------
 
242
 {{meeting,lunch},{training,presentation}}
 
243
(1 row)</pre>
 
244
<p>
 
245
 </p>
 
246
<p>  The current dimensions of any array value can be retrieved with the
 
247
  <tt class="function">array_dims</tt> function:
 
248
 
 
249
</p>
 
250
<pre class="programlisting">SELECT array_dims(schedule) FROM sal_emp WHERE name = 'Carol';
 
251
 
 
252
 array_dims
 
253
------------
 
254
 [1:2][1:1]
 
255
(1 row)</pre>
 
256
<p>
 
257
 
 
258
  <tt class="function">array_dims</tt> produces a <tt class="type">text</tt> result,
 
259
  which is convenient for people to read but perhaps not so convenient
 
260
  for programs.  Dimensions can also be retrieved with
 
261
  <tt class="function">array_upper</tt> and <tt class="function">array_lower</tt>,
 
262
  which return the upper and lower bound of a
 
263
  specified array dimension, respectively.
 
264
 
 
265
</p>
 
266
<pre class="programlisting">SELECT array_upper(schedule, 1) FROM sal_emp WHERE name = 'Carol';
 
267
 
 
268
 array_upper
 
269
-------------
 
270
           2
 
271
(1 row)</pre>
 
272
<p>
 
273
 </p>
 
274
</div>
 
275
<div class="sect2" lang="en">
 
276
<div class="titlepage">
 
277
<div><div><h3 class="title">
 
278
<a name="id2538000"></a>8.10.4.�Modifying Arrays</h3></div></div>
 
279
<div></div>
 
280
</div>
 
281
<p>  An array value can be replaced completely:
 
282
 
 
283
</p>
 
284
<pre class="programlisting">UPDATE sal_emp SET pay_by_quarter = '{25000,25000,27000,27000}'
 
285
    WHERE name = 'Carol';</pre>
 
286
<p>
 
287
 
 
288
  or using the <tt class="literal">ARRAY</tt> expression syntax:
 
289
 
 
290
</p>
 
291
<pre class="programlisting">UPDATE sal_emp SET pay_by_quarter = ARRAY[25000,25000,27000,27000]
 
292
    WHERE name = 'Carol';</pre>
 
293
<p>
 
294
 
 
295
  An array may also be updated at a single element:
 
296
 
 
297
</p>
 
298
<pre class="programlisting">UPDATE sal_emp SET pay_by_quarter[4] = 15000
 
299
    WHERE name = 'Bill';</pre>
 
300
<p>
 
301
 
 
302
  or updated in a slice:
 
303
 
 
304
</p>
 
305
<pre class="programlisting">UPDATE sal_emp SET pay_by_quarter[1:2] = '{27000,27000}'
 
306
    WHERE name = 'Carol';</pre>
 
307
<p>
 
308
 
 
309
 </p>
 
310
<p>  A stored array value can be enlarged by assigning to an element adjacent to
 
311
  those already present, or by assigning to a slice that is adjacent
 
312
  to or overlaps the data already present.  For example, if array
 
313
  <tt class="literal">myarray</tt> currently has 4 elements, it will have five
 
314
  elements after an update that assigns to <tt class="literal">myarray[5]</tt>.
 
315
  Currently, enlargement in this fashion is only allowed for one-dimensional
 
316
  arrays, not multidimensional arrays.
 
317
 </p>
 
318
<p>  Array slice assignment allows creation of arrays that do not use one-based
 
319
  subscripts.  For example one might assign to <tt class="literal">myarray[-2:7]</tt> to
 
320
  create an array with subscript values running from -2 to 7.
 
321
 </p>
 
322
<p>  New array values can also be constructed by using the concatenation operator,
 
323
  <tt class="literal">||</tt>.
 
324
</p>
 
325
<pre class="programlisting">SELECT ARRAY[1,2] || ARRAY[3,4];
 
326
 ?column?
 
327
-----------
 
328
 {1,2,3,4}
 
329
(1 row)
 
330
 
 
331
SELECT ARRAY[5,6] || ARRAY[[1,2],[3,4]];
 
332
      ?column?
 
333
---------------------
 
334
 {{5,6},{1,2},{3,4}}
 
335
(1 row)</pre>
 
336
<p>
 
337
 </p>
 
338
<p>  The concatenation operator allows a single element to be pushed on to the
 
339
  beginning or end of a one-dimensional array. It also accepts two
 
340
  <i class="replaceable"><tt>N</tt></i>-dimensional arrays, or an <i class="replaceable"><tt>N</tt></i>-dimensional
 
341
  and an <i class="replaceable"><tt>N+1</tt></i>-dimensional array.
 
342
 </p>
 
343
<p>  When a single element is pushed on to the beginning of a one-dimensional
 
344
  array, the result is an array with a lower bound subscript equal to
 
345
  the right-hand operand's lower bound subscript, minus one. When a single
 
346
  element is pushed on to the end of a one-dimensional array, the result is
 
347
  an array retaining the lower bound of the left-hand operand. For example:
 
348
</p>
 
349
<pre class="programlisting">SELECT array_dims(1 || ARRAY[2,3]);
 
350
 array_dims
 
351
------------
 
352
 [0:2]
 
353
(1 row)
 
354
 
 
355
SELECT array_dims(ARRAY[1,2] || 3);
 
356
 array_dims
 
357
------------
 
358
 [1:3]
 
359
(1 row)</pre>
 
360
<p>
 
361
 </p>
 
362
<p>  When two arrays with an equal number of dimensions are concatenated, the
 
363
  result retains the lower bound subscript of the left-hand operand's outer
 
364
  dimension. The result is an array comprising every element of the left-hand
 
365
  operand followed by every element of the right-hand operand. For example:
 
366
</p>
 
367
<pre class="programlisting">SELECT array_dims(ARRAY[1,2] || ARRAY[3,4,5]);
 
368
 array_dims
 
369
------------
 
370
 [1:5]
 
371
(1 row)
 
372
 
 
373
SELECT array_dims(ARRAY[[1,2],[3,4]] || ARRAY[[5,6],[7,8],[9,0]]);
 
374
 array_dims
 
375
------------
 
376
 [1:5][1:2]
 
377
(1 row)</pre>
 
378
<p>
 
379
 </p>
 
380
<p>  When an <i class="replaceable"><tt>N</tt></i>-dimensional array is pushed on to the beginning
 
381
  or end of an <i class="replaceable"><tt>N+1</tt></i>-dimensional array, the result is
 
382
  analogous to the element-array case above. Each <i class="replaceable"><tt>N</tt></i>-dimensional
 
383
  sub-array is essentially an element of the <i class="replaceable"><tt>N+1</tt></i>-dimensional
 
384
  array's outer dimension. For example:
 
385
</p>
 
386
<pre class="programlisting">SELECT array_dims(ARRAY[1,2] || ARRAY[[3,4],[5,6]]);
 
387
 array_dims
 
388
------------
 
389
 [0:2][1:2]
 
390
(1 row)</pre>
 
391
<p>
 
392
 </p>
 
393
<p>  An array can also be constructed by using the functions
 
394
  <tt class="function">array_prepend</tt>, <tt class="function">array_append</tt>,
 
395
  or <tt class="function">array_cat</tt>. The first two only support one-dimensional
 
396
  arrays, but <tt class="function">array_cat</tt> supports multidimensional arrays.
 
397
 
 
398
  Note that the concatenation operator discussed above is preferred over
 
399
  direct use of these functions. In fact, the functions are primarily for use
 
400
  in implementing the concatenation operator. However, they may be directly
 
401
  useful in the creation of user-defined aggregates. Some examples:
 
402
 
 
403
</p>
 
404
<pre class="programlisting">SELECT array_prepend(1, ARRAY[2,3]);
 
405
 array_prepend
 
406
---------------
 
407
 {1,2,3}
 
408
(1 row)
 
409
 
 
410
SELECT array_append(ARRAY[1,2], 3);
 
411
 array_append
 
412
--------------
 
413
 {1,2,3}
 
414
(1 row)
 
415
 
 
416
SELECT array_cat(ARRAY[1,2], ARRAY[3,4]);
 
417
 array_cat
 
418
-----------
 
419
 {1,2,3,4}
 
420
(1 row)
 
421
 
 
422
SELECT array_cat(ARRAY[[1,2],[3,4]], ARRAY[5,6]);
 
423
      array_cat
 
424
---------------------
 
425
 {{1,2},{3,4},{5,6}}
 
426
(1 row)
 
427
 
 
428
SELECT array_cat(ARRAY[5,6], ARRAY[[1,2],[3,4]]);
 
429
      array_cat
 
430
---------------------
 
431
 {{5,6},{1,2},{3,4}}</pre>
 
432
<p>
 
433
 </p>
 
434
</div>
 
435
<div class="sect2" lang="en">
 
436
<div class="titlepage">
 
437
<div><div><h3 class="title">
 
438
<a name="id2538219"></a>8.10.5.�Searching in Arrays</h3></div></div>
 
439
<div></div>
 
440
</div>
 
441
<p>  To search for a value in an array, you must check each value of the
 
442
  array. This can be done by hand, if you know the size of the array.
 
443
  For example:
 
444
 
 
445
</p>
 
446
<pre class="programlisting">SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR
 
447
                            pay_by_quarter[2] = 10000 OR
 
448
                            pay_by_quarter[3] = 10000 OR
 
449
                            pay_by_quarter[4] = 10000;</pre>
 
450
<p>
 
451
 
 
452
  However, this quickly becomes tedious for large arrays, and is not
 
453
  helpful if the size of the array is uncertain. An alternative method is
 
454
  described in <a href="functions-comparisons.html" title="9.17.�Row and Array Comparisons">Section�9.17, &#8220;Row and Array Comparisons&#8221;</a>. The above
 
455
  query could be replaced by:
 
456
 
 
457
</p>
 
458
<pre class="programlisting">SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);</pre>
 
459
<p>
 
460
 
 
461
  In addition, you could find rows where the array had all values
 
462
  equal to 10000 with:
 
463
 
 
464
</p>
 
465
<pre class="programlisting">SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter);</pre>
 
466
<p>
 
467
 
 
468
 </p>
 
469
<div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;">
 
470
<h3 class="title">Tip</h3>
 
471
<p>   Arrays are not sets; searching for specific array elements
 
472
   may be a sign of database misdesign.  Consider
 
473
   using a separate table with a row for each item that would be an
 
474
   array element.  This will be easier to search, and is likely to
 
475
   scale up better to large numbers of elements.
 
476
  </p>
 
477
</div>
 
478
</div>
 
479
<div class="sect2" lang="en">
 
480
<div class="titlepage">
 
481
<div><div><h3 class="title">
 
482
<a name="id2538270"></a>8.10.6.�Array Input and Output Syntax</h3></div></div>
 
483
<div></div>
 
484
</div>
 
485
<p>   The external text representation of an array value consists of items that
 
486
   are interpreted according to the I/O conversion rules for the array's
 
487
   element type, plus decoration that indicates the array structure.
 
488
   The decoration consists of curly braces (<tt class="literal">{</tt> and <tt class="literal">}</tt>)
 
489
   around the array value plus delimiter characters between adjacent items.
 
490
   The delimiter character is usually a comma (<tt class="literal">,</tt>) but can be
 
491
   something else: it is determined by the <tt class="literal">typdelim</tt> setting
 
492
   for the array's element type.  (Among the standard data types provided
 
493
   in the <span class="productname">PostgreSQL</span> distribution, type
 
494
   <tt class="literal">box</tt> uses a semicolon (<tt class="literal">;</tt>) but all the others
 
495
   use comma.)  In a multidimensional array, each dimension (row, plane,
 
496
   cube, etc.) gets its own level of curly braces, and delimiters
 
497
   must be written between adjacent curly-braced entities of the same level.
 
498
  </p>
 
499
<p>   The array output routine will put double quotes around element values
 
500
   if they are empty strings or contain curly braces, delimiter characters,
 
501
   double quotes, backslashes, or white space.  Double quotes and backslashes
 
502
   embedded in element values will be backslash-escaped.  For numeric
 
503
   data types it is safe to assume that double quotes will never appear, but
 
504
   for textual data types one should be prepared to cope with either presence
 
505
   or absence of quotes.  (This is a change in behavior from pre-7.2
 
506
   <span class="productname">PostgreSQL</span> releases.)
 
507
  </p>
 
508
<p>   By default, the lower bound index value of an array's dimensions is
 
509
   set to one. If any of an array's dimensions has a lower bound index not
 
510
   equal to one, an additional decoration that indicates the actual
 
511
   array dimensions will precede the array structure decoration.
 
512
   The decoration consists of square braces (<tt class="literal">[</tt> and <tt class="literal">]</tt>)
 
513
   around each array dimension's lower and upper bound indicies, plus
 
514
   a colon (<tt class="literal">:</tt>) delimiter character inbetween. Delimiting the
 
515
   array dimension decoration from the array structure decoration is a
 
516
   single assignment operator (<tt class="literal">=</tt>). For example:
 
517
</p>
 
518
<pre class="programlisting">SELECT 1 || ARRAY[2,3] AS array;
 
519
 
 
520
     array
 
521
---------------
 
522
 [0:2]={1,2,3}
 
523
(1 row)
 
524
 
 
525
SELECT ARRAY[1,2] || ARRAY[[3,4]] AS array;
 
526
 
 
527
          array
 
528
--------------------------
 
529
 [0:1][1:2]={{1,2},{3,4}}
 
530
(1 row)</pre>
 
531
<p>
 
532
  </p>
 
533
<p>   In a similar fashion, an array with non-default indicies may be specified
 
534
   using the same literal syntax. For example:
 
535
</p>
 
536
<pre class="programlisting">SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
 
537
 FROM (SELECT '[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[] AS f1) AS ss;
 
538
 
 
539
 e1 | e2
 
540
----+----
 
541
  1 |  6
 
542
(1 row)</pre>
 
543
<p>
 
544
  </p>
 
545
<p>   As shown previously, when writing an array value you may write double
 
546
   quotes around any individual array element. You <span class="emphasis"><em>must</em></span> do so
 
547
   if the element value would otherwise confuse the array-value parser.
 
548
   For example, elements containing curly braces, commas (or whatever the
 
549
   delimiter character is), double quotes, backslashes, or leading or trailing
 
550
   whitespace must be double-quoted.  To put a double quote or backslash in a
 
551
   quoted array element value, precede it with a backslash. Alternatively, you
 
552
   can use backslash-escaping to protect all data characters that would
 
553
   otherwise be taken as array syntax.
 
554
  </p>
 
555
<p>   You may write whitespace before a left brace or after a right
 
556
   brace. You may also write whitespace before or after any individual item
 
557
   string. In all of these cases the whitespace will be ignored. However,
 
558
   whitespace within double quoted elements, or surrounded on both sides by
 
559
   non-whitespace characters of an element, are not ignored.
 
560
  </p>
 
561
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
 
562
<h3 class="title">Note</h3>
 
563
<p>   Remember that what you write in an SQL command will first be interpreted
 
564
   as a string literal, and then as an array.  This doubles the number of
 
565
   backslashes you need.  For example, to insert a <tt class="type">text</tt> array
 
566
   value containing a backslash and a double quote, you'd need to write
 
567
</p>
 
568
<pre class="programlisting">INSERT ... VALUES ('{"\\\\","\\""}');</pre>
 
569
<p>
 
570
   The string-literal processor removes one level of backslashes, so that
 
571
   what arrives at the array-value parser looks like <tt class="literal">{"\\","\""}</tt>.
 
572
   In turn, the strings fed to the <tt class="type">text</tt> data type's input routine
 
573
   become <tt class="literal">\</tt> and <tt class="literal">"</tt> respectively.  (If we were working
 
574
   with a data type whose input routine also treated backslashes specially,
 
575
   <tt class="type">bytea</tt> for example, we might need as many as eight backslashes
 
576
   in the command to get one backslash into the stored array element.)
 
577
  </p>
 
578
</div>
 
579
<div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;">
 
580
<h3 class="title">Tip</h3>
 
581
<p>   The <tt class="literal">ARRAY</tt> constructor syntax (see
 
582
   <a href="sql-expressions.html#sql-syntax-array-constructors" title="4.2.10.�Array Constructors">Section�4.2.10, &#8220;Array Constructors&#8221;</a>) is often easier to work
 
583
   with than the array-literal syntax when writing array values in SQL
 
584
   commands. In <tt class="literal">ARRAY</tt>, individual element values are written the
 
585
   same way they would be written when not members of an array.
 
586
  </p>
 
587
</div>
 
588
</div>
 
589
</div></body>
 
590
</html>