I am new to postgres and SQL in general. I am coming from NoSQL. More specific mongodb. In mongodb every document had a unique id automatically generated by mongodb.
Do postgres rows automatically have a unique id or do you have to generate them yourself?
Advertisement
Answer
You need to define one for yourself:
x
create table foo
(
id integer generated always as identity primary key,
other columns .
);
In a relational database you have to define the primary key yourself, the database does not make assumptions about that. e.g. a link table for a many-to-many relationship usually doesn’t have a generated PK at all:
create table many_to_many
(
one_id integer not null references table_one,
other_id integer not null references other_table,
primary key (one_id, other_id)
);