default column value using a function, trigger, or whatever

Поиск
Список
Период
Сортировка
Искать

default column value using a function, trigger, or whatever

От:
"Roderick A. Anderson" <raanders@tsmg.us>
Дата:
I am trying to come up with a method to have a default value for a 
column based on a function and other columns.  I'm hoping ( well not too 
much ) that what I figure out here will apply to MS SQL Server as I am 
stuck using it unless I can prove there ain't no way it's going to 
happen.  ( Now that the embarrassment of being stuck using SQL Server is 
out of the way. )

I'd like to populate a column with the md5 of ColA || ':' || ColB || ':' 
  || ColC.  A search of the archives ( with the terms I was using ) 
didn't turn up anything useful.

Would this be a function or a trigger?

Actually I can probably find examples in the two PostgreSQL books ( 
Bruce's and the one from Command Prompt ) I have when I get home tonight 
but I couldn't in the docs.  There used to be a PostgreSQL Cookbook but 
it can't find it -- even doing the Google thing.


Thought, ideas, pointers are welcome.
Rod
-- 


---
[This E-mail scanned for viruses by Declude Virus]

Re: default column value using a function, trigger, or

От:
Scott Marlowe <smarlowe@g2switchworks.com>
Дата:
On Tue, 2005-05-03 at 17:31, Roderick A. Anderson wrote:
> I am trying to come up with a method to have a default value for a 
> column based on a function and other columns.  I'm hoping ( well not too 
> much ) that what I figure out here will apply to MS SQL Server as I am 
> stuck using it unless I can prove there ain't no way it's going to 
> happen.  ( Now that the embarrassment of being stuck using SQL Server is 
> out of the way. )
> 
> I'd like to populate a column with the md5 of ColA || ':' || ColB || ':' 
>   || ColC.  A search of the archives ( with the terms I was using ) 
> didn't turn up anything useful.
> 
> Would this be a function or a trigger?

This will be an after trigger function (everything's a function!  woot!
hehe.  seriously though, all triggers are functions, but not all
functions are triggers, in postgresql land).

Basically, you'll need an after trigger that updates the md5 field with
that cola/b/c bit you've got up there.  Not much to it really.  

One of the examples on this page ought to help:

http://www.postgresql.org/docs/8.0/static/plpgsql-trigger.html
FAQ