~ubuntu-branches/ubuntu/trusty/postgresql-9.3/trusty-updates

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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Composite Types</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.3.13 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Data Types"
HREF="datatype.html"><LINK
REL="PREVIOUS"
TITLE="Arrays"
HREF="arrays.html"><LINK
REL="NEXT"
TITLE="Range Types"
HREF="rangetypes.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2016-05-09T21:13:26"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.3.13 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Arrays"
HREF="arrays.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="datatype.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 8. Data Types</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Range Types"
HREF="rangetypes.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="ROWTYPES"
>8.16. Composite Types</A
></H1
><P
>  A <I
CLASS="FIRSTTERM"
>composite type</I
> represents the structure of a row or record;
  it is essentially just a list of field names and their data types.
  <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> allows  composite types to be
  used in many of the same ways that simple types can be used.  For example, a
  column of a table can be declared to be of a composite type.
 </P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN7414"
>8.16.1. Declaration of Composite Types</A
></H2
><P
>  Here are two simple examples of defining composite types:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TYPE complex AS (
    r       double precision,
    i       double precision
);

CREATE TYPE inventory_item AS (
    name            text,
    supplier_id     integer,
    price           numeric
);</PRE
><P>
  The syntax is comparable to <TT
CLASS="COMMAND"
>CREATE TABLE</TT
>, except that only
  field names and types can be specified; no constraints (such as <TT
CLASS="LITERAL"
>NOT
  NULL</TT
>) can presently be included.  Note that the <TT
CLASS="LITERAL"
>AS</TT
> keyword
  is essential; without it, the system will think a different kind
  of <TT
CLASS="COMMAND"
>CREATE TYPE</TT
> command is meant, and you will get odd syntax
  errors.
 </P
><P
>  Having defined the types, we can use them to create tables:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE on_hand (
    item      inventory_item,
    count     integer
);

INSERT INTO on_hand VALUES (ROW('fuzzy dice', 42, 1.99), 1000);</PRE
><P>

  or functions:

</P><PRE
CLASS="PROGRAMLISTING"
>CREATE FUNCTION price_extension(inventory_item, integer) RETURNS numeric
AS 'SELECT $1.price * $2' LANGUAGE SQL;

SELECT price_extension(item, 10) FROM on_hand;</PRE
><P>

 </P
><P
>  Whenever you create a table, a composite type is also automatically
  created, with the same name as the table, to represent the table's
  row type.  For example, had we said:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE inventory_item (
    name            text,
    supplier_id     integer REFERENCES suppliers,
    price           numeric CHECK (price &gt; 0)
);</PRE
><P>
  then the same <TT
CLASS="LITERAL"
>inventory_item</TT
> composite type shown above would
  come into being as a
  byproduct, and could be used just as above.  Note however an important
  restriction of the current implementation: since no constraints are
  associated with a composite type, the constraints shown in the table
  definition <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>do not apply</I
></SPAN
> to values of the composite type
  outside the table.  (A partial workaround is to use domain
  types as members of composite types.)
 </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN7429"
>8.16.2. Composite Value Input</A
></H2
><P
>   To write a composite value as a literal constant, enclose the field
   values within parentheses and separate them by commas.  You can put double
   quotes around any field value, and must do so if it contains commas or
   parentheses.  (More details appear below.)  Thus, the general format of a
   composite constant is the following:
</P><PRE
CLASS="SYNOPSIS"
>'( <TT
CLASS="REPLACEABLE"
><I
>val1</I
></TT
> , <TT
CLASS="REPLACEABLE"
><I
>val2</I
></TT
> , ... )'</PRE
><P>
   An example is:
</P><PRE
CLASS="PROGRAMLISTING"
>'("fuzzy dice",42,1.99)'</PRE
><P>
   which would be a valid value of the <TT
CLASS="LITERAL"
>inventory_item</TT
> type
   defined above.  To make a field be NULL, write no characters at all
   in its position in the list.  For example, this constant specifies
   a NULL third field:
</P><PRE
CLASS="PROGRAMLISTING"
>'("fuzzy dice",42,)'</PRE
><P>
   If you want an empty string rather than NULL, write double quotes:
</P><PRE
CLASS="PROGRAMLISTING"
>'("",42,)'</PRE
><P>
   Here the first field is a non-NULL empty string, the third is NULL.
  </P
><P
>   (These constants are actually only a special case of
   the generic type constants discussed in <A
HREF="sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS-GENERIC"
>Section 4.1.2.7</A
>.  The constant is initially
   treated as a string and passed to the composite-type input conversion
   routine.  An explicit type specification might be necessary.)
  </P
><P
>  The <TT
CLASS="LITERAL"
>ROW</TT
> expression syntax can also be used to
  construct composite values.  In most cases this is considerably
  simpler to use than the string-literal syntax since you don't have
  to worry about multiple layers of quoting.  We already used this
  method above:
</P><PRE
CLASS="PROGRAMLISTING"
>ROW('fuzzy dice', 42, 1.99)
ROW('', 42, NULL)</PRE
><P>
  The ROW keyword is actually optional as long as you have more than one
  field in the expression, so these can simplify to:
</P><PRE
CLASS="PROGRAMLISTING"
>('fuzzy dice', 42, 1.99)
('', 42, NULL)</PRE
><P>
  The <TT
CLASS="LITERAL"
>ROW</TT
> expression syntax is discussed in more detail in <A
HREF="sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS"
>Section 4.2.13</A
>.
 </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN7450"
>8.16.3. Accessing Composite Types</A
></H2
><P
>  To access a field of a composite column, one writes a dot and the field
  name, much like selecting a field from a table name.  In fact, it's so
  much like selecting from a table name that you often have to use parentheses
  to keep from confusing the parser.  For example, you might try to select
  some subfields from our <TT
CLASS="LITERAL"
>on_hand</TT
> example table with something
  like:

</P><PRE
CLASS="PROGRAMLISTING"
>SELECT item.name FROM on_hand WHERE item.price &gt; 9.99;</PRE
><P>

  This will not work since the name <TT
CLASS="LITERAL"
>item</TT
> is taken to be a table
  name, not a column name of <TT
CLASS="LITERAL"
>on_hand</TT
>, per SQL syntax rules.
  You must write it like this:

</P><PRE
CLASS="PROGRAMLISTING"
>SELECT (item).name FROM on_hand WHERE (item).price &gt; 9.99;</PRE
><P>

  or if you need to use the table name as well (for instance in a multitable
  query), like this:

</P><PRE
CLASS="PROGRAMLISTING"
>SELECT (on_hand.item).name FROM on_hand WHERE (on_hand.item).price &gt; 9.99;</PRE
><P>

  Now the parenthesized object is correctly interpreted as a reference to
  the <TT
CLASS="LITERAL"
>item</TT
> column, and then the subfield can be selected from it.
 </P
><P
>  Similar syntactic issues apply whenever you select a field from a composite
  value.  For instance, to select just one field from the result of a function
  that returns a composite value, you'd need to write something like:

</P><PRE
CLASS="PROGRAMLISTING"
>SELECT (my_func(...)).field FROM ...</PRE
><P>

  Without the extra parentheses, this will generate a syntax error.
 </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN7462"
>8.16.4. Modifying Composite Types</A
></H2
><P
>  Here are some examples of the proper syntax for inserting and updating
  composite columns.
  First, inserting or updating a whole column:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO mytab (complex_col) VALUES((1.1,2.2));

UPDATE mytab SET complex_col = ROW(1.1,2.2) WHERE ...;</PRE
><P>

  The first example omits <TT
CLASS="LITERAL"
>ROW</TT
>, the second uses it; we
  could have done it either way.
 </P
><P
>  We can update an individual subfield of a composite column:

</P><PRE
CLASS="PROGRAMLISTING"
>UPDATE mytab SET complex_col.r = (complex_col).r + 1 WHERE ...;</PRE
><P>

  Notice here that we don't need to (and indeed cannot)
  put parentheses around the column name appearing just after
  <TT
CLASS="LITERAL"
>SET</TT
>, but we do need parentheses when referencing the same
  column in the expression to the right of the equal sign.
 </P
><P
>  And we can specify subfields as targets for <TT
CLASS="COMMAND"
>INSERT</TT
>, too:

</P><PRE
CLASS="PROGRAMLISTING"
>INSERT INTO mytab (complex_col.r, complex_col.i) VALUES(1.1, 2.2);</PRE
><P>

  Had we not supplied values for all the subfields of the column, the
  remaining subfields would have been filled with null values.
 </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="ROWTYPES-IO-SYNTAX"
>8.16.5. Composite Type Input and Output Syntax</A
></H2
><P
>   The external text representation of a composite value consists of items that
   are interpreted according to the I/O conversion rules for the individual
   field types, plus decoration that indicates the composite structure.
   The decoration consists of parentheses (<TT
CLASS="LITERAL"
>(</TT
> and <TT
CLASS="LITERAL"
>)</TT
>)
   around the whole value, plus commas (<TT
CLASS="LITERAL"
>,</TT
>) between adjacent
   items.  Whitespace outside the parentheses is ignored, but within the
   parentheses it is considered part of the field value, and might or might not be
   significant depending on the input conversion rules for the field data type.
   For example, in:
</P><PRE
CLASS="PROGRAMLISTING"
>'(  42)'</PRE
><P>
   the whitespace will be ignored if the field type is integer, but not if
   it is text.
  </P
><P
>   As shown previously, when writing a composite value you can write double
   quotes around any individual field value.
   You <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>must</I
></SPAN
> do so if the field value would otherwise
   confuse the composite-value parser.  In particular, fields containing
   parentheses, commas, double quotes, or backslashes must be double-quoted.
   To put a double quote or backslash in a quoted composite field value,
   precede it with a backslash.  (Also, a pair of double quotes within a
   double-quoted field value is taken to represent a double quote character,
   analogously to the rules for single quotes in SQL literal strings.)
   Alternatively, you can avoid quoting and use backslash-escaping to
   protect all data characters
   that would otherwise be taken as composite syntax.
  </P
><P
>   A completely empty field value (no characters at all between the commas
   or parentheses) represents a NULL.  To write a value that is an empty
   string rather than NULL, write <TT
CLASS="LITERAL"
>""</TT
>.
  </P
><P
>   The composite output routine will put double quotes around field values
   if they are empty strings or contain parentheses, commas,
   double quotes, backslashes, or white space.  (Doing so for white space
   is not essential, but aids legibility.)  Double quotes and backslashes
   embedded in field values will be doubled.
  </P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>   Remember that what you write in an SQL command will first be interpreted
   as a string literal, and then as a composite.  This doubles the number of
   backslashes you need (assuming escape string syntax is used).
   For example, to insert a <TT
CLASS="TYPE"
>text</TT
> field
   containing a double quote and a backslash in a composite
   value, you'd need to write:
</P><PRE
CLASS="PROGRAMLISTING"
>INSERT ... VALUES (E'("\\"\\\\")');</PRE
><P>
   The string-literal processor removes one level of backslashes, so that
   what arrives at the composite-value parser looks like
   <TT
CLASS="LITERAL"
>("\"\\")</TT
>.  In turn, the string
   fed to the <TT
CLASS="TYPE"
>text</TT
> data type's input routine
   becomes <TT
CLASS="LITERAL"
>"\</TT
>.  (If we were working
   with a data type whose input routine also treated backslashes specially,
   <TT
CLASS="TYPE"
>bytea</TT
> for example, we might need as many as eight backslashes
   in the command to get one backslash into the stored composite field.)
   Dollar quoting (see <A
HREF="sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING"
>Section 4.1.2.4</A
>) can be
   used to avoid the need to double backslashes.
  </P
></BLOCKQUOTE
></DIV
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
>   The <TT
CLASS="LITERAL"
>ROW</TT
> constructor syntax is usually easier to work with
   than the composite-literal syntax when writing composite values in SQL
   commands.
   In <TT
CLASS="LITERAL"
>ROW</TT
>, individual field values are written the same way
   they would be written when not members of a composite.
  </P
></BLOCKQUOTE
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="arrays.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="rangetypes.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Arrays</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="datatype.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Range Types</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>