pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Evil Azrael <evilazrael@evilazrael.de>
Cc: pgsql-sql@postgresql.org
Subject: Re: INSERT ... SELECT problem in Mysql
Date: Fri, 04 Jul 2003 17:45:46 -0400
Message-ID: <8818.1057355146@sss.pgh.pa.us> (raw)
In-Reply-To: <158603745.20030704231102@evilazrael.de>
References: <158603745.20030704231102@evilazrael.de>

Evil Azrael <evilazrael@evilazrael.de> writes:
> I have a problem with this part in the text
> ----
> The target table of the INSERT statement cannot appear in the FROM
> clause of the SELECT part of the query because it's forbidden in
> standard SQL to SELECT from the same table into which you are
> inserting.

This is just MySQL's self-serving reading of the spec.  What SQL92
actually says is (section 13.8)

         1) The following restrictions apply for Intermediate SQL:

            a) The leaf generally underlying table of T shall not be gen-
              erally contained in the <query expression> immediately
              contained in the <insert columns and source> except as the
              <qualifier> of a <column reference>.

SQL99 puts it a little differently:

         Conformance Rules

         1) Without Feature F781, "Self-referencing operations", no leaf
            generally underlying table of T shall be generally contained in
            the <query expression> immediately contained in the <insert
            columns and source> except as the <table or query name> or
            <correlation name> of a column reference.

In other words, the behavior is perfectly standard, it's just not
required for minimal SQL implementations to support it.

What the standard behavior is is defined by this rule, earlier in the
same section:

         3) The <query expression> is effectively evaluated before inserting
            any rows into B.

that is, the SELECT doesn't see any rows that are being inserted into
the target table during the same command.  There would not be any need
for that rule if selecting from the same table you're inserting into
were flat-out forbidden, as the MySQL docs claim.

			regards, tom lane



view thread (4+ messages)  latest in thread

Message-ID: <8818.1057355146@sss.pgh.pa.us>
Permalink:  ../8818.1057355146@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/8818.1057355146@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, evilazrael@evilazrael.de
  Subject: Re: INSERT ... SELECT problem in Mysql
  In-Reply-To: <8818.1057355146@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox