Let's suppose I have a table with a composite primary key, as follows:
CREATE TABLE fruits(
id SERIAL NOT NULL,
name VARCHAR NOT NULL,
PRIMARY KEY(id , name)
);
Had name
not be a primary key too, we could have expected this behaviour:
id name
1 banana
2 apple
3 peach
4 banana
5 apple
However, I want id
, the first of the two primary keys, to be incremented for each name
, in order to have an increment of id
for EACH value of name
.
Is it possible, with any data type like SERIAL
or with any feature provided by postgres, to reach the following behaviour when you have a composite key, without having to add extra logic ( like a TRIGGER
) for each new row of fruits
and to reach an example such as follows?
id
just has to be an incrementing integer.
id name
1 banana
1 apple
1 peach
2 banana
3 banana
2 peach