Re: SQL (?) question
| От | Mike Mascari |
|---|---|
| Тема | Re: SQL (?) question |
| Дата | |
| Msg-id | 40FD8D8C.6000306@mascari.com обсуждение исходный текст |
| Ответ на | SQL (?) question ("Joost Kraaijeveld" <J.Kraaijeveld@Askesis.nl>) |
| Список | pgsql-general |
Joost Kraaijeveld wrote:
> Hi all,
>
> Is there an easy way (e.g. short SQL script) to add a column to a
> table and fill it with integers (from 1- n) just like using an
> sequence? (I need to retofit a system id that is handled in the
> application, not in the database).
I assume there is some unique candidate key on the table already (or
worst case, an OID.) If so, I'd just do:
CREATE SEQUENCE my_sequence;
CREATE TEMPORARY TABLE foo AS
SELECT candidate_key, nextval('my_sequence') as new_key
FROM table_that_needs_fixed;
ALTER TABLE table_that_needs_fixed
ADD COLUMN new_key integer;
UPDATE table_that_needs_fixed
SET new_key = foo.new_key
WHERE candidate_key = foo.candidate_key;
Add appropriate constraints as necessary...
HTH,
Mike Mascari
В списке pgsql-general по дате отправления: