确保唯一ID的PostgreSQL序列

确保唯一ID的PostgreSQL序列

本文介绍了确保唯一ID的PostgreSQL序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有一个表,该表的列ID为主键,而列MyNumber包含由序列myUniqueSequence定义的整数。我想在PostgreSQL中定义myUniqueSequence,它将为MyNumber列返回下一个唯一的自由编号。

Having a table with a columnn ID as primary key, and a column MyNumber that contains integers defined by the sequence myUniqueSequence. I would like to define myUniqueSequence in PostgreSQL that will return the next free and unique number for the column MyNumber.

这意味着,下次以编程方式创建新行时,从数字1开始,如果免费,它将用于myNumber列,否则将尝试2,依此类推。

This means, the next time a new row is created programatically will start by number 1, if it's free it will use it for the column myNumber, if not, it tries with 2 and so on.

推荐答案

对列使用 serial 数据类型(而不是您自己的序列):

Use the serial data type for your column (instead of your own sequence):

这篇关于确保唯一ID的PostgreSQL序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:37