简体   繁体   English

创建外键时,DB2 SQL错误-104

[英]DB2 SQL error -104 when creating foreign key

I'm trying to create temporary tables to do some testing and I'm facing this error: 我正在尝试创建临时表以进行一些测试,并且遇到此错误:

[DECLARE - 0 row(s), 0.000 secs]  [Error Code: -104, SQL State: 42601]  DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=key;(
id int primary;<references_spec>, DRIVER=3.50.152

when trying to create 2 temporary tables with 1 foreign key, as follows: 尝试使用1个外键创建2个临时表时,如下所示:

declare global temporary table session.company (
    id_comp int not null generated always as identity (start with 0 increment by 1),
    name_comp varchar(60)
    ) with replace on commit preserve rows not logged;

delete from session.company;

declare global temporary table session.employee (
    id_emp int not null generated always as identity (start with 0 increment by 1),
    name varchar(40),
    id_comp int,
        constraint fk_id_comp
            foreign key (id_comp)
            references session.company (id_comp)
            on delete cascade
    ) with replace on commit preserve rows not logged;

delete from session.employee;

If I remove the constraint part, it executes fine. 如果我删除constraint部分,它会正常执行。 I've tried both references session.company (id_comp) and references session.company.id_comp , with the same result. 我已经尝试了references session.company (id_comp)references session.company.id_comp ,结果相同。

How can I solve this? 我该如何解决?

Thanks in advance. 提前致谢。

UPDATE: 更新:

It might be considered a different question but, as it was suggested that I generate id_comp as a PRIMARY_KEY in session.company , I can't get it work this way neither. 它可能被认为是一个不同的问题,但是,由于有人建议我在session.company id_comp成为PRIMARY_KEY ,所以我也无法通过这种方式工作。

I tried to create a table with a PRIMARY KEY in a new script with that simple table (as you can see, I tried to do it with the primary key constraint after a comma: 我试图在带有该简单表的新脚本中创建一个带有PRIMARY KEY表(如您所见,我尝试使用逗号后的primary key约束来完成该操作:

declare global temporary table session.t1 (
    id int primary key generated always as identity (start with 0 increment by 1) -- ,
    -- primary key (id)
    ) with replace on commit preserve rows not logged;

Also tried all this different options: 还尝试了所有这些不同的选项:

primary key id int generated always as identity (start with 0 increment by 1)
---
primary key (id) int
---
primary key id int
---
id int,
primary key (id)

and none of them works, all return `Error Code: -104'. 它们都不起作用,都返回`Error Code:-104'。

If your Db2 server runs on Linux/Unix/Windows, then a DGTT cannot participate in declarative RI. 如果您的Db2服务器在Linux / Unix / Windows上运行,则DGTT无法参与声明性RI。 See the documentation at this link specifically this text: 请参阅此链接上的文档,特别是以下文本:

Restrictions on the use of declared temporary tables: Declared temporary tables cannot:.. Be specified in referential constraints (SQLSTATE 42995). 使用声明的临时表的限制:声明的临时表不能:..在引用约束中指定(SQLSTATE 42995)。

You can use programmed RI however (that is to say, manually enforce the integrity by using set null or delete as appropriate for your data model and business). 但是,您可以使用编程的RI(也就是说,通过使用set null或根据您的数据模型和业务情况适当地删除来手动实施完整性)。 This means your code must populate both tables and then program the RI checks and resulting actions (to set null or delete rows) accordingly using plain SQL. 这意味着您的代码必须填充两个表,然后使用普通SQL对RI检查和相应的操作(设置为null或删除行)进行编程。

You should explain clearly why you don't want persistent tables so that the motivation is known - you then may get a better solution. 您应该清楚地解释为什么不希望使用持久表,以便了解动机,然后可能会得到更好的解决方案。

You can use persistent tables with the 'not logged ' characteristic at transaction level, but this is a bad idea because you then have to drop/recreate persistent tables after any Db2 error. 您可以在事务级别使用具有“未记录”特征的持久性表,但这不是一个好主意,因为在任何Db2错误之后,您都必须删除/重新创建持久性表。

If you do not need DGTT (session tables) and if you are happy to use persistent tables then the example syntax below is valid for Db2 current versions on LUW: 如果不需要DGTT(会话表),并且乐于使用持久性表,那么下面的示例语法对于LUW上的Db2当前版本有效:

create table company (
    id_comp int not null generated always as identity (start with 0 increment by 1) constraint pk1 primary key,
    name_comp varchar(60)
    ) ;


create table xemployee (
    id_emp int not null generated always as identity (start with 0 increment by 1),
    name varchar(40),
    id_comp int,
        constraint fk_id_comp
            foreign key (id_comp)
            references company (id_comp)
            on delete cascade
    ) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM