可以凝结主键/序列? [英] Possible to condense primary key/serial?

查看:188
本文介绍了可以凝结主键/序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,一切都与外键链接,所以Postgres完全知道数据库是如何布局的。

好吧,让我们说我有Table1和Table2。

Table1有3个字段。 RID,table2_rid,data
$ b $所以table1.table2_rid引用table2.RID,这是用一个外键表示的。在 RID 字段中都是主键,并且是一个序列类型。



我想知道的是如何凝结主键?比如说你添加了5条记录,删除了3条记录。你的主键看起来就像

  1 
2


$ / code>

那么,我怎样才能到处更新,键(和相应的外键)可以被压缩成

  1 
2
3
4


解决方案

最好不要改变主键:重新编号他们是一个PITA。



如果您需要一个人类的ID,一个没有空白的ID,Elein Mustain显示如何创建无间隙序列。


I have a database where everything is linked with foreign keys, so the Postgres knows exactly how the database is layed out..

Well, Lets say I have Table1 and Table2.

Table1 has 3 fields. RID, table2_rid,data

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.

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

解决方案

It's best if a primary key never changes: Renumbering them is a PITA.

If you need an ID for humans, one that has no gaps, A. Elein Mustain shows how to create a gapless sequence.

这篇关于可以凝结主键/序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆