Sub-query having NULL row returning FALSE result

From: Sridhar N Bamandlapally <sridhar(dot)bn1(at)gmail(dot)com>
To: PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Sub-query having NULL row returning FALSE result
Date: 2016-06-29 06:07:26
Message-ID: CAGuFTBU8bTOSpdgrU7UUVCryUm4nctOUy_-fE=tO1rzvY9F1Xw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi

Please go through below case

postgres=# CREATE TABLE emp (id INTEGER unique, ename VARCHAR);
CREATE TABLE
postgres=# INSERT INTO emp VALUES (null, 'aaa');
INSERT 0 1
postgres=# INSERT INTO emp VALUES (null, 'bbb');
INSERT 0 1
postgres=# INSERT INTO emp VALUES (3, 'ccc');
INSERT 0 1
postgres=# INSERT INTO emp VALUES (4, 'ddd');
INSERT 0 1
postgres=# SELECT * FROM emp ;
id | ename
----+-------
| aaa
| bbb
3 | ccc
4 | ddd
(4 rows)

postgres=# SELECT * FROM (SELECT 5::integer id, 'eee'::varchar ename) nr;
id | ename
----+-------
5 | eee
(1 row)

postgres=# INSERT INTO emp SELECT * FROM (SELECT 5::integer id,
'eee'::varchar ename) nr WHERE id NOT IN (SELECT id FROM emp);
INSERT 0 0
postgres=# SELECT * FROM emp ;
id | ename
----+-------
| aaa
| bbb
3 | ccc
4 | ddd
(4 rows)

postgres=#

The application is generating SQL-Statement to avoid exception while
inserting

The expected behavior is to INSERT row if the NEW id is not existing in
table, but this is doing FALSE

Please advise me if am doing something wrong here or any alternate

but this is working with other databases

Thanks
Sridhar

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2016-06-29 06:28:03 Re: Sub-query having NULL row returning FALSE result
Previous Message Tom Lane 2016-06-28 18:46:05 Re: cache lookup failed for index