-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays.html
More file actions
962 lines (895 loc) · 54.1 KB
/
arrays.html
File metadata and controls
962 lines (895 loc) · 54.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>8.15. 配列</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="datatype-json.html" title="8.14. JSONデータ型" /><link rel="next" href="rowtypes.html" title="8.16. 複合型" /><meta name="viewport" content="width=device-width,initial-scale=1.0" /></head><body id="docContent" class="container-fluid col-10"><div class="other_version"><a href="https://www.postgresql.jp/document/">バージョンごとのドキュメント一覧</a></div><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="4" align="center"><a accesskey="h" href="index.html">PostgreSQL 18.3文書</a></th></tr><tr><td width="10%" align="left"></td><td width="10%" align="left"></td><td width="60%" align="center"><a href="datatype.html" title="第8章 データ型">第8章 データ型</a></td><td width="20%" align="right"><div class="actions"><a class="issue" title="github" href="https://github.com/pgsql-jp/jpug-doc/issues/new?template=bug_report.yml&what-happened=version 18.3 : arrays.html">誤訳等の報告
</a></div></td></tr><tr><td width="10%" align="left"><a accesskey="p" href="datatype-json.html" title="8.14. JSONデータ型">前へ</a> </td><td width="10%" align="left"><a accesskey="u" href="datatype.html" title="第8章 データ型">上へ</a></td><td width="60%" align="center">8.15. 配列</td><td width="20%" align="right"> <a accesskey="n" href="rowtypes.html" title="8.16. 複合型">次へ</a></td></tr></table><hr /></div><div class="sect1" id="ARRAYS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">8.15. 配列 <a href="#ARRAYS" class="id_link">#</a></h2></div></div></div><span class="original">
<title>Arrays</title>
</span><a id="id-1.5.7.25.2" class="indexterm"></a><a id="id-1.5.7.25.3" class="indexterm"></a><p>
<span class="original">
<productname>PostgreSQL</productname> allows columns of a table to be
defined as variable-length multidimensional arrays. Arrays of any
built-in or user-defined base type, enum type, composite type, range type,
or domain can be created.
</span>
<span class="productname">PostgreSQL</span>ではテーブルの列を可変長多次元配列として定義できます。
あらゆる組み込み型あるいはユーザ定義の基本型、列挙型、複合型、範囲型そしてドメインの配列も作成可能です。
</p><div class="sect2" id="ARRAYS-DECLARATION"><div class="titlepage"><div><div><h3 class="title">8.15.1. 配列型の宣言 <a href="#ARRAYS-DECLARATION" class="id_link">#</a></h3></div></div></div><span class="original">
<title>Declaration of Array Types</title>
</span><a id="id-1.5.7.25.5.2" class="indexterm"></a><a id="id-1.5.7.25.5.3" class="indexterm"></a><p>
<span class="original">
To illustrate the use of array types, we create this table:
</span>
実際に配列の使い方を説明するために、次のテーブルを作成します。
</p><pre class="programlisting">
CREATE TABLE sal_emp (
name text,
pay_by_quarter integer[],
schedule text[][]
);
</pre><p>
<span class="original">
As shown, an array data type is named by appending square brackets
(<literal>[]</literal>) to the data type name of the array elements. The
above command will create a table named
<structname>sal_emp</structname> with a column of type
<type>text</type> (<structfield>name</structfield>), a
one-dimensional array of type <type>integer</type>
(<structfield>pay_by_quarter</structfield>), which represents the
employee's salary by quarter, and a two-dimensional array of
<type>text</type> (<structfield>schedule</structfield>), which
represents the employee's weekly schedule.
</span>
見ておわかりのように配列データ型は配列要素のデータ型の名前に大括弧(<code class="literal">[]</code>)を付けて指定します。
このコマンドは<code class="type">text</code>型文字列(<code class="structfield">name</code>)、従業員の四半期の給与を保存する<code class="type">integer</code>型の一次元配列(<code class="structfield">pay_by_quarter</code>)、そして従業員の週間スケジュールを保存する<code class="type">text</code>型の二次元配列(<code class="structfield">schedule</code>)の列を持つ<code class="structname">sal_emp</code>という名前のテーブルを作成します。
</p><p>
<span class="original">
The syntax for <command>CREATE TABLE</command> allows the exact size of
arrays to be specified, for example:
</span>
<code class="command">CREATE TABLE</code>構文で指定する配列の正確な大きさを指定することができます。
</p><pre class="programlisting">
CREATE TABLE tictactoe (
squares integer[3][3]
);
</pre><p>
<span class="original">
However, the current implementation ignores any supplied array size
limits, i.e., the behavior is the same as for arrays of unspecified
length.
</span>
とは言っても現在の実装では指定された配列の大きさの制限を無視します。
つまり、長さの指定がない配列と同じ振舞いをします。
</p><p>
<span class="original">
The current implementation does not enforce the declared
number of dimensions either. Arrays of a particular element type are
all considered to be of the same type, regardless of size or number
of dimensions. So, declaring the array size or number of dimensions in
<command>CREATE TABLE</command> is simply documentation; it does not
affect run-time behavior.
</span>
現在の実装では次元数の宣言も強制していません。
特定の要素型の配列はすべて大きさあるいは次元数とは無関係に同じ型とみなされます。
ですから<code class="command">CREATE TABLE</code>で配列の大きさや次元数を宣言することは、単なる説明です。
実行時の動作に影響を及ぼしません。
</p><p>
<span class="original">
An alternative syntax, which conforms to the SQL standard by using
the keyword <literal>ARRAY</literal>, can be used for one-dimensional arrays.
<structfield>pay_by_quarter</structfield> could have been defined
as:
</span>
標準SQLに準拠している、<code class="literal">ARRAY</code>キーワードを使用したもう1つの構文を一次元配列に使うことができます。
<code class="structfield">pay_by_quarter</code>を次のように定義することもできます。
</p><pre class="programlisting">
pay_by_quarter integer ARRAY[4],
</pre><p>
<span class="original">
Or, if no array size is to be specified:
</span>
または、もし配列の大きさが指定されない場合は次のようになります。
</p><pre class="programlisting">
pay_by_quarter integer ARRAY,
</pre><p>
<span class="original">
As before, however, <productname>PostgreSQL</productname> does not enforce the
size restriction in any case.
</span>
しかし、前で触れたように<span class="productname">PostgreSQL</span>はどんな場合でも大きさの制限を強要しません。
</p></div><div class="sect2" id="ARRAYS-INPUT"><div class="titlepage"><div><div><h3 class="title">8.15.2. 配列の値の入力 <a href="#ARRAYS-INPUT" class="id_link">#</a></h3></div></div></div><span class="original">
<title>Array Value Input</title>
</span><a id="id-1.5.7.25.6.2" class="indexterm"></a><a id="id-1.5.7.25.6.3" class="indexterm"></a><p>
<span class="original">
To write an array value as a literal constant, enclose the element
values within curly braces and separate them by commas. (If you
know C, this is not unlike the C syntax for initializing
structures.) You can put double quotes around any element value,
and must do so if it contains commas or curly braces. (More
details appear below.) Thus, the general format of an array
constant is the following:
</span>
リテラル定数として配列の値を書き込むには、その要素の値を中括弧で囲み、それぞれの要素の値をカンマで区切ります。
(C言語を知っているならば、構造体を初期化するための構文のようなものと考えてください。)
要素の値を二重引用符でくくることもでき、カンマもしくは中括弧がある時は必ずそのように書かなければなりません。
(詳細は以下に出てきます。)
したがって配列定数の一般的書式は次のようになります。
</p><pre class="synopsis">
'{ <em class="replaceable"><code>val1</code></em> <em class="replaceable"><code>delim</code></em> <em class="replaceable"><code>val2</code></em> <em class="replaceable"><code>delim</code></em> ... }'
</pre><p>
<span class="original">
where <replaceable>delim</replaceable> is the delimiter character
for the type, as recorded in its <literal>pg_type</literal> entry.
Among the standard data types provided in the
<productname>PostgreSQL</productname> distribution, all use a comma
(<literal>,</literal>), except for type <type>box</type> which uses a semicolon
(<literal>;</literal>). Each <replaceable>val</replaceable> is
either a constant of the array element type, or a subarray. An example
of an array constant is:
</span>
ここで<em class="replaceable"><code>delim</code></em>はその<code class="literal">pg_type</code>項目に記録されている型の区切り文字です。
<span class="productname">PostgreSQL</span>配布物で提供されている標準データ型の内、セミコロン(<code class="literal">;</code>)を使用する<code class="type">box</code>型を除き、すべてはカンマ(<code class="literal">,</code>)を使います。
それぞれの<em class="replaceable"><code>val</code></em>は配列要素の型の定数か副配列です。
配列定数の例を以下に示します。
</p><pre class="programlisting">
'{{1,2,3},{4,5,6},{7,8,9}}'
</pre><p>
<span class="original">
This constant is a two-dimensional, 3-by-3 array consisting of
three subarrays of integers.
</span>
この定数は整数の3つの副配列を持っている二次元3×3の配列です。
</p><p>
<span class="original">
To set an element of an array constant to NULL, write <literal>NULL</literal>
for the element value. (Any upper- or lower-case variant of
<literal>NULL</literal> will do.) If you want an actual string value
<quote>NULL</quote>, you must put double quotes around it.
</span>
配列定数の要素をNULLとするためには、その要素値に<code class="literal">NULL</code>と記載してください。
(<code class="literal">NULL</code>を大文字で書いても小文字で書いても構いません。)
<span class="quote">「<span class="quote">NULL</span>」</span>という文字列値を指定したければ、二重引用符でくくって記載しなければなりません。
</p><p>
<span class="original">
(These kinds of array constants are actually only a special case of
the generic type constants discussed in <xref
linkend="sql-syntax-constants-generic"/>. The constant is initially
treated as a string and passed to the array input conversion
routine. An explicit type specification might be necessary.)
</span>
(この種の配列定数は実際<a class="xref" href="sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS-GENERIC" title="4.1.2.7. 他の型の定数">4.1.2.7</a>で説明されている一般型定数の特別の場合に過ぎません。
この定数は元々文字列として扱われていて配列入力ルーチンに渡されます。
明示的な型指定が必要かもしれません。)
</p><p>
<span class="original">
Now we can show some <command>INSERT</command> statements:
</span>
では、<code class="command">INSERT</code>文をいくつか紹介します。
</p><pre class="programlisting">
INSERT INTO sal_emp
VALUES ('Bill',
'{10000, 10000, 10000, 10000}',
'{{"meeting", "lunch"}, {"training", "presentation"}}');
INSERT INTO sal_emp
VALUES ('Carol',
'{20000, 25000, 25000, 25000}',
'{{"breakfast", "consulting"}, {"meeting", "lunch"}}');
</pre><p>
</p><p>
<span class="original">
The result of the previous two inserts looks like this:
</span>
上に記載した2つの挿入文の結果は次のようになります。
</p><pre class="programlisting">
SELECT * FROM sal_emp;
name | pay_by_quarter | schedule
-------+---------------------------+-------------------------------------------
Bill | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows)
</pre><p>
</p><p>
<span class="original">
Multidimensional arrays must have matching extents for each
dimension. A mismatch causes an error, for example:
</span>
多次元配列では、各次元の範囲を合わせなければなりません。
一致しないと以下のようにエラーが発生します。
</p><pre class="programlisting">
INSERT INTO sal_emp
VALUES ('Bill',
'{10000, 10000, 10000, 10000}',
'{{"meeting", "lunch"}, {"meeting"}}');
ERROR: malformed array literal: "{{"meeting", "lunch"}, {"meeting"}}"
DETAIL: Multidimensional arrays must have sub-arrays with matching dimensions.
</pre><p>
</p><p>
<span class="original">
The <literal>ARRAY</literal> constructor syntax can also be used:
</span>
<code class="literal">ARRAY</code>生成子構文も使えます。
</p><pre class="programlisting">
INSERT INTO sal_emp
VALUES ('Bill',
ARRAY[10000, 10000, 10000, 10000],
ARRAY[['meeting', 'lunch'], ['training', 'presentation']]);
INSERT INTO sal_emp
VALUES ('Carol',
ARRAY[20000, 25000, 25000, 25000],
ARRAY[['breakfast', 'consulting'], ['meeting', 'lunch']]);
</pre><p>
<span class="original">
Notice that the array elements are ordinary SQL constants or
expressions; for instance, string literals are single quoted, instead of
double quoted as they would be in an array literal. The <literal>ARRAY</literal>
constructor syntax is discussed in more detail in
<xref linkend="sql-syntax-array-constructors"/>.
</span>
配列要素は通常のSQL定数もしくは演算式であることに注意してください。
例えば文字列リテラルは配列リテラルと同様、二重引用符ではなく単一引用符でくくられます。
<code class="literal">ARRAY</code>生成子構文は<a class="xref" href="sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS" title="4.2.12. 配列コンストラクタ">4.2.12</a>により詳しい説明があります。
</p></div><div class="sect2" id="ARRAYS-ACCESSING"><div class="titlepage"><div><div><h3 class="title">8.15.3. 配列へのアクセス <a href="#ARRAYS-ACCESSING" class="id_link">#</a></h3></div></div></div><span class="original">
<title>Accessing Arrays</title>
</span><a id="id-1.5.7.25.7.2" class="indexterm"></a><a id="id-1.5.7.25.7.3" class="indexterm"></a><p>
<span class="original">
Now, we can run some queries on the table.
First, we show how to access a single element of an array.
This query retrieves the names of the employees whose pay changed in
the second quarter:
</span>
ではテーブルに対していくつかの問い合わせを行ってみましょう。
初めに、配列の単一要素にアクセスする方法を示します。
この問い合わせは第2四半期に給与が更新された従業員の名前を抽出します。
</p><pre class="programlisting">
SELECT name FROM sal_emp WHERE pay_by_quarter[1] <> pay_by_quarter[2];
name
-------
Carol
(1 row)
</pre><p>
<span class="original">
The array subscript numbers are written within square brackets.
By default <productname>PostgreSQL</productname> uses a
one-based numbering convention for arrays, that is,
an array of <replaceable>n</replaceable> elements starts with <literal>array[1]</literal> and
ends with <literal>array[<replaceable>n</replaceable>]</literal>.
</span>
配列の添字番号は大括弧で囲んで記述されます。
デフォルトで<span class="productname">PostgreSQL</span>は配列に対し「1始まり」の振り番規定を採用しています。
つまり要素が<em class="replaceable"><code>n</code></em>個ある配列は<code class="literal">array[1]</code>で始まり、<code class="literal">array[<em class="replaceable"><code>n</code></em>]</code>で終わります。
</p><p>
<span class="original">
This query retrieves the third quarter pay of all employees:
</span>
次の問い合わせは全ての従業員の第3四半期の給与を抽出します。
</p><pre class="programlisting">
SELECT pay_by_quarter[3] FROM sal_emp;
pay_by_quarter
----------------
10000
25000
(2 rows)
</pre><p>
</p><p>
<span class="original">
We can also access arbitrary rectangular slices of an array, or
subarrays. An array slice is denoted by writing
<literal><replaceable>lower-bound</replaceable>:<replaceable>upper-bound</replaceable></literal>
for one or more array dimensions. For example, this query retrieves the first
item on Bill's schedule for the first two days of the week:
</span>
また、配列や副配列の任意の縦方向の部分を切り出すこともできます。
一次元以上の配列についてその一部を表現するには、<code class="literal"><em class="replaceable"><code>lower-bound</code></em>:<em class="replaceable"><code>upper-bound</code></em></code>と記述します。
例えばこの問い合わせはBillのその週の初めの2日に最初何が予定されているかを抽出します。
</p><pre class="programlisting">
SELECT schedule[1:2][1:1] FROM sal_emp WHERE name = 'Bill';
schedule
------------------------
{{meeting},{training}}
(1 row)
</pre><p>
<span class="original">
If any dimension is written as a slice, i.e., contains a colon, then all
dimensions are treated as slices. Any dimension that has only a single
number (no colon) is treated as being from 1
to the number specified. For example, <literal>[2]</literal> is treated as
<literal>[1:2]</literal>, as in this example:
</span>
任意の次元を部分として、つまりコロンを含めて記述すると、すべての次元が部分として扱われます。
単一の番号のみ(コロンを持たない)を持つ次元はすべて、1から指定番号までと扱われます。
例えば、<code class="literal">[2]</code>は以下の例のように <code class="literal">[1:2]</code>と扱われます。
</p><pre class="programlisting">
SELECT schedule[1:2][2] FROM sal_emp WHERE name = 'Bill';
schedule
-------------------------------------------
{{meeting,lunch},{training,presentation}}
(1 row)
</pre><p>
<span class="original">
To avoid confusion with the non-slice case, it's best to use slice syntax
for all dimensions, e.g., <literal>[1:2][1:1]</literal>, not <literal>[2][1:1]</literal>.
</span>
切り出しのない場合と混乱を避けるため、すべての次元に対し切り出し構文を使用することが最善です。
例えば、<code class="literal">[2][1:1]</code>ではなく、<code class="literal">[1:2][1:1]</code>のようにします。
</p><p>
<span class="original">
It is possible to omit the <replaceable>lower-bound</replaceable> and/or
<replaceable>upper-bound</replaceable> of a slice specifier; the missing
bound is replaced by the lower or upper limit of the array's subscripts.
For example:
</span>
切り出し指定子の<em class="replaceable"><code>lower-bound</code></em>、<em class="replaceable"><code>upper-bound</code></em>は省略可能です。省略された上限または下限は、配列の添字の上限または下限で置き換えられます。
例えば、
</p><pre class="programlisting">
SELECT schedule[:2][2:] FROM sal_emp WHERE name = 'Bill';
schedule
------------------------
{{lunch},{presentation}}
(1 row)
SELECT schedule[:][1:1] FROM sal_emp WHERE name = 'Bill';
schedule
------------------------
{{meeting},{training}}
(1 row)
</pre><p>
</p><p>
<span class="original">
An array subscript expression will return null if either the array itself or
any of the subscript expressions are null. Also, null is returned if a
subscript is outside the array bounds (this case does not raise an error).
For example, if <literal>schedule</literal>
currently has the dimensions <literal>[1:3][1:2]</literal> then referencing
<literal>schedule[3][3]</literal> yields NULL. Similarly, an array reference
with the wrong number of subscripts yields a null rather than an error.
</span>
配列自体がNULLもしくはその添字式がNULLとなる場合、配列添字式はNULLを返します。
また、配列の範囲を超える添字の場合もNULLが返されます(この場合はエラーになりません)。
例えば、<code class="literal">schedule</code>が現在<code class="literal">[1:3][1:2]</code>次元であれば、<code class="literal">schedule[3][3]</code>の参照はNULLとなります。
同様にして、添字として間違った値を指定して配列を参照した場合もエラーではなく、NULLが返されます。
</p><p>
<span class="original">
An array slice expression likewise yields null if the array itself or
any of the subscript expressions are null. However, in other
cases such as selecting an array slice that
is completely outside the current array bounds, a slice expression
yields an empty (zero-dimensional) array instead of null. (This
does not match non-slice behavior and is done for historical reasons.)
If the requested slice partially overlaps the array bounds, then it
is silently reduced to just the overlapping region instead of
returning null.
</span>
同様に、部分配列式も配列自体がNULLもしくはその添字式がNULLとなる場合にNULLを返します。
しかし、現在の配列範囲を完全に超えた部分配列を選択する場合では、部分配列式はNULLではなく空の(0次元)の配列を返します。
(これは切り出しなしの動作に一致せず、歴史的理由で行われるものです。)
要求された部分配列が配列の範囲に重なる場合、NULLを返さずに、警告なく重複部分だけに減少させます。
</p><p>
<span class="original">
The current dimensions of any array value can be retrieved with the
<function>array_dims</function> function:
</span>
<code class="function">array_dims</code>関数で任意の配列値の現在の次元を取り出せます。
</p><pre class="programlisting">
SELECT array_dims(schedule) FROM sal_emp WHERE name = 'Carol';
array_dims
------------
[1:2][1:2]
(1 row)
</pre><p>
<span class="original">
<function>array_dims</function> produces a <type>text</type> result,
which is convenient for people to read but perhaps inconvenient
for programs. Dimensions can also be retrieved with
<function>array_upper</function> and <function>array_lower</function>,
which return the upper and lower bound of a
specified array dimension, respectively:
</span>
<code class="function">array_dims</code>関数は<code class="type">text</code>型で結果を返します。
人間が結果を見るためには便利ですが、プログラムにとって都合がよくありません。
次元は<code class="function">array_upper</code>と<code class="function">array_lower</code>でも抽出することができ、それぞれ特定の配列の次元の上限と下限を返します。
</p><pre class="programlisting">
SELECT array_upper(schedule, 1) FROM sal_emp WHERE name = 'Carol';
array_upper
-------------
2
(1 row)
</pre><p>
<span class="original">
<function>array_length</function> will return the length of a specified
array dimension:
</span>
<code class="function">array_length</code>は指定された配列次元の長さを返します。
</p><pre class="programlisting">
SELECT array_length(schedule, 1) FROM sal_emp WHERE name = 'Carol';
array_length
--------------
2
(1 row)
</pre><p>
<span class="original">
<function>cardinality</function> returns the total number of elements in an
array across all dimensions. It is effectively the number of rows a call to
<function>unnest</function> would yield:
</span>
<code class="function">cardinality</code>は配列の全次元に渡る要素の総数を返します。
実質的に<code class="function">unnest</code>の呼び出しで生成される行の数です。
</p><pre class="programlisting">
SELECT cardinality(schedule) FROM sal_emp WHERE name = 'Carol';
cardinality
-------------
4
(1 row)
</pre><p>
</p></div><div class="sect2" id="ARRAYS-MODIFYING"><div class="titlepage"><div><div><h3 class="title">8.15.4. 配列の変更 <a href="#ARRAYS-MODIFYING" class="id_link">#</a></h3></div></div></div><span class="original">
<title>Modifying Arrays</title>
</span><a id="id-1.5.7.25.8.2" class="indexterm"></a><a id="id-1.5.7.25.8.3" class="indexterm"></a><p>
<span class="original">
An array value can be replaced completely:
</span>
配列の値を全て置き換えることができます。
</p><pre class="programlisting">
UPDATE sal_emp SET pay_by_quarter = '{25000,25000,27000,27000}'
WHERE name = 'Carol';
</pre><p>
<span class="original">
or using the <literal>ARRAY</literal> expression syntax:
</span>
もしくは<code class="literal">ARRAY</code>演算構文を用いて次のように書きます。
</p><pre class="programlisting">
UPDATE sal_emp SET pay_by_quarter = ARRAY[25000,25000,27000,27000]
WHERE name = 'Carol';
</pre><p>
<span class="original">
An array can also be updated at a single element:
</span>
配列の1つの要素を更新することも可能です。
</p><pre class="programlisting">
UPDATE sal_emp SET pay_by_quarter[4] = 15000
WHERE name = 'Bill';
</pre><p>
<span class="original">
or updated in a slice:
</span>
あるいは一部分の更新も可能です。
</p><pre class="programlisting">
UPDATE sal_emp SET pay_by_quarter[1:2] = '{27000,27000}'
WHERE name = 'Carol';
</pre><p>
<span class="original">
The slice syntaxes with omitted <replaceable>lower-bound</replaceable> and/or
<replaceable>upper-bound</replaceable> can be used too, but only when
updating an array value that is not NULL or zero-dimensional (otherwise,
there is no existing subscript limit to substitute).
</span>
<em class="replaceable"><code>lower-bound</code></em>や<em class="replaceable"><code>upper-bound</code></em>が省略された切り出し構文も使用可能ですが、NULLや0次元でない配列の値を更新する場合に限ります(さもなければ、置き換えるべき添字の上限、下限が存在しません)。
</p><p>
<span class="original">
A stored array value can be enlarged by assigning to elements not already
present. Any positions between those previously present and the newly
assigned elements will be filled with nulls. For example, if array
<literal>myarray</literal> currently has 4 elements, it will have six
elements after an update that assigns to <literal>myarray[6]</literal>;
<literal>myarray[5]</literal> will contain null.
Currently, enlargement in this fashion is only allowed for one-dimensional
arrays, not multidimensional arrays.
</span>
保存されている配列の値は、存在しない要素に代入することで拡張することができます。
過去に存在した位置と新しく代入された位置との間はNULLで埋められます。
例えば、現在配列<code class="literal">myarray</code>の要素数が4の場合、<code class="literal">myarray[6]</code>を割り当てる更新の後6要素を持つことなり、<code class="literal">myarray[5]</code>はNULLを含みます。
現在、こうした方法での拡張は、1次元配列でのみ許されます。
多次元配列では行うことができません。
</p><p>
<span class="original">
Subscripted assignment allows creation of arrays that do not use one-based
subscripts. For example one might assign to <literal>myarray[-2:7]</literal> to
create an array with subscript values from -2 to 7.
</span>
添字指定の代入で1始まり以外の添字がある配列を作れます。
例えば添字が-2から7までの値を持つ配列を<code class="literal">array[-2:7]</code>で指定できます。
</p><p>
<span class="original">
New array values can also be constructed using the concatenation operator,
<literal>||</literal>:
</span>
新規の配列の値は連結演算子<code class="literal">||</code>を用いて作成することもできます。
</p><pre class="programlisting">
SELECT ARRAY[1,2] || ARRAY[3,4];
?column?
-----------
{1,2,3,4}
(1 row)
SELECT ARRAY[5,6] || ARRAY[[1,2],[3,4]];
?column?
---------------------
{{5,6},{1,2},{3,4}}
(1 row)
</pre><p>
</p><p>
<span class="original">
The concatenation operator allows a single element to be pushed onto the
beginning or end of a one-dimensional array. It also accepts two
<replaceable>N</replaceable>-dimensional arrays, or an <replaceable>N</replaceable>-dimensional
and an <replaceable>N+1</replaceable>-dimensional array.
</span>
連結演算子を使うと、一次元配列の最初もしくは最後に1つの要素を押し込むことができます。
さらには2つの<em class="replaceable"><code>N</code></em>-次元配列もしくは<em class="replaceable"><code>N</code></em>-次元配列と<em class="replaceable"><code>N+1</code></em>-次元配列にも対応しています。
</p><p>
<span class="original">
When a single element is pushed onto either the beginning or end of a
one-dimensional array, the result is an array with the same lower bound
subscript as the array operand. For example:
</span>
1つの要素が1次元配列の先頭や末尾に押し込まれた時、結果は配列演算項目と同じ下限添字を持つ配列となります。
以下に例を示します。
</p><pre class="programlisting">
SELECT array_dims(1 || '[0:1]={2,3}'::int[]);
array_dims
------------
[0:2]
(1 row)
SELECT array_dims(ARRAY[1,2] || 3);
array_dims
------------
[1:3]
(1 row)
</pre><p>
</p><p>
<span class="original">
When two arrays with an equal number of dimensions are concatenated, the
result retains the lower bound subscript of the left-hand operand's outer
dimension. The result is an array comprising every element of the left-hand
operand followed by every element of the right-hand operand. For example:
</span>
等しい次元を持った2つの配列が連結された場合、結果は左側演算項目の外側の次元の下限添字を引き継ぎます。
結果は右側被演算子のすべての要素に左側被演算子が続いた配列となります。
例を挙げます。
</p><pre class="programlisting">
SELECT array_dims(ARRAY[1,2] || ARRAY[3,4,5]);
array_dims
------------
[1:5]
(1 row)
SELECT array_dims(ARRAY[[1,2],[3,4]] || ARRAY[[5,6],[7,8],[9,0]]);
array_dims
------------
[1:5][1:2]
(1 row)
</pre><p>
</p><p>
<span class="original">
When an <replaceable>N</replaceable>-dimensional array is pushed onto the beginning
or end of an <replaceable>N+1</replaceable>-dimensional array, the result is
analogous to the element-array case above. Each <replaceable>N</replaceable>-dimensional
sub-array is essentially an element of the <replaceable>N+1</replaceable>-dimensional
array's outer dimension. For example:
</span>
<em class="replaceable"><code>N</code></em>-次元配列が<em class="replaceable"><code>N+1</code></em>-次元配列の最初または最後に押し込まれると、結果は上記と似通った要素配列になります。
それぞれの<em class="replaceable"><code>N</code></em>-次元副配列は本質的に<em class="replaceable"><code>N+1</code></em>-次元配列の外側の次元の要素となります。
例を挙げます。
</p><pre class="programlisting">
SELECT array_dims(ARRAY[1,2] || ARRAY[[3,4],[5,6]]);
array_dims
------------
[1:3][1:2]
(1 row)
</pre><p>
</p><p>
<span class="original">
An array can also be constructed by using the functions
<function>array_prepend</function>, <function>array_append</function>,
or <function>array_cat</function>. The first two only support one-dimensional
arrays, but <function>array_cat</function> supports multidimensional arrays.
Some examples:
</span>
配列は<code class="function">array_prepend</code>、<code class="function">array_append</code>、もしくは<code class="function">array_cat</code>を使って構築することもできます。
初めの2つは一次元配列にしか対応していませんが、<code class="function">array_cat</code>は多次元配列でも使えます。
例を挙げます。
</p><pre class="programlisting">
SELECT array_prepend(1, ARRAY[2,3]);
array_prepend
---------------
{1,2,3}
(1 row)
SELECT array_append(ARRAY[1,2], 3);
array_append
--------------
{1,2,3}
(1 row)
SELECT array_cat(ARRAY[1,2], ARRAY[3,4]);
array_cat
-----------
{1,2,3,4}
(1 row)
SELECT array_cat(ARRAY[[1,2],[3,4]], ARRAY[5,6]);
array_cat
---------------------
{{1,2},{3,4},{5,6}}
(1 row)
SELECT array_cat(ARRAY[5,6], ARRAY[[1,2],[3,4]]);
array_cat
---------------------
{{5,6},{1,2},{3,4}}
</pre><p>
</p><p>
<span class="original">
In simple cases, the concatenation operator discussed above is preferred
over direct use of these functions. However, because the concatenation
operator is overloaded to serve all three cases, there are situations where
use of one of the functions is helpful to avoid ambiguity. For example
consider:
</span>
単純な状況では、上で説明した連結演算子はそれぞれの関数を直接実行することよりも望ましいです。
とは言っても、連結演算子は3つの場合すべてに対応するようオーバーロードされていますので、その関数の1つを使うとあいまいさを避けるのに役立つ場合があります。
例えば、以下のような状況を考えてください。
</p><pre class="programlisting">
<span class="original">
SELECT ARRAY[1, 2] || '{3, 4}'; &#45;- the untyped literal is taken as an array
</span>
SELECT ARRAY[1, 2] || '{3, 4}'; -- 型指定のないリテラルは配列と見なされる
?column?
-----------
{1,2,3,4}
<span class="original">
SELECT ARRAY[1, 2] || '7'; &#45;- so is this one
</span>
SELECT ARRAY[1, 2] || '7'; -- これも同様
ERROR: malformed array literal: "7"
<span class="original">
SELECT ARRAY[1, 2] || NULL; &#45;- so is an undecorated NULL
</span>
SELECT ARRAY[1, 2] || NULL; -- 修飾されていないNULLも同様
?column?
----------
{1,2}
(1 row)
<span class="original">
SELECT array_append(ARRAY[1, 2], NULL); &#45;- this might have been meant
</span>
SELECT array_append(ARRAY[1, 2], NULL); -- これがやりたかった事かも
array_append
--------------
{1,2,NULL}
</pre><p>
<span class="original">
In the examples above, the parser sees an integer array on one side of the
concatenation operator, and a constant of undetermined type on the other.
The heuristic it uses to resolve the constant's type is to assume it's of
the same type as the operator's other input &mdash; in this case,
integer array. So the concatenation operator is presumed to
represent <function>array_cat</function>, not <function>array_append</function>. When
that's the wrong choice, it could be fixed by casting the constant to the
array's element type; but explicit use of <function>array_append</function> might
be a preferable solution.
</span>
上の例では、パーサは連結演算子の一方の側に整数の配列を見つけ、もう一方の側に型の決まらない定数を見つけます。
パーサが定数の型を解決するのに使う発見的手法は、演算子のもう一方の入力と同じ型(この場合には整数の配列)だと仮定することです。
そのため、連結演算子は<code class="function">array_append</code>ではなく、<code class="function">array_cat</code>と推定されます。
これが誤った選択である場合には、定数を配列の要素の型にキャストすることで直せるかもしれません。ですが、<code class="function">array_append</code>を明示的に使うのが好ましい解決法であるかもしれません。
</p></div><div class="sect2" id="ARRAYS-SEARCHING"><div class="titlepage"><div><div><h3 class="title">8.15.5. 配列内の検索 <a href="#ARRAYS-SEARCHING" class="id_link">#</a></h3></div></div></div><span class="original">
<title>Searching in Arrays</title>
</span><a id="id-1.5.7.25.9.2" class="indexterm"></a><a id="id-1.5.7.25.9.3" class="indexterm"></a><p>
<span class="original">
To search for a value in an array, each value must be checked.
This can be done manually, if you know the size of the array.
For example:
</span>
配列内のある値を検索するにはそれぞれの値が検証されなければなりません。
もし配列の大きさがわかっているならば手作業でも検索できます。
例を挙げます。
</p><pre class="programlisting">
SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR
pay_by_quarter[2] = 10000 OR
pay_by_quarter[3] = 10000 OR
pay_by_quarter[4] = 10000;
</pre><p>
<span class="original">
However, this quickly becomes tedious for large arrays, and is not
helpful if the size of the array is unknown. An alternative method is
described in <xref linkend="functions-comparisons"/>. The above
query could be replaced by:
</span>
とは言ってもこの方法では大きい配列では大変な作業となりますし、配列の大きさが不明な場合この方法は使えません。
代わりになる方法が<a class="xref" href="functions-comparisons.html" title="9.25. 行と配列の比較">9.25</a>で説明されています。
上の問い合わせは以下のように書くことができます。
</p><pre class="programlisting">
SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);
</pre><p>
<span class="original">
In addition, you can find rows where the array has all values
equal to 10000 with:
</span>
さらに配列で行の値が全て10000に等しいものを見つけることもできます。
</p><pre class="programlisting">
SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter);
</pre><p>
</p><p>
<span class="original">
Alternatively, the <function>generate_subscripts</function> function can be used.
For example:
</span>
代わりとして、<code class="function">generate_subscripts</code>関数を使うことができます。
以下はその例です。
</p><pre class="programlisting">
SELECT * FROM
(SELECT pay_by_quarter,
generate_subscripts(pay_by_quarter, 1) AS s
FROM sal_emp) AS foo
WHERE pay_by_quarter[s] = 10000;
</pre><p>
<span class="original">
This function is described in <xref linkend="functions-srf-subscripts"/>.
</span>
この関数は<a class="xref" href="functions-srf.html#FUNCTIONS-SRF-SUBSCRIPTS" title="表9.70 添え字生成関数">表 9.70</a>に記載されています。
</p><p>
<span class="original">
You can also search an array using the <literal>&amp;&amp;</literal> operator,
which checks whether the left operand overlaps with the right operand.
For instance:
</span>
<code class="literal">&&</code>演算子を使って配列を検索することもできます。
この演算子は左辺が右辺と重なるかどうかを調べます。
例えば、
</p><pre class="programlisting">
SELECT * FROM sal_emp WHERE pay_by_quarter && ARRAY[10000];
</pre><p>
<span class="original">
This and other array operators are further described in
<xref linkend="functions-array"/>. It can be accelerated by an appropriate
index, as described in <xref linkend="indexes-types"/>.
</span>
この演算子やその他の配列の演算子は<a class="xref" href="functions-array.html" title="9.19. 配列関数と演算子">9.19</a>により詳しく書かれています。
<a class="xref" href="indexes-types.html" title="11.2. インデックスの種類">11.2</a>に書いてあるように、適切なインデックスにより高速化されます。
</p><p>
<span class="original">
You can also search for specific values in an array using the <function>array_position</function>
and <function>array_positions</function> functions. The former returns the subscript of
the first occurrence of a value in an array; the latter returns an array with the
subscripts of all occurrences of the value in the array. For example:
</span>
関数<code class="function">array_position</code>や<code class="function">array_positions</code>を使って、配列内の特定の値を検索することもできます。
前者は配列内で初めてその値が現れる添字を返し、後者は配列内でその値が現れる添字すべての配列を返します。
例えば、以下の通りです。
</p><pre class="programlisting">
SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon');
array_position
----------------
2
(1 row)
SELECT array_positions(ARRAY[1, 4, 3, 1, 3, 4, 2, 1], 1);
array_positions
-----------------
{1,4,8}
(1 row)
</pre><p>
</p><div class="tip"><h3 class="title">ヒント</h3><p>
<span class="original">
Arrays are not sets; searching for specific array elements
can be a sign of database misdesign. Consider
using a separate table with a row for each item that would be an
array element. This will be easier to search, and is likely to
scale better for a large number of elements.
</span>
配列は集合ではありません。
特定の配列要素に検索をかけることはデータベース設計が誤っている可能性があります。
配列の要素とみなされるそれぞれの項目を行に持つ別のテーブルを使うことを検討してください。
この方が検索がより簡単になり要素数が大きくなっても規模的拡張性があります。
</p></div></div><div class="sect2" id="ARRAYS-IO"><div class="titlepage"><div><div><h3 class="title">8.15.6. 配列の入出力構文 <a href="#ARRAYS-IO" class="id_link">#</a></h3></div></div></div><span class="original">
<title>Array Input and Output Syntax</title>
</span><a id="id-1.5.7.25.10.2" class="indexterm"></a><a id="id-1.5.7.25.10.3" class="indexterm"></a><p>
<span class="original">
The external text representation of an array value consists of items that
are interpreted according to the I/O conversion rules for the array's
element type, plus decoration that indicates the array structure.
The decoration consists of curly braces (<literal>{</literal> and <literal>}</literal>)
around the array value plus delimiter characters between adjacent items.
The delimiter character is usually a comma (<literal>,</literal>) but can be
something else: it is determined by the <literal>typdelim</literal> setting
for the array's element type. Among the standard data types provided
in the <productname>PostgreSQL</productname> distribution, all use a comma,
except for type <type>box</type>, which uses a semicolon (<literal>;</literal>).
In a multidimensional array, each dimension (row, plane,
cube, etc.) gets its own level of curly braces, and delimiters
must be written between adjacent curly-braced entities of the same level.
</span>
配列の値の外部表現は配列の要素の型に対するI/O変換ルールに基づいて解釈された項目と配列の構造を示す装飾項目で構成されています。
装飾は配列の値を中括弧(<code class="literal">{</code>と<code class="literal">}</code>)で囲んだものと次の項目との間を区切り文字で区切ったものです。
区切り文字は通常カンマ(<code class="literal">,</code>)ですが他の文字でも構いません。
配列の要素の型<code class="literal">typdelim</code>を設定することで決まります。
<span class="productname">PostgreSQL</span>配布物における標準のデータ型の中でセミコロン(<code class="literal">;</code>)を使う<code class="type">box</code>型を除いて、すべてはカンマを使います。
多次元配列ではそれぞれの次元(行、面、立体など)はそれ自身の階層において中括弧、同じ階層の中括弧でくくられた次の塊との間に区切り文字が書かれていなければなりません。
</p><p>
<span class="original">
The array output routine will put double quotes around element values
if they are empty strings, contain curly braces, delimiter characters,
double quotes, backslashes, or white space, or match the word
<literal>NULL</literal>. Double quotes and backslashes
embedded in element values will be backslash-escaped. For numeric
data types it is safe to assume that double quotes will never appear, but
for textual data types one should be prepared to cope with either the presence
or absence of quotes.
</span>
空の文字列や中括弧や区切り文字、二重引用符、バックスラッシュ、空白、<code class="literal">NULL</code>という単語が含まれていると、配列出力処理は要素の値を二重引用符でくくります。
要素の値に組み込まれている二重引用符とバックスラッシュはバックスラッシュでエスケープされます。
数値データ型に対しては二重引用符が出現しないと想定するのが安全ですが、テキストデータ型の場合引用符がある場合とない場合に対処できるようにしておくべきです。
</p><p>
<span class="original">
By default, the lower bound index value of an array's dimensions is
set to one. To represent arrays with other lower bounds, the array
subscript ranges can be specified explicitly before writing the
array contents.
This decoration consists of square brackets (<literal>[]</literal>)
around each array dimension's lower and upper bounds, with
a colon (<literal>:</literal>) delimiter character in between. The
array dimension decoration is followed by an equal sign (<literal>=</literal>).
For example:
</span>
デフォルトでは配列の次元の下限インデックス値は1に設定されています。
他の下限値を持つ配列を表現したければ、配列定数を作成する前に明示的に配列添字範囲を指定することで実現できます。
修飾項目はそれぞれの配列次元の上限と下限をコロン(<code class="literal">:</code>)で区切って前後を大括弧(<code class="literal">[]</code>)でくくった形式になっています。
代入演算子(<code class="literal">=</code>)の後に配列次元修飾項目が続きます。
例を示します。
</p><pre class="programlisting">
SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
FROM (SELECT '[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[] AS f1) AS ss;
e1 | e2
----+----
1 | 6
(1 row)
</pre><p>
<span class="original">
The array output routine will include explicit dimensions in its result
only when there are one or more lower bounds different from one.
</span>
1とは異なる下限を持つ場合にのみ、配列出力関数はその結果に明示的な次元を含めます。
</p><p>
<span class="original">
If the value written for an element is <literal>NULL</literal> (in any case
variant), the element is taken to be NULL. The presence of any quotes
or backslashes disables this and allows the literal string value
<quote>NULL</quote> to be entered. Also, for backward compatibility with
pre-8.2 versions of <productname>PostgreSQL</productname>, the <xref
linkend="guc-array-nulls"/> configuration parameter can be turned
<literal>off</literal> to suppress recognition of <literal>NULL</literal> as a NULL.
</span>
要素に指定された値が<code class="literal">NULL</code>(またはその亜種)の場合、要素はNULLとして扱われます。
引用符やバックスラッシュがあると、これは無効となり、<span class="quote">「<span class="quote">NULL</span>」</span>という文字列リテラルを入力することができます。
また、8.2以前の<span class="productname">PostgreSQL</span>との後方互換性のため、<a class="xref" href="runtime-config-compatible.html#GUC-ARRAY-NULLS">array_nulls</a>設定パラメータを<code class="literal">off</code>にして、<code class="literal">NULL</code>をNULLとして認識しないようにすることができます。
</p><p>
<span class="original">
As shown previously, when writing an array value you can use double
quotes around any individual array element. You <emphasis>must</emphasis> do so
if the element value would otherwise confuse the array-value parser.
For example, elements containing curly braces, commas (or the data type's
delimiter character), double quotes, backslashes, or leading or trailing
whitespace must be double-quoted. Empty strings and strings matching the
word <literal>NULL</literal> must be quoted, too. To put a double
quote or backslash in a quoted array element value, precede it
with a backslash. Alternatively, you can avoid quotes and use
backslash-escaping to protect all data characters that would otherwise
be taken as array syntax.
</span>
前に示したように配列に値を書き込む場合は独立した配列要素を二重引用符でくくります。
配列値パーサが配列要素値によって混乱を来さないように<span class="emphasis"><em>必ず</em></span>この形式を守ってください。
例えば、中括弧、カンマ(もしくはデータ型の区切り文字)、二重引用符、バックスラッシュもしくは前後に付いた空白を含む要素は必ず二重引用符でくくらなければなりません。
空文字列や<code class="literal">NULL</code>という単語自体も同様に引用符でくくらなければなりません。
二重引用符もしくはバックスラッシュを引用符付きの配列要素に付け加えたい場合、その直前にバックスラッシュを付けます。
別の方法として、配列構文とみなされかねない全てのデータ文字を保護するために、引用符を使用しないでバックスラッシュでエスケープしても構いません。
</p><p>
<span class="original">
You can add whitespace before a left brace or after a right
brace. You can also add whitespace before or after any individual item
string. In all of these cases the whitespace will be ignored. However,
whitespace within double-quoted elements, or surrounded on both sides by
non-whitespace characters of an element, is not ignored.
</span>
括弧の右側もしくは左側それぞれの前と後に空白を追加することができます。
同様に独立した項目の文字列の前後に空白を付け加えることもできます。
これらすべての場合において空白は無視されます。
とは言っても二重引用符で囲まれた要素の中の空白、もしくは要素の空白文字以外により両側がくくられているものは無視されません。
</p><div class="tip"><h3 class="title">ヒント</h3><p>
<span class="original">
The <literal>ARRAY</literal> constructor syntax (see
<xref linkend="sql-syntax-array-constructors"/>) is often easier to work
with than the array-literal syntax when writing array values in SQL
commands. In <literal>ARRAY</literal>, individual element values are written the
same way they would be written when not members of an array.
</span>
SQLコマンドの中で配列値を書く時、配列リテラル構文よりも<code class="literal">ARRAY</code>生成子構文(<a class="xref" href="sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS" title="4.2.12. 配列コンストラクタ">4.2.12</a>を参照)の方が往々にして扱いやすい場合があります。
<code class="literal">ARRAY</code>では、個々の要素値は、配列のメンバでない場合と同じ方法で記述されます。
</p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="datatype-json.html" title="8.14. JSONデータ型">前へ</a> </td><td width="20%" align="center"><a accesskey="u" href="datatype.html" title="第8章 データ型">上へ</a></td><td width="40%" align="right"> <a accesskey="n" href="rowtypes.html" title="8.16. 複合型">次へ</a></td></tr><tr><td width="40%" align="left" valign="top">8.14. <acronym class="acronym">JSON</acronym>データ型 </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.3文書">ホーム</a></td><td width="40%" align="right" valign="top"> 8.16. 複合型</td></tr></table></div></body></html>