2

当我在 Bluemix 上的 DashDB (DB2) 中创建一个表时,如下所示:

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 不允许为此表类型添加外键约束。

4

2 回答 2

5

当您查看dashDB(不是 DB2)的文档时,您会注意到可以创建外键。但是,默认情况下创建的表是按列组织的。仅支持非强制引用约束。在您的示例中,您需要在语句中添加 NOT ENFORCED:

ALTER TABLE EMPLOYEE 
 ADD CONSTRAINT EMPLOYEE_DEPARTMENT_FK FOREIGN KEY (depname ) 
 REFERENCES DEPARTMENT ( depname ) NOT ENFORCED;
于 2015-07-27T07:00:13.050 回答
2

默认情况下,在 CREATE 上,Bluemix 上的 DashDB 表“按列组织”... https://www-01.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.admin.dbobj。 doc/doc/c0060592.html

这也将不允许为此表类型添加外键约束。

要将 FK 添加ORGANIZE BY ROW到您的CREATE TABLE语句中:

CREATE TABLE DEPARTMENT (
    depname CHAR (10) UNIQUE NOT NULL ,
    phone   INTEGER
  ) ORGANIZE BY ROW;

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)
  ) ORGANIZE BY ROW;

ALTER TABLE EMPLOYEE 
 ADD CONSTRAINT EMPLOYEE_PK PRIMARY KEY ( "EmpNr" ) ;

ALTER TABLE EMPLOYEE 
 ADD CONSTRAINT EMPLOYEE_DEPARTMENT_FK FOREIGN KEY (depname )
 REFERENCES DEPARTMENT ( depname ) ;
于 2015-07-25T16:28:59.920 回答