如何在 PostgreSQL 13 中将 id 更新为行号

Dol*_*hin 2 postgresql

我的表article_favorites没有行号,现在它包含 100000 条记录。我想在表中添加一个 bigint id 列,并将 id 默认设置为行号。我尝试了这个sql:

update article_favorites  set id = row_number() OVER ()
Run Code Online (Sandbox Code Playgroud)

但它告诉我window functions are not allowed in UPDATE,我应该怎么做才能将 id 更新为行号?

小智 5

使用序列可能比使用更快row_number()

create sequence id_seq;
update article_favorites  
  set id = nextval('id_seq');
drop sequence id_seq;
Run Code Online (Sandbox Code Playgroud)