我想在 pg 8.4 中重新创建一个主键列。但我正在尝试的查询不起作用(它实际上没有执行):
update beta
set id=rown
from
(select row_number() as rown
from beta as b over (order by b.id) -- b.id is null on all rows
) q;
我想在 pg 8.4 中重新创建一个主键列。但我正在尝试的查询不起作用(它实际上没有执行):
update beta
set id=rown
from
(select row_number() as rown
from beta as b over (order by b.id) -- b.id is null on all rows
) q;
你试过这个解决方案吗?始终首先备份所有内容。
--Approach 2: Closer to Hubert's Examples
--Advantage: Easy to read - intuitive, doesn't rely on a primary key
--Disadvantage: Creates temp junk in the db
-- which means reusing in same session you must drop
-- and using in nested subquery results may be unpredictable
CREATE TEMP sequence temp_seq;
SELECT nextval('temp_seq') As row_number, oldtable.*
FROM (SELECT * FROM beta) As oldtable;
http://www.postgresonline.com/journal/archives/79-Simulating-Row-Number-in-PostgreSQL-Pre-8.4.html
你应该使用ALTER TABLE
命令
ALTER TABLE beta ADD PRIMARY KEY (id);
这个链接应该有助于http://www.postgresql.org/docs/current/static/sql-altertable.html