How to use value generated by GENERATE_UNIQUE() fu

2019-09-06 20:56发布

问题:

I trying to implement the example from this manual Generating Unique Values in DB2 Universal Database but encountered the following problem.

I've two tables,

CREATE TABLE PC_LIST (
        PC_ID CHAR(13) FOR BIT DATA NOT NULL, 
        PC_NAME VARCHAR(100) NOT NULL 

        PRIMARY KEY (ID)
);

And second table

CREATE TABLE PC_DATA (
        ID CHAR(13) FOR BIT DATA NOT NULL, 
        PC_ID CHAR(13) FOR BIT DATA NOT NULL, 
        PC_NAME VARCHAR(100) NOT NULL 

        PRIMARY KEY (ID),
        FOREIGN KEY (PC_ID) REFERENCES PC_LIST(PC_ID)
);

I add records to first table PC_LIST without any issues, and use inside INSERT query GENERATE_UNIQUE() function, everything works fine. When I try to add a record to the second table and copy a value from PC_LIST.PC_ID as is I get an error Value "20150408131401000109000000 " is too long.. When I added a cast like following: CAST('20150408131401000109000000' AS CHAR(13) FOR BIT DATA) I get an error that The INSERT or UPDATE value of the FOREIGN KEY is not equal to any value of the parent key of the parent table.

What am I doing wrong? Thank you.

UPDATE: This code I use to insert data in PC_DATA

INSERT INTO PC_DATA 
(
    ID,
    PC_ID, 
    PC_NAME

) VALUES (GENERATE_UNIQUE(), CAST('20150408131401000109000000' AS CHAR(13) FOR BIT DATA) ,'anatoly-pc');

Of course that PC_ID I've taken from PC_LIST table where it exists.

标签: sql db2