本文介绍了无法在 Bluemix 上的 DashDB/DB2 中的表上添加外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我像这样在 Bluemix 上的 DashDB (DB2) 中创建表时:
When I create a table in DashDB (DB2) on Bluemix like this:
CREATE TABLE DEPARTMENT (
depname CHAR (10) UNIQUE NOT NULL ,
phone INTEGER
) ;
ALTER TABLE DEPARTMENT ADD CONSTRAINT DEPARTMENT_PK PRIMARY KEY ( depname ) ;
CREATE TABLE EMPLOYEE (
"EmpNr" NUMERIC (3) UNIQUE NOT NULL ,
empname CHAR (20) ,
depname CHAR (10) ,
EMPLOYEE2_title CHAR (20)
);
ALTER TABLE EMPLOYEE ADD CONSTRAINT EMPLOYEE_PK PRIMARY KEY ( "EmpNr" ) ;
ALTER TABLE EMPLOYEE ADD CONSTRAINT EMPLOYEE_DEPARTMENT_FK FOREIGN KEY (depname ) REFERENCES DEPARTMENT ( depname ) ;
Bluemix 不允许为此表类型添加外键约束.
Bluemix disallows adding a Foreign Key constraint for this table type.
推荐答案
当你查看dashDB 文档(不是 DB2)您会注意到可以创建外键.但是,默认情况下创建的表是按列组织的.仅支持非强制引用约束.在您的示例中,您需要在语句中添加 NOT ENFORCED:
When you look at the documentation for dashDB (not DB2) you will notice that foreign keys can be created. However, a table by default is created column-organized. Only non-enforced referential constraints are supported. In your example you would need to add NOT ENFORCED to your statement:
ALTER TABLE EMPLOYEE
ADD CONSTRAINT EMPLOYEE_DEPARTMENT_FK FOREIGN KEY (depname )
REFERENCES DEPARTMENT ( depname ) NOT ENFORCED;
这篇关于无法在 Bluemix 上的 DashDB/DB2 中的表上添加外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!