[GENERAL] default value based on select

2008-04-05 Thread Pedro Doria Meunier
Hi,

Is it possible to have a table's column default value set to some form of 
select?

e.g. picking another table's value for a given SELECT ... WHERE ...

Already thankful for any insight,
-- 
Pedro Doria Meunier
Ips. da Olaria, Edf. Jardins do Garajau, 4, r/c Y
9125-162 Caniço
Madeira - Portugal
--
Skype : pdoriam
Mobile: +351961720188


signature.asc
Description: This is a digitally signed message part.


Re: [GENERAL] default value based on select

2008-04-05 Thread Volkan YAZICI
On Sat, 5 Apr 2008, Pedro Doria Meunier [EMAIL PROTECTED] writes:
 Is it possible to have a table's column default value set to some form of 
 select?

AFAIK, you cannot provide sub-selects in the default values of a
field. E.g.

  CREATE TABLE foo (bar int DEFAULTS (SELECT ...), ...);

For this, I know two solutions:

1. You can create an SQL function that issues the related sub-select and
   call this function as the default value of the column.

2. Create a BEFORE INSERT/UPDATE trigger that issues the related
   sub-select if related column appears to be un-specified.


Regards.

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] default value based on select

2008-04-05 Thread Pavel Stehule
Hello

On 05/04/2008, Pedro Doria Meunier [EMAIL PROTECTED] wrote:
 Hi,

  Is it possible to have a table's column default value set to some form of
  select?


directly no, but you can use function

postgres=# create table a(a integer);
CREATE TABLE

postgres=# create function mx() returns int as $$select max(a) from
a$$ language sql volatile;
CREATE FUNCTION
postgres=# create table b(x integer default mx());
CREATE TABLE

Regards
Pavel Stehule
  e.g. picking another table's value for a given SELECT ... WHERE ...

  Already thankful for any insight,

 --
  Pedro Doria Meunier
  Ips. da Olaria, Edf. Jardins do Garajau, 4, r/c Y
  9125-162 Caniço
  Madeira - Portugal
  --
  Skype : pdoriam
  Mobile: +351961720188



-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] default value based on select

2008-04-05 Thread Pedro Doria Meunier
Pavel and Volkan

Thank you for your kind (and prompt) replies.

Let me try to better explain my needs:
The record being created has a column with an Id that has to be passed as an 
argument to the function that, in turn, fills the other column in the same 
record.

Does this even make sense? :)

Kind regards,
-- 
Pedro Doria Meunier
Ips. da Olaria, Edf. Jardins do Garajau, 4, r/c Y
9125-162 Caniço
Madeira - Portugal
--
Skype : pdoriam
Mobile: +351961720188


signature.asc
Description: This is a digitally signed message part.


Re: [GENERAL] default value based on select

2008-04-05 Thread Volkan YAZICI
On Sat, 5 Apr 2008, Pedro Doria Meunier [EMAIL PROTECTED] writes:
 The record being created has a column with an Id that has to be passed as an 
 argument to the function that, in turn, fills the other column in the same 
 record.

Doesn't BEFORE INSERT/UPDATE trigger solve this problem? (If not, I
wonder what is missing.)


Regards.

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general