在phoenix已有表的结构上如何新增一个自增列ID

探索如下步骤,基本可行。

以笔者开发环境远程服务器上 NOCURRENTEVASIONDIV_FIRSTDIV 表为例子。


---构造临时表,以便将现有数据插入临时表,再从临时表插入到原有表,并且生成自增ID。
CREATE TABLE TEMP_NOCURRENTEVASIONDIV_FIRSTDIV
(
   TS_CODE VARCHAR(6) not null,
   SEG_CODE VARCHAR(3) not null,
   CARTYPECODE VARCHAR(1) not null,
   ISTRUCK INTEGER not null,
   TOLLBYWEIGHT INTEGER not null,
   REPORTTIME INTEGER not null,
   DIVCHARGE DECIMAL not null,
   CREATETIME DATE not null,
   TICKET_CODE VARCHAR(6) not null,
   TSEG_CODE VARCHAR(3) not null,
   PUNITIVETYPE VARCHAR(2) not null,
    CONSTRAINT NOCURRENTEVASIONDIV_FIRSTDIV_KEY PRIMARY KEY (TS_CODE,SEG_CODE,CARTYPECODE,ISTRUCK,TOLLBYWEIGHT,REPORTTIME,DIVCHARGE,CREATETIME,TICKET_CODE,TSEG_CODE,PUNITIVETYPE)
)

---将原表的数据灌入临时表
upsert into TEMP_NOCURRENTEVASIONDIV_FIRSTDIV select * from NOCURRENTEVASIONDIV_FIRSTDIV
---建立原表的自增序列
CREATE SEQUENCE NOCURRENTEVASIONDIV_FIRSTDIV_SEQ

---删除原表数据
DELETE FROM  NOCURRENTEVASIONDIV_FIRSTDIV
--删除原表,重建原表结构,加入ID列
drop table NOCURRENTEVASIONDIV_FIRSTDIV;
CREATE TABLE NOCURRENTEVASIONDIV_FIRSTDIV
(  ID BIGINT NOT NULL,
   TS_CODE varchar(6),
   SEG_CODE varchar(3),
   CARTYPECODE varchar(1),
   ISTRUCK integer,
   TOLLBYWEIGHT integer,
   REPORTTIME integer,
   DIVCHARGE DECIMAL,
   CREATETIME date,
   TICKET_CODE varchar(6),
   TSEG_CODE varchar(3),
   PUNITIVETYPE varchar(2),
   CONSTRAINT NOCURRENTEVASIONDIV_FIRSTDIV_KEY PRIMARY KEY (ID)
);
--插入包含自增ID的数据
UPSERT INTO NOCURRENTEVASIONDIV_FIRSTDIV SELECT NEXT VALUE FOR NOCURRENTEVASIONDIV_FIRSTDIV_SEQ,TS_CODE,SEG_CODE,CARTYPECODE,ISTRUCK,TOLLBYWEIGHT,REPORTTIME,DIVCHARGE,CREATETIME,TICKET_CODE,TSEG_CODE,PUNITIVETYPE from 
TEMP_NOCURRENTEVASIONDIV_FIRSTDIV
--验证原表数据
select count(*) from NOCURRENTEVASIONDIV_FIRSTDIV --看记录是否和临时表一样? 934786条
--删除临时表
delete from TEMP_NOCURRENTEVASIONDIV_FIRSTDIV;
drop TEMP_NOCURRENTEVASIONDIV_FIRSTDIV;

你可能感兴趣的:(Java,Web开发,大数据)