什么是序列?我们什么时候需要一个?

发布于 2024-08-09 05:37:17 字数 27 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揽月 2024-08-16 05:37:17

主键是表中的一列。

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

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

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

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

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 an incrementing number.

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

原野 2024-08-16 05:37:17

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

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

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

    <代码>sql> create sequence NAME_OF_YOUR_SEQUENCE;

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

    <代码>sql> grant select on NAME_OF_YOUR_SEQUENCE to NAME_OF_YOUR_USER;

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

  • 您可以使用 改变序列

  • 您可以循环其数字

    <代码>sql>创建序列 NAME_OF_YOUR_SEQUENCE maxvalue 1500 循环;

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

It's different from a serial or auto_incremement primary key in the sense that:

  • 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;

花伊自在美 2024-08-16 05:37:17

主键(从技术角度来说)只是一个强制唯一性(以及加快查询性能)的索引。其中有一些语义信息是该行描述的实体的“键”,但仅此而已。

序列是完全不同的实体;它与表分开存在(就像存储过程一样),并且可以调用它来生成序列号。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文