Shift (update) unique column values in PostgreSQL

后端 未结 2 2000
星月不相逢
星月不相逢 2021-01-13 06:40

Using MS SQL Server, the following works fine:

CREATE TABLE #temptable(mykey int primary key)

INSERT INTO #temptable VALUES (1)
INSERT INTO #temptable VALUE         


        
2条回答
  •  粉色の甜心
    2021-01-13 06:55

    Solution without altering constraint as deferrable initially immediate

    UPDATE tbl_test t1 
    SET    testkey = t2.testkey + 1 
    FROM   (SELECT testkey 
        FROM   tbl_test 
        ORDER  BY testkey DESC) t2 
    WHERE  t1.testkey = t2.testkey 
    

    Online example: http://rextester.com/edit/GMJ48099

提交回复
热议问题