Question


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 this 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]

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top