H2 Database - Reorder columns using SQL

送分小仙女□ 提交于 2019-12-12 15:34:24

问题


I have a H2 database with 16 million entries and no primary key. I successfully added an auto-incrementing primary key using the following statements:

ALTER TABLE
    PUBLIC.ADDRESSES ADD ID BIGINT AUTO_INCREMENT;
ALTER TABLE
    PUBLIC.ADDRESSES ADD PRIMARY KEY (ID)

Now the problem is, that the column order is STREET, HOUSENUMBER, ..., ID, but I would like ID to be the first column of the table. It looks like there is a corresponding ALTER TABLE statement MySQL (see here), but I'm unable to adapt it to H2.

Long story short: How can I change the column order to ID, STREET, HOUSENUMBER ...? Is there a solution similar to:

ALTER TABLE "ADDRESSES" MODIFY COLUMN "ID" BEFORE "STREET";

Any help is kindly appreciated.


回答1:


H2 does not currently support re-ordering columns. You would need to run multiple statements:

  • First, rename the column, then add a new column with the right name at the right position (alter table add supports positioning), and finally drop the old column.

  • Or, probably more elegant, use rename table and then create table ... as select.



来源:https://stackoverflow.com/questions/22812765/h2-database-reorder-columns-using-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!