database 什么是序列(数据库)?我们什么时候需要它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1649102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:30:20  来源:igfitidea点击:

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

database

提问by Matrix

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

即使有主键,为什么还要创建序列?

回答by Stefan Steinegger

The primary key is a column in a table.

主键是表中的一列。

The primary key needs a unique value, which needs to come from somewhere.

主键需要一个唯一的值,它需要来自某个地方。

The sequence is a feature by some database products which just creates unique values. It just increments a value and returns it. The special thing about it is: there is no transaction isolation, so several transactions can not get the same value, the incrementation is also not rolled back. Without a database sequence it is very hard to generate unique incrementing numbers.

序列是一些数据库产品的一个特性,它只是创建唯一的值。它只是增加一个值并返回它。它的特殊之处在于:没有事务隔离,因此多个事务无法获得相同的值,增量也不会回滚。如果没有数据库序列,就很难生成唯一的递增数字。

Other database products support columns that are automatically initialized with a incrementing number.

其他数据库产品支持使用递增数字自动初始化的列。

There are other means to create unique values for the primary keys, for instance Guids.

还有其他方法可以为主键创建唯一值,例如 Guids。

回答by Pablo Santa Cruz

Sequence will allow you to populate primary key with a unique, serialized number.

Sequence 将允许您使用唯一的序列号填充主键。

It's different from a serialor auto_incremementprimary key in the sense that:

它与 aserialauto_incremement主键的不同之处在于:

  • It is an actual database object (you need to create it):

    sql> create sequence NAME_OF_YOUR_SEQUENCE;

  • You could assign independent permissions to it, to different database users:

    sql> grant select on NAME_OF_YOUR_SEQUENCE to NAME_OF_YOUR_USER;

  • You can use to have a unique number that is different among several tables (not just one). Say you have four tables with numeric primary keys, and you want unique numbers among those four tables. You could use a sequence for that, without having to worry about implementing locking mechanisms to do it 'by hand'.

  • You can change its number to any value you want with alter sequence

  • You can cycle through its numbers

    sql> create sequence NAME_OF_YOUR_SEQUENCE maxvalue 1500 cycle;

  • 它是一个实际的数据库对象(您需要创建它):

    sql> create sequence NAME_OF_YOUR_SEQUENCE;

  • 您可以为它分配独立的权限,给不同的数据库用户:

    sql> grant select on NAME_OF_YOUR_SEQUENCE to NAME_OF_YOUR_USER;

  • 您可以使用一个在多个表(不仅仅是一个)中不同的唯一编号。假设您有四个带有数字主键的表,并且您希望这四个表中有唯一的数字。您可以为此使用一个序列,而不必担心实现锁定机制来“手动”完成。

  • 您可以将其编号更改为您想要的任何值 alter sequence

  • 你可以循环查看它的数字

    sql> create sequence NAME_OF_YOUR_SEQUENCE maxvalue 1500 cycle;

回答by Andrzej Doyle

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).

这两者经常一起使用,为没有合理的“本机”键的实体生成自动主键。但它们是两个不同的概念;您可以拥有在插入期间显式填充主键的表,并且可以拥有用于填充非 PK 列的序列(甚至在存储过程中强制使用,与插入记录不同)。