Re: Appending values non-destructively - Mailing list pgsql-novice

From Henshall, Stuart - WCP
Subject Re: Appending values non-destructively
Date
Msg-id E2870D8CE1CCD311BAF50008C71EDE8E01F7488A@MAIL_EXCHANGE
Whole thread Raw
In response to Appending values non-destructively  (Andre Dubuc <aajdubuc@webhart.net>)
List pgsql-novice
Hello,
    This sounds like a misunderstanding of how relational databases
work.
You have given no idea of the current schema but assuming that you have a
table with sponsor currently in it which we'll call tbl_a. We'll also assume
it has a primary key called pk_a. What you could do is create another table
as follows:
CREATE tbl_b (
    pk_b    SERIAL,
    sponsor    int4,
    pk_a        int4,
    PRIMARY KEY (pk_b)
    );
This table will have a 0 to n relationship with tbl_a by way of pk_a.
If you want you could use foriegn keys to enforce a relationship.
When ever you want to change the sponsor field you just INSERT another value
into this table (eg INSERT (sponsor,pk_a) VALUES (sponsor_id,<tbl_a's
pk_a>);
You can tell which is the currrent sponsor as this will have the highest
value of pk_b.
Alternatively you could UPDATE the record in sponsor, while either INSERTing
the last value into sponsor, or alternatively the new value (which would
require you to do this on INSERTS into tbl_a as well). This approach has the
advantage of speedier queries to find the current sponsor. Also rather than
having the application doing the updates you could use triggers to
automatically do the INSERTS into tbl_b.
Of course if you are infact just wanting a text representation of these
numbers you could just do something like:
UPDATE tbl_name SET sponsor=sponsor || '', 567'' WHERE ....
Hope this helps,
- Stuart

> -----Original Message-----
> From: Andre Dubuc [mailto:aajdubuc@webhart.net]
>
> Using PostgreSQL 7.2 with PHP 4.1.2,  I would like to use the
> UPDATE function
> to add values to a field in a row that already has a value or
> values, i.e.:
> in a field named 'sponsor', the current value is '2588'. I
> would like the new
> value in 'sponsor' to become '2588, 2961', and later , '2588,
> 2961, 3166' etc.
>
> From my newbie reading, I haven't discovered whether it is
> possible to append
> values to a field without destroying the current value, and
> further, whether
> comma separators (or any sprators for that fact) are allowed
> or even possible.
>
> If some kind guru could help out with this simple question, I
> would greatly
> appreciate a small example code showing how to do it.
>
> Tia,
> Andre

pgsql-novice by date:

Previous
From: "Joshua b. Jore"
Date:
Subject: Re: Appending values non-destructively
Next
From: "Reinhard Hnat"
Date:
Subject: View don't know table