I know that you can use = DEFAULT
in UPDATE
statements, e.g.
UPDATE my_table
SET some_column = DEFAULT
WHERE id = 1;
Is it possible to look for defaults in a SELECT
statement? e.g.
SELECT id FROM my_table
WHERE some_column = DEFAULT;
That specific syntax gives the error "DEFAULT is not allowed in this context."
2
Answers
No, you cannot do that, and since a column’s
DEFAULT
value can be an expression, it would be difficult to fake.This answer shows how to get the DEFAULT value stored in the information_schema.
The problem is that the default value is stored as text, so you see e.g. a string
'hello'::text
which is difficult to compare with the column value.Anyway you may use a trick using a helper table illustrated below.
Create table
and put some data in it
now create a clone table (
like
) and insert one row with DEFAULT valuesquery is trivial only you must hanle the case of
null
default value in extraOR
predicate