Thread: Primary Key with auto increment field

Primary Key with auto increment field

From
"x asasaxax"
Date:
I would like to increment one field, depending of the value of the other. here´s an example:

id variable
1 1
2 1
1 2
2 2

can i do that with the following commands?

SELECT setval('sequence',(SELECT max(id) FROM table)) INTO variable;
insert into table values(variable, ..., ...); ?

Will this be transactional? Cause, they say that setval is a command that its transactional. Using this way i

don´t will need to use a sequence anymore. Is that correct?


Thanks you all.

Re: Primary Key with auto increment field

From
Craig Ringer
Date:
x asasaxax wrote:
> I would like to increment one field, depending of the value of the
> other. here´s an example:

Didn't you just ask this question with practically the same wording a
few days ago?

What was wrong with the answers then? What has changed? What's the
*difference* this time?

Repeating your question won't help, you need to ask something more
specific or at least different if you want a useful answer.

If you did not understand the examples you were given last time (which
it sounds like you didn't given your final message) then I strongly
suggest AVOIDING this approach. It is complex, and the solutions
suggested to you require you to understand some PL/PgSQL and a bit about
how locking is used. Are you SURE you can't just use a normal sequence
instead?

--
Craig Ringer