PostgreSQL query to rename and change column type with single query

前端 未结 2 1947
执笔经年
执笔经年 2021-01-31 02:16

In PostgreSQL if I need to rename and change a column data type, I run two separate queries to do so.

To rename:

AL         


        
2条回答
  •  感动是毒
    2021-01-31 02:35

    In PostgreSQL, ALTER TABLE can take a series of operations. So:

    ALTER TABLE  RENAME  TO ;
    ALTER TABLE  ALTER COLUMN  TYPE ;
    

    is the same as

    ALTER TABLE  
      ALTER COLUMN  TYPE 
      RENAME  TO ;
    

    However... why? IIRC the rename won't cause a full-table scan, so there's no benefit over just doing the two statements separately, within one transaction. What problem are you actually trying to solve with this?

提交回复
热议问题