什么是序列?我们什么时候需要一个?
即使有主键,为什么我们还要创建一个序列?
Why would we create a sequence even if there is a primary key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
即使有主键,为什么我们还要创建一个序列?
Why would we create a sequence even if there is a primary key?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
主键是表中的一列。
主键需要一个唯一的值,该值需要来自某个地方。
序列是某些数据库产品的一个功能,它只是创建唯一的值。它只是增加一个值并返回它。它的特别之处在于:没有事务隔离,因此多个事务无法获得相同的值,增量也不会回滚。如果没有数据库序列,就很难生成唯一的递增数字。
其他数据库产品支持使用递增数字自动初始化的列。
还有其他方法可以为主键创建唯一值,例如 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.
序列将允许您使用唯一的序列号填充主键。
它与
serial
或auto_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
orauto_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;
主键(从技术角度来说)只是一个强制唯一性(以及加快查询性能)的索引。其中有一些语义信息是该行描述的实体的“键”,但仅此而已。
序列是完全不同的实体;它与表分开存在(就像存储过程一样),并且可以调用它来生成序列号。
两者经常一起使用,为没有合理“本机”键的实体生成自动主键。但它们是两个不同的概念;您可以拥有在插入期间显式填充主键的表,并且可以拥有用于填充非 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).