Re: defaults referencing other columns?

From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: James Cloos <cloos(at)jhcloos(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: defaults referencing other columns?
Date: 2006-09-04 01:46:50
Message-ID: 20060904014650.14524.qmail@web31801.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

> Can a default value reference the value specified for one of the other
> columns in the insert?

from:
http://www.postgresql.org/docs/8.1/interactive/sql-createtable.html

DEFAULT default_expr
The DEFAULT clause assigns a default data value for the column whose column definition it appears
within. The value is any variable-free expression (subqueries and cross-references to other
columns in the current table are not allowed). The data type of the default expression must match
the data type of the column.

The default expression will be used in any insert operation that does not specify a value for the
column. If there is no default for a column, then the default is null.

> bar TEXT DEFAULT somefunc(id)

"The value is any variable-free expression". To me the "id" in somefunc would classify as a
variable. The other suggestion to use a Rule might work, however a before insert trigger would
diffently work in this case.

However, why would you what to to store the text anyway? If you wanted to see it you could still
generate "on the fly" with a select statement.

select id, somefunc(id) from foo;

Regards,

Richard Broersma Jr.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Richard Broersma Jr 2006-09-04 01:49:52 Re: function parameters
Previous Message Jasbinder Bali 2006-09-03 22:24:54 Re: defaults referencing other columns?