views:

88

answers:

2

I'm working with some code. There are several queries whose effect is, if the row exists with some of the data filled in, then that row is updated with the rest of the data, and if the row does not exist, a new one is created. They look like this:

INSERT INTO table_name (col1, col2, col3)
SELECT %s AS COL1, %s AS COL2, %s AS COL3
FROM ( SELECT %s AS COL1, %s AS COL2, %s AS COL3 ) A
LEFT JOIN table_name B
ON  B.COL1 = %s
AND B.COL2 = %s     --note: doesn't mention all columns here
WHERE B.id IS NULL
LIMIT 1

I can mimic this pattern and it seems to work, but I'm confused as to what is actually going on behind the scenes. Can anyone elucidate how this actually works? I'm using PostgreSQL.

+1  A: 

The LEFT JOIN/IS NULL means that the query is INSERTing records that don't already exist. That's assuming the table defined on the INSERT clause is the same as the one in the LEFT JOIN clause - careful about abstracting...

I'm interested to know what %s is

OMG Ponies
I suspect this is the format string for a 'sprintf()'-like function, and the '%s' will be replaced with some sort of name or value before the SQL is sent to the DBMS.
Jonathan Leffler
yep, the %s is data (you can assume it's "foo", "bar", "baz" for this example) to be put in the table.
Claudiu
+3  A: 

Hi,

Are you sure that is updating using only that piece of code?

What is happing is that you are doing left join with the table_name (the table to insert new records) and filtering only for rows that doesn't exist in that table. (WHERE B.id IS NULL)

Is like doing "not exists", only in a different way.

I hope my answer could help you.

Regards.

Bruno Costa
ah so this won't actually update an entry that already exists? it's just making sure duplicate rows don't get created?
Claudiu
Yup, the where clause is only to make sure that is no duplication.
Bruno Costa
is t here any difference to doing this vs. something like: `INSERT INTO table_name (col1, col2, col3) SELECT %s, %s, %s WHERE NOT EXISTS (SELECT * FROM table_name as T WHERE T.col1 = %s AND T.col2 = %s)`?
Claudiu
I believe that doing left join have a better performance that the not exists in where clause. But I'm not sure. I've to test that :-).
Bruno Costa