I have two tables:
CREATE TABLE "user"
(
username character varying(35) NOT NULL,
CONSTRAINT user_pk PRIMARY KEY (username)
)
CREATE TABLE item
(
id serial NOT NULL,
username character varying(35),
user_item_number integer,
item_value character varying(35),
CONSTRAINT item_pk PRIMARY KEY (id),
CONSTRAINT item_fk FOREIGN KEY (username)
REFERENCES "user" (username) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT unique_item_username UNIQUE (username, user_item_number)
)
I would like to auto increment user_item_number
separately for each username
. Following image shows example. For each username
: (user1
, user2
) user_item_number
starts form 1 and is incremented by 1.
I suppose that I should use some trigger before insert which get max value of user_item_number
for username
which is inserted and increment it. But I don't know how to write that trigger.
I also don't know how to consider a concurency access (concurency insert more than one row with the same value of username
). I don't want to get constraint violation error when two rows with the same username
and user_item_number
are inserted, I would like that trigger catch that error and one again increment user_item_number
value.
Any idea?