What is a sequence (Database)? When would we need it?

后端 未结 3 1921
暗喜
暗喜 2020-11-29 02:40

Why would we create a sequence even if there is a primary key?

3条回答
  •  孤独总比滥情好
    2020-11-29 03:42

    The primary key is (in technical terms) merely an index that enforces uniqueness (as well as speeding query performance). There's some semantic information there along that being the "key" for the entity the row describes, but that's it.

    A sequence is a different entity entirely; it exists separate from tables (like a stored procedure would) and can be called to yield sequential numbers.

    The two are often used together, to generate automatic primary keys for entities that have no sensible "native" keys. But they are two separate concepts; you can have tables where the primary key is explicitly populated during an insert, and you can have sequences that are used to populate non-PK columns (or even used imperatively during a stored procedure, distinct from inserting records).

提交回复
热议问题