How to use value generated by GENERATE_UNIQUE() function as foreign key?

。_饼干妹妹 提交于 2019-12-12 06:20:54

问题


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.

来源:https://stackoverflow.com/questions/29516596/how-to-use-value-generated-by-generate-unique-function-as-foreign-key

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