postgres是否会自动为每一行生成一个ID? [英] Does postgres automatically generate an id for every row?
问题描述
我一般对Postgres和SQL还是陌生的。我来自NoSQL。更具体的mongodb。在mongodb中,每个文档都有一个由mongodb自动生成的唯一ID。
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.
postgres行会自动具有唯一的ID还是您必须自己生成它们?
Do postgres rows automatically have a unique id or do you have to generate them yourself?
推荐答案
您需要为自己定义一个:
You need to define one for yourself:
create table foo
(
id integer generated always as identity primary key,
.... other columns
);
在关系数据库中,您必须自己定义主键,数据库不会对此进行假设。例如通常,用于多对多关系的链接表根本没有生成的PK:
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)
);
这篇关于postgres是否会自动为每一行生成一个ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!