可以压缩主键/序列吗? [英] Possible to condense primary key/serial?
问题描述
我有一个数据库,其中所有内容都与外键链接,因此 Postgres 确切地知道数据库的布局..
I have a database where everything is linked with foreign keys, so the Postgres knows exactly how the database is layed out..
好吧,假设我有 Table1 和 Table2.
Well, Lets say I have Table1 and Table2.
Table1 有 3 个字段.RID、table2_rid、数据
Table1 has 3 fields. RID, table2_rid,data
所以 table1.table2_rid 引用 table2.RID 并用外键表示.在这两个RID
字段中都是主键并且是序列类型.
So table1.table2_rid references table2.RID and this is expressed with a foreign key. In both the RID
field is the primary key and is a serial type.
我想知道的是如何压缩"主键?比如说你添加了 5 条记录并删除了第 3 条记录.你的主键看起来像
What I would like to know is how to "condense" the primary keys? Like say you add 5 records and deleted record number 3. Your primary keys would look like
1
2
4
5
那么,我如何才能在所有地方进行更新,以便可以将主键(和相应的外键)压缩成
Well, how do I get to update everywhere so that the primary key(and corresponding foreign keys) can be condensed into
1
2
3
4
推荐答案
最好不要改变主键:重新编号是一个 PITA.
It's best if a primary key never changes: Renumbering them is a PITA.
如果您需要一个没有间隙的人类 ID,A. Elein Mustain 显示 如何创建无间隙序列.
If you need an ID for humans, one that has no gaps, A. Elein Mustain shows how to create a gapless sequence.
这篇关于可以压缩主键/序列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!