简体   繁体   English

如何在不转储的情况下更改postgresql中列的位置

[英]How to change the position of column in postgresql without dumping

I know the only workaround is to dump the table and then recreate the whole database with correct positioning of the required columns.我知道唯一的解决方法是转储表,然后使用所需列的正确定位重新创建整个数据库。

But the problem is one of the columns is a foreign key to many tables in the database so it would not be possible to just delete the whole table.但问题是其中一列是数据库中许多表的外键,因此不可能只删除整个表。 Also I cannot delete any column as the foreign key column lies to the last of the table.我也不能删除任何列,因为外键列位于表的最后一个。 Please give me a solution.请给我一个解决方案。

If this question is a duplicate please give me the link to the correct answer.如果这个问题是重复的,请给我正确答案的链接。

edit: for more clarification i want to add rows using the insert command and the problem is the second last column is of the type serial.编辑:为了更多说明,我想使用插入命令添加行,问题是倒数第二列的类型为串行。 My main aim is to not touch the serial column while giving the insert command我的主要目标是在给出插入命令时不接触串行列

All you need is a custom insert statement,您只需要一个自定义的插入语句,

For example,例如,

Your table looks like this,你的桌子看起来像这样,

CREATE TABLE "public"."ada" (
    
    "trandate" date, 
    "locname" text, 
    "totusers" integer, 
    "actusers" integer, 
    "datausage" integer, 
    "issues" integer, 
    "id" serial PRIMARY KEY, 
    "issuessolved" integer
);

and the insert statement can be written as并且插入语句可以写成

INSERT INTO "public"."ada" (
    "trandate"
    ,"locname"
    ,"totusers"
    ,"actusers"
    ,"datausage"
    ,"issues"
    ,"issuessolved"
    )
VALUES (
    < trandate
    ,date >
    ,< locname
    ,text >
    ,< totusers
    ,integer >
    ,< actusers
    ,integer >
    ,< datausage
    ,integer >
    ,< issues
    ,integer >
    ,< issuessolved
    ,integer >
    );

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM