~ubuntu-branches/ubuntu/utopic/postgresql-9.4/utopic-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/html/queries-table-expressions.html

  • Committer: Package Import Robot
  • Author(s): Martin Pitt, CVE-2014-8161
  • Date: 2015-02-06 12:31:46 UTC
  • mfrom: (1.1.5) (7.1.2 utopic-proposed)
  • Revision ID: package-import@ubuntu.com-20150206123146-vtmf30jbkm7w16p8
Tags: 9.4.1-0ubuntu0.14.10
* New upstream security/bug fix release (LP: #1418928)
  - Fix buffer overruns in to_char() [CVE-2015-0241]
  - Fix buffer overruns in contrib/pgcrypto [CVE-2015-0243]
  - Fix possible loss of frontend/backend protocol synchronization after an
    error [CVE-2015-0244]
  - Fix information leak via constraint-violation error messages
    [CVE-2014-8161]
  - See release notes for details about other fixes:
    http://www.postgresql.org/about/news/1569/

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
REV="MADE"
10
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
11
11
REL="HOME"
12
 
TITLE="PostgreSQL 9.4beta3 Documentation"
 
12
TITLE="PostgreSQL 9.4.1 Documentation"
13
13
HREF="index.html"><LINK
14
14
REL="UP"
15
15
TITLE="Queries"
26
26
HTTP-EQUIV="Content-Type"
27
27
CONTENT="text/html; charset=ISO-8859-1"><META
28
28
NAME="creation"
29
 
CONTENT="2014-10-06T18:35:22"></HEAD
 
29
CONTENT="2015-02-02T20:51:01"></HEAD
30
30
><BODY
31
31
CLASS="SECT1"
32
32
><DIV
44
44
VALIGN="bottom"
45
45
><A
46
46
HREF="index.html"
47
 
>PostgreSQL 9.4beta3 Documentation</A
 
47
>PostgreSQL 9.4.1 Documentation</A
48
48
></TH
49
49
></TR
50
50
><TR
183
183
><P>
184
184
 
185
185
    A table reference can be a table name (possibly schema-qualified),
186
 
    or a derived table such as a subquery, a table join, or complex
187
 
    combinations of these.  If more than one table reference is listed
188
 
    in the <TT
189
 
CLASS="LITERAL"
190
 
>FROM</TT
191
 
> clause they are cross-joined (see below)
192
 
    to form the intermediate virtual table that can then be subject to
 
186
    or a derived table such as a subquery, a <TT
 
187
CLASS="LITERAL"
 
188
>JOIN</TT
 
189
> construct, or
 
190
    complex combinations of these.  If more than one table reference is
 
191
    listed in the <TT
 
192
CLASS="LITERAL"
 
193
>FROM</TT
 
194
> clause, the tables are cross-joined
 
195
    (that is, the Cartesian product of their rows is formed; see below).
 
196
    The result of the <TT
 
197
CLASS="LITERAL"
 
198
>FROM</TT
 
199
> list is an intermediate virtual
 
200
    table that can then be subject to
193
201
    transformations by the <TT
194
202
CLASS="LITERAL"
195
203
>WHERE</TT
249
257
>     A joined table is a table derived from two other (real or
250
258
     derived) tables according to the rules of the particular join
251
259
     type.  Inner, outer, and cross-joins are available.
 
260
     The general syntax of a joined table is
 
261
</P><PRE
 
262
CLASS="SYNOPSIS"
 
263
><TT
 
264
CLASS="REPLACEABLE"
 
265
><I
 
266
>T1</I
 
267
></TT
 
268
> <TT
 
269
CLASS="REPLACEABLE"
 
270
><I
 
271
>join_type</I
 
272
></TT
 
273
> <TT
 
274
CLASS="REPLACEABLE"
 
275
><I
 
276
>T2</I
 
277
></TT
 
278
> [<SPAN
 
279
CLASS="OPTIONAL"
 
280
> <TT
 
281
CLASS="REPLACEABLE"
 
282
><I
 
283
>join_condition</I
 
284
></TT
 
285
> </SPAN
 
286
>]</PRE
 
287
><P>
 
288
     Joins of all types can be chained together, or nested: either or
 
289
     both <TT
 
290
CLASS="REPLACEABLE"
 
291
><I
 
292
>T1</I
 
293
></TT
 
294
> and
 
295
     <TT
 
296
CLASS="REPLACEABLE"
 
297
><I
 
298
>T2</I
 
299
></TT
 
300
> can be joined tables.  Parentheses
 
301
     can be used around <TT
 
302
CLASS="LITERAL"
 
303
>JOIN</TT
 
304
> clauses to control the join
 
305
     order.  In the absence of parentheses, <TT
 
306
CLASS="LITERAL"
 
307
>JOIN</TT
 
308
> clauses
 
309
     nest left-to-right.
252
310
    </P
253
311
><P
254
312
></P
331
389
><I
332
390
>T1</I
333
391
></TT
334
 
>,
335
 
        <TT
336
 
CLASS="REPLACEABLE"
337
 
><I
338
 
>T2</I
339
 
></TT
340
 
></TT
341
 
>.  It is also equivalent to
342
 
        <TT
343
 
CLASS="LITERAL"
344
 
>FROM <TT
345
 
CLASS="REPLACEABLE"
346
 
><I
347
 
>T1</I
348
 
></TT
349
392
> INNER JOIN
350
393
        <TT
351
394
CLASS="REPLACEABLE"
354
397
></TT
355
398
> ON TRUE</TT
356
399
> (see below).
 
400
        It is also equivalent to
 
401
        <TT
 
402
CLASS="LITERAL"
 
403
>FROM <TT
 
404
CLASS="REPLACEABLE"
 
405
><I
 
406
>T1</I
 
407
></TT
 
408
>,
 
409
        <TT
 
410
CLASS="REPLACEABLE"
 
411
><I
 
412
>T2</I
 
413
></TT
 
414
></TT
 
415
>.
 
416
        </P><DIV
 
417
CLASS="NOTE"
 
418
><BLOCKQUOTE
 
419
CLASS="NOTE"
 
420
><P
 
421
><B
 
422
>Note: </B
 
423
>         This latter equivalence does not hold exactly when more than two
 
424
         tables appear, because <TT
 
425
CLASS="LITERAL"
 
426
>JOIN</TT
 
427
> binds more tightly than
 
428
         comma.  For example
 
429
         <TT
 
430
CLASS="LITERAL"
 
431
>FROM <TT
 
432
CLASS="REPLACEABLE"
 
433
><I
 
434
>T1</I
 
435
></TT
 
436
> CROSS JOIN
 
437
         <TT
 
438
CLASS="REPLACEABLE"
 
439
><I
 
440
>T2</I
 
441
></TT
 
442
> INNER JOIN <TT
 
443
CLASS="REPLACEABLE"
 
444
><I
 
445
>T3</I
 
446
></TT
 
447
>
 
448
         ON <TT
 
449
CLASS="REPLACEABLE"
 
450
><I
 
451
>condition</I
 
452
></TT
 
453
></TT
 
454
>
 
455
         is not the same as
 
456
         <TT
 
457
CLASS="LITERAL"
 
458
>FROM <TT
 
459
CLASS="REPLACEABLE"
 
460
><I
 
461
>T1</I
 
462
></TT
 
463
>,
 
464
         <TT
 
465
CLASS="REPLACEABLE"
 
466
><I
 
467
>T2</I
 
468
></TT
 
469
> INNER JOIN <TT
 
470
CLASS="REPLACEABLE"
 
471
><I
 
472
>T3</I
 
473
></TT
 
474
>
 
475
         ON <TT
 
476
CLASS="REPLACEABLE"
 
477
><I
 
478
>condition</I
 
479
></TT
 
480
></TT
 
481
>
 
482
         because the <TT
 
483
CLASS="REPLACEABLE"
 
484
><I
 
485
>condition</I
 
486
></TT
 
487
> can
 
488
         reference <TT
 
489
CLASS="REPLACEABLE"
 
490
><I
 
491
>T1</I
 
492
></TT
 
493
> in the first case but not
 
494
         the second.
 
495
        </P
 
496
></BLOCKQUOTE
 
497
></DIV
 
498
><P>
357
499
       </P
358
500
></DD
359
501
><DT
473
615
>, as explained in detail below.
474
616
       </P
475
617
><P
 
618
>        The possible types of qualified join are:
 
619
 
 
620
       <P
 
621
></P
 
622
></P><DIV
 
623
CLASS="VARIABLELIST"
 
624
><DL
 
625
><DT
 
626
><TT
 
627
CLASS="LITERAL"
 
628
>INNER JOIN</TT
 
629
></DT
 
630
><DD
 
631
><P
 
632
>           For each row R1 of T1, the joined table has a row for each
 
633
           row in T2 that satisfies the join condition with R1.
 
634
          </P
 
635
></DD
 
636
><DT
 
637
><TT
 
638
CLASS="LITERAL"
 
639
>LEFT OUTER JOIN</TT
 
640
>
 
641
         
 
642
 
 
643
         </DT
 
644
><DD
 
645
><P
 
646
>           First, an inner join is performed.  Then, for each row in
 
647
           T1 that does not satisfy the join condition with any row in
 
648
           T2, a joined row is added with null values in columns of
 
649
           T2.  Thus, the joined table always has at least
 
650
           one row for each row in T1.
 
651
          </P
 
652
></DD
 
653
><DT
 
654
><TT
 
655
CLASS="LITERAL"
 
656
>RIGHT OUTER JOIN</TT
 
657
>
 
658
         
 
659
 
 
660
         </DT
 
661
><DD
 
662
><P
 
663
>           First, an inner join is performed.  Then, for each row in
 
664
           T2 that does not satisfy the join condition with any row in
 
665
           T1, a joined row is added with null values in columns of
 
666
           T1.  This is the converse of a left join: the result table
 
667
           will always have a row for each row in T2.
 
668
          </P
 
669
></DD
 
670
><DT
 
671
><TT
 
672
CLASS="LITERAL"
 
673
>FULL OUTER JOIN</TT
 
674
></DT
 
675
><DD
 
676
><P
 
677
>           First, an inner join is performed.  Then, for each row in
 
678
           T1 that does not satisfy the join condition with any row in
 
679
           T2, a joined row is added with null values in columns of
 
680
           T2.  Also, for each row of T2 that does not satisfy the
 
681
           join condition with any row in T1, a joined row with null
 
682
           values in the columns of T1 is added.
 
683
          </P
 
684
></DD
 
685
></DL
 
686
></DIV
 
687
><P>
 
688
       </P
 
689
><P
476
690
>        The <TT
477
691
CLASS="LITERAL"
478
692
>ON</TT
496
710
        <TT
497
711
CLASS="LITERAL"
498
712
>ON</TT
499
 
> expression evaluates to true for them.
 
713
> expression evaluates to true.
500
714
       </P
501
715
><P
502
 
>        <TT
 
716
>        The <TT
503
717
CLASS="LITERAL"
504
718
>USING</TT
505
 
> is a shorthand notation: it takes a
506
 
        comma-separated list of column names, which the joined tables
507
 
        must have in common, and forms a join condition specifying
508
 
        equality of each of these pairs of columns.  Furthermore, the
509
 
        output of <TT
 
719
> clause is a shorthand that allows you to take
 
720
        advantage of the specific situation where both sides of the join use
 
721
        the same name for the joining column(s).  It takes a
 
722
        comma-separated list of the shared column names
 
723
        and forms a join condition that includes an equality comparison
 
724
        for each one.  For example, joining <TT
 
725
CLASS="REPLACEABLE"
 
726
><I
 
727
>T1</I
 
728
></TT
 
729
>
 
730
        and <TT
 
731
CLASS="REPLACEABLE"
 
732
><I
 
733
>T2</I
 
734
></TT
 
735
> with <TT
 
736
CLASS="LITERAL"
 
737
>USING (a, b)</TT
 
738
> produces
 
739
        the join condition <TT
 
740
CLASS="LITERAL"
 
741
>ON <TT
 
742
CLASS="REPLACEABLE"
 
743
><I
 
744
>T1</I
 
745
></TT
 
746
>.a
 
747
        = <TT
 
748
CLASS="REPLACEABLE"
 
749
><I
 
750
>T2</I
 
751
></TT
 
752
>.a AND <TT
 
753
CLASS="REPLACEABLE"
 
754
><I
 
755
>T1</I
 
756
></TT
 
757
>.b
 
758
        = <TT
 
759
CLASS="REPLACEABLE"
 
760
><I
 
761
>T2</I
 
762
></TT
 
763
>.b</TT
 
764
>.
 
765
       </P
 
766
><P
 
767
>        Furthermore, the output of <TT
510
768
CLASS="LITERAL"
511
769
>JOIN USING</TT
512
 
> has one column for each of
513
 
        the equated pairs of input columns, followed by the
514
 
        remaining columns from each table.  Thus, <TT
515
 
CLASS="LITERAL"
516
 
>USING (a, b,
517
 
        c)</TT
518
 
> is equivalent to <TT
519
 
CLASS="LITERAL"
520
 
>ON (t1.a = t2.a AND
521
 
        t1.b = t2.b AND t1.c = t2.c)</TT
522
 
> with the exception that
523
 
        if <TT
524
 
CLASS="LITERAL"
525
 
>ON</TT
526
 
> is used there will be two columns
527
 
        <TT
528
 
CLASS="LITERAL"
529
 
>a</TT
 
770
> suppresses
 
771
        redundant columns: there is no need to print both of the matched
 
772
        columns, since they must have equal values.  While <TT
 
773
CLASS="LITERAL"
 
774
>JOIN
 
775
        ON</TT
 
776
> produces all columns from <TT
 
777
CLASS="REPLACEABLE"
 
778
><I
 
779
>T1</I
 
780
></TT
 
781
> followed by all
 
782
        columns from <TT
 
783
CLASS="REPLACEABLE"
 
784
><I
 
785
>T2</I
 
786
></TT
530
787
>, <TT
531
788
CLASS="LITERAL"
532
 
>b</TT
533
 
>, and <TT
534
 
CLASS="LITERAL"
535
 
>c</TT
536
 
> in the result,
537
 
        whereas with <TT
538
 
CLASS="LITERAL"
539
 
>USING</TT
540
 
> there will be only one of each
541
 
        (and they will appear first if <TT
542
 
CLASS="COMMAND"
543
 
>SELECT *</TT
544
 
> is used).
 
789
>JOIN USING</TT
 
790
> produces one
 
791
        output column for each of the listed column pairs (in the listed
 
792
        order), followed by any remaining columns from <TT
 
793
CLASS="REPLACEABLE"
 
794
><I
 
795
>T1</I
 
796
></TT
 
797
>,
 
798
        followed by any remaining columns from <TT
 
799
CLASS="REPLACEABLE"
 
800
><I
 
801
>T2</I
 
802
></TT
 
803
>.
545
804
       </P
546
805
><P
547
806
>        
563
822
>USING</TT
564
823
>, these columns appear
565
824
        only once in the output table.  If there are no common
566
 
        columns, <TT
 
825
        column names, <TT
567
826
CLASS="LITERAL"
568
827
>NATURAL</TT
569
828
> behaves like
572
831
>CROSS JOIN</TT
573
832
>.
574
833
       </P
575
 
><P
576
 
>        The possible types of qualified join are:
577
 
 
578
 
       <P
579
 
></P
580
 
></P><DIV
581
 
CLASS="VARIABLELIST"
582
 
><DL
583
 
><DT
584
 
><TT
585
 
CLASS="LITERAL"
586
 
>INNER JOIN</TT
587
 
></DT
588
 
><DD
589
 
><P
590
 
>           For each row R1 of T1, the joined table has a row for each
591
 
           row in T2 that satisfies the join condition with R1.
592
 
          </P
593
 
></DD
594
 
><DT
595
 
><TT
596
 
CLASS="LITERAL"
597
 
>LEFT OUTER JOIN</TT
598
 
>
599
 
         
600
 
 
601
 
         </DT
602
 
><DD
603
 
><P
604
 
>           First, an inner join is performed.  Then, for each row in
605
 
           T1 that does not satisfy the join condition with any row in
606
 
           T2, a joined row is added with null values in columns of
607
 
           T2.  Thus, the joined table always has at least
608
 
           one row for each row in T1.
609
 
          </P
610
 
></DD
611
 
><DT
612
 
><TT
613
 
CLASS="LITERAL"
614
 
>RIGHT OUTER JOIN</TT
615
 
>
616
 
         
617
 
 
618
 
         </DT
619
 
><DD
620
 
><P
621
 
>           First, an inner join is performed.  Then, for each row in
622
 
           T2 that does not satisfy the join condition with any row in
623
 
           T1, a joined row is added with null values in columns of
624
 
           T1.  This is the converse of a left join: the result table
625
 
           will always have a row for each row in T2.
626
 
          </P
627
 
></DD
628
 
><DT
629
 
><TT
630
 
CLASS="LITERAL"
631
 
>FULL OUTER JOIN</TT
632
 
></DT
633
 
><DD
634
 
><P
635
 
>           First, an inner join is performed.  Then, for each row in
636
 
           T1 that does not satisfy the join condition with any row in
637
 
           T2, a joined row is added with null values in columns of
638
 
           T2.  Also, for each row of T2 that does not satisfy the
639
 
           join condition with any row in T1, a joined row with null
640
 
           values in the columns of T1 is added.
641
 
          </P
642
 
></DD
643
 
></DL
644
 
></DIV
645
 
><P>
646
 
       </P
647
 
></DD
648
 
></DL
649
 
></DIV
650
 
><P
651
 
>     Joins of all types can be chained together or nested: either or
652
 
     both <TT
653
 
CLASS="REPLACEABLE"
654
 
><I
655
 
>T1</I
656
 
></TT
657
 
> and
658
 
     <TT
659
 
CLASS="REPLACEABLE"
660
 
><I
661
 
>T2</I
662
 
></TT
663
 
> can be joined tables.  Parentheses
664
 
     can be used around <TT
665
 
CLASS="LITERAL"
666
 
>JOIN</TT
667
 
> clauses to control the join
668
 
     order.  In the absence of parentheses, <TT
669
 
CLASS="LITERAL"
670
 
>JOIN</TT
671
 
> clauses
672
 
     nest left-to-right.
673
 
    </P
 
834
><DIV
 
835
CLASS="NOTE"
 
836
><BLOCKQUOTE
 
837
CLASS="NOTE"
 
838
><P
 
839
><B
 
840
>Note: </B
 
841
>         <TT
 
842
CLASS="LITERAL"
 
843
>USING</TT
 
844
> is reasonably safe from column changes
 
845
         in the joined relations since only the listed columns
 
846
         are combined.  <TT
 
847
CLASS="LITERAL"
 
848
>NATURAL</TT
 
849
> is considerably more risky since
 
850
         any schema changes to either relation that cause a new matching
 
851
         column name to be present will cause the join to combine that new
 
852
         column as well.
 
853
        </P
 
854
></BLOCKQUOTE
 
855
></DIV
 
856
></DD
 
857
></DL
 
858
></DIV
674
859
><P
675
860
>     To put this together, assume we have tables <TT
676
861
CLASS="LITERAL"
881
1066
>after</I
882
1067
></SPAN
883
1068
> the join.
 
1069
     That does not matter with inner joins, but it matters a lot with outer
 
1070
     joins.
884
1071
    </P
885
1072
></DIV
886
1073
><DIV