Imagine 2 table with the same structure but that might have some rows not equal for the same primary key.
What really happen when using a where clause like this : " where table1.* <> table2.*
" ?
I "used" it in PostgreSQL but I’m interested for other’s languages behavior with this weird thing.
2
Answers
This statement is comparing, every column together of the first table to every column together of the second table. It is the same as writing the composite type explicitly, which would be required if the columns are not in the same order in both tables.
or even more verbose
But you may want to use
IS DISTINCT FROM
instead of<>
to considernull vs not null
as being different/not equal.In postgres t1.* <> t2.* in this context is expanded to be:
which is the same as:
I think the expansion is a postgres extension to the standard, tuple comparision exists in several other DBMS. You can read about it at https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS
The number of columns is required to be the same when comparing tuples, but
I discovered something peculiar when trying your example:
The last select succeds, despite the tuples having different number of columns. Adding some rows change that:
so it appears as this is not checked when the statement is prepared. Expanding the tuple manually yields an ERROR even with empty tables:
Fiddle