开发者

CREATE TABLE AS - how to add column with PK?

I have to create a table (H2 embedded database) using fields from other tables. I decided to use CREATE TABLE AS statement.

My code:

CREATE TABLE DOC AS

SELECT I.ID, I.STATUS, A.REMINDERINFORMATION

FROM IE802 I JOIN IE802_ATTRIBUTES A ON A.IE802_ID=I.ID;

Each row which is generated from the code above additionally must have DOCID PrimaryKey. How can I add 开发者_如何学Pythonthis column and make it autoincrement and PK at the same time?

Thanks for any tips and other solutions!

Alternatively, how can I make existing I.ID to be PK?

I'm still getting an error: Column "ID" must not be nullable; SQL statement: ALTER TABLE DOC ADD PRIMARY KEY (ID) [90023-147]


H2 supports column definitions in CREATE AS SELECT:

CREATE TABLE DOC(
    ID INT PRIMARY KEY, 
    STATUS INT, 
    REMINDERINFORMATION VARCHAR(255)
) 
AS SELECT I.ID, I.STATUS, A.REMINDERINFORMATION
FROM IE802 I JOIN IE802_ATTRIBUTES A ON A.IE802_ID=I.ID;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜