I have a PostgreSQL database Version 9.5.0.
The table UserComments was created like:
create table UserComments (
userId integer,
commentNr integer,
comment text,
UNIQUE (userId, commentNr)
);
The table cannot be changed neither can I add an extra table.
I would like to make a sequence (in other words: an auto-incrementing value) on commentNr depending on the amount of userId entries.
For each new entry with the same userId its value should be incremented.
Example:
userId | commentNr | comment -------|-----------|--------- 1 | 1 | blabla 1 | 2 | more bla 2 | 1 | myownbla 1 | 3 | evenmorebla
I have already found some answers with "triggers" and "window functions" but none did really work for me since I am not allowed to change the table.
Best solution would be a trigger on each INSERT that automatically counts/computes the commentNr so I can add a new entry with
insert into user(userId, comment) values ('1', 'bla');
Any help would be appreciated.
primary key(id),:: there is no columnidin your table. Please post real code. - joop