简体   繁体   English

我不明白 postgresql 的 nextval() 是如何工作的,有人可以解释一下吗?

[英]I don't understand how postgresql's nextval() work, can someone explain?

I'm just starting to wade into backend development after my first few months on the job as a front end dev.在我担任前端开发人员的头几个月后,我才刚刚开始涉足后端开发。 I'm working with postgreSQL and can't seem to wrap my head around the nextval() function.我正在使用 postgreSQL,但似乎无法理解 nextval() 函数。 I read this, but it's not clear to me.我读过这个,但我不清楚。 http://www.postgresql.org/docs/current/interactive/functions-sequence.html what are the benefits/use cases for nexval()? http://www.postgresql.org/docs/current/interactive/functions-sequence.html nexval() 的好处/用例是什么?

NEXTVAL is a function to get the next value from a sequence. NEXTVAL是从序列中获取下一个值的函数。

Sequence is an object which returns ever-increasing numbers, different for each call, regardless of transactions etc. Sequence 是一个对象,它返回不断增加的数字,每次调用都不同,无论交易等如何。

Each time you call NEXTVAL , you get a different number.每次调用NEXTVAL ,都会得到一个不同的数字。

This is mainly used to generate surrogate primary keys for you tables.这主要用于为您的表生成代理主键。

You can create a table like this:您可以像这样创建一个表:

CREATE SEQUENCE mysequence;
CREATE TABLE mytable (id BIGINT NOT NULL PRIMARY KEY, value INT);

and insert values like this:并插入这样的值:

INSERT
INTO    mytable (id, value)
VALUES
        (NEXTVAL('mysequence'), 1),
        (NEXTVAL('mysequence'), 2);

and see what you get:看看你得到了什么:

SELECT * FROM mytable;
 id | value
----+-------
  1 |     1
  2 |     2

PostgreSQL offers a nice syntax sugar for this: PostgreSQL 为此提供了一个很好的语法糖:

CREATE TABLE mytable (id BIGSERIAL PRIMARY KEY, value INT);

which is equivalent to这相当于

CREATE SEQUENCE mytable_id_seq; -- table_column_'seq'
CREATE TABLE mytable (id BIGINT NOT NULL PRIMARY KEY DEFAULT NEXTVAL('mytable_id_seq'), value INT); -- it's not null and has a default value automatically

and can be used like this:并且可以这样使用:

INSERT
INTO    mytable (value)
VALUES  (1),
        (2);  -- you can omit id, it will get filled for you.

Note that even if you rollback your insert statement or run concurrent statements from two different sessions, the returned sequence values will never be the same and never get reused (read the fine print in the docs though under CYCLE ).请注意,即使您回滚插入语句或从两个不同的会话运行并发语句,返回的序列值也永远不会相同并且永远不会被重用(尽管在CYCLE下阅读文档中的CYCLE )。

So you can be sure all the values of your primary keys will be generated unique within the table.因此,您可以确保主键的所有值都将在表中生成唯一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不了解自我加入的必要性。 有人可以向我解释一下吗? - I don't understand the need for self-joins. Can someone please explain them to me? 我的Postgres数据库没有使用我的索引; 我解决了,但不了解解决方法,有人可以解释发生了什么吗? - My Postgres database wasn't using my index; I resolved it, but don't understand the fix, can anyone explain what happened? 我不明白为什么其中一个有效,另一个没有。 请解释我是如何错误地处理这个字符串的 - I don't understand why one of these works and the other doesn't. Please explain how I'm handling this string incorrectly 我遇到了一个奇怪的问题,使用 NOT IN 对 PostgreSQL 数据库进行查询,但不明白为什么它不起作用 - I hit a weird issue running a query against a PostgreSQL Database using NOT IN and don't understand why it didn't work 为什么需要运行postgresql nextval函数? 以及如何预防呢? - Why do I need to run the postgresql nextval function? And how to prevent it? 有人可以解释这个SQL吗? (以及如何“参数化”它并作为函数调用?) - Can someone explain this SQL? (and how may I 'parametrize' it and invoke as a function?) 如何编写 postgresql 查询以在数据库中查找某人的堂兄? - how can I write a postgresql query to find someone's cousin in database? 有人可以帮助解释此子查询及其工作方式 - Can Someone Help Explain this Subquery and How it Works 有人可以向我解释这个陈述是如何排除的吗? - Can someone explain to me how this statement is an exclude? 有人可以解释一下 sql 查询是如何工作的吗? - Can someone explain how the sql query works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM