Re: sub query reference error or user error - Mailing list pgsql-general

From David Johnston
Subject Re: sub query reference error or user error
Date
Msg-id AD488835-DD56-4B7D-9137-033137F8015D@yahoo.com
Whole thread Raw
In response to sub query reference error or user error  (sivakumar krishnamurthy <sivakumar.mailinglist@gmail.com>)
List pgsql-general
On Nov 6, 2012, at 12:36, sivakumar krishnamurthy <sivakumar.mailinglist@gmail.com> wrote:

> Hi All,
>   For the below test case shouldn't the  update statement throw error because the sub query projects a column which
isnot existing in the table. For ex in inner select nos1 column exists in t1 and not t2. 
> Is this a bug or an OP error?
>
> template1=# SELECT version();
>                                                     version
> ----------------------------------------------------------------------------------------------------------------
>  PostgreSQL 9.1.6 on x86_64-unknown-linux-gnu, compiled by gcc-4.4.real (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3, 64-bit
> (1 row)
> template1=# \d t1
>       Table "public.t1"
>  Column |  Type   | Modifiers
> --------+---------+-----------
>  nos1   | integer |
>
> template1=# \d t2
>       Table "public.t2"
>  Column |  Type   | Modifiers
> --------+---------+-----------
>  nos2   | integer |
>
> template1=# UPDATE t1 set nos1=2 where nos1 in (select nos1 from t2);
> UPDATE 0
> template1=#
>
> Thanks,
> Sivakumar.K
>

In a correlated sub-query any column from the parent or sub-query can appear.  In this case anything in t1or t2.  In
thiscase the effect is a constant true evaluation since for each row in t1 the value of nos1 is constant.  The use of
t2simply causes the constant to be repeated once for each row in t2.  As a safety feature you should prefix your
columnsin sub-queries with the proper table name.  t2.nos1 would then throw the desired error. 

Dave J.

pgsql-general by date:

Previous
From: Tianyin Xu
Date:
Subject: Re: Question about "ident_file" in postgres.conf
Next
From: Lists
Date:
Subject: Re: Unexpectedly high disk space usage