在Oracle中添加约束时,数据类型无效

时间:2012-04-01 10:21:36

标签: sql oracle oracle10g oracle11g

我想在Oracle中创建此表。这只是表SQL脚本。

     -- CREATE TABLES SECTION -------------------------------------------------

-- TABLE DATACENTER

CREATE TABLE DATACENTER(
  DATACENTERID INTEGER NOT NULL,
  NAME VARCHAR2(80 ) NOT NULL,
  LOCATION VARCHAR2(200 ),
  DCALLOWEDWEIGHTKG NUMBER(9,0),
  DCMAXIMUMWEIGHTKG NUMBER(9,0),
  DCALLOWEDPOWERWATT NUMBER(9,0),
  DCMAXPOWERWATT NUMBER(9,0),
  DCALLOWCOOLINGPOWERBTU NUMBER(9,0),
  DCMAXCOOLINGPOWERBTU NUMBER(9,0),
  DESCRIPTION CLOB
)
/
-- ADD KEYS FOR TABLE DATACENTER

ALTER TABLE DATACENTER ADD CONSTRAINT DATACENTERID PRIMARY KEY (DATACENTERID)
/

-- TABLE COMPONENT

CREATE TABLE COMPONENT(
  COMPONENTID INTEGER NOT NULL,
  DATACENTERID INTEGER,
  FKCOMPONENTID INTEGER,
  COMPONENTSTATSID INTEGER NOT NULL
)
/

-- ADD KEYS FOR TABLE COMPONENT

ALTER TABLE COMPONENT ADD CONSTRAINT COMPONENTID PRIMARY KEY (COMPONENTID)
/
-- CREATE RELATIONSHIPS SECTION ------------------------------------------------- 

ALTER TABLE COMPONENT ADD CONSTRAINT IS PART OF A FOREIGN KEY (DATACENTERID) REFERENCES DATACENTER (DATACENTERID)
/

ALTER TABLE COMPONENT ADD CONSTRAINT IS A SUBPART OF FOREIGN KEY (FKCOMPONENTID) REFERENCES COMPONENT (COMPONENTID)
/

错误堆栈:

table DATACENTER created.
table DATACENTER altered.
table COMPONENT created.
table COMPONENT altered.

Error starting at line 39 in command:
ALTER TABLE COMPONENT ADD CONSTRAINT IS PART OF A FOREIGN KEY (DATACENTERID) REFERENCES DATACENTER (DATACENTERID)
Error report:
SQL Error: ORA-00902: invalid datatype
00902. 00000 -  "invalid datatype"
*Cause:    
*Action:

Error starting at line 42 in command:
ALTER TABLE COMPONENT ADD CONSTRAINT IS A SUBPART OF FOREIGN KEY (FKCOMPONENTID) REFERENCES COMPONENT (COMPONENTID)
Error report:
SQL Error: ORA-00902: invalid datatype
00902. 00000 -  "invalid datatype"
*Cause:    
*Action:

编辑我编辑了代码,因为我看到一些SQL语句丢失了。 如果你愿意,我可以提供架构吗?

祝福

1 个答案:

答案 0 :(得分:5)

您的约束名称IS PART OF A是非法标识符(因为空格)。

您需要移除空格,例如IS_PART_OF_A

ALTER TABLE COMPONENT 
   ADD CONSTRAINT IS_PART_OF_A 
   FOREIGN KEY (DATACENTERID) 
   REFERENCES DATACENTER (DATACENTERID)

修改
我还意识到你的脚本没有正确终止各个语句。在第一个CREATE TABLE之后缺少;(或/)。并且第一个ALTER TABLE也未正确终止。我不知道这是不是副本&虽然粘贴错误。

<强> EDIT2

以下是具有正确名称,语句终止和正确顺序的完整脚本:

CREATE TABLE COMPONENT(
    COMPONENTID INTEGER NOT NULL,
    DATACENTERID INTEGER,
    FKCOMPONENTID INTEGER,
    COMPONENTSTATSID INTEGER NOT NULL
)
/

CREATE TABLE DATACENTER(
  DATACENTERID INTEGER NOT NULL,
  NAME VARCHAR2(80 ) NOT NULL,
  LOCATION VARCHAR2(200 ),
  DCALLOWEDWEIGHTKG NUMBER(9,0),
  DCMAXIMUMWEIGHTKG NUMBER(9,0),
  DCALLOWEDPOWERWATT NUMBER(9,0),
  DCMAXPOWERWATT NUMBER(9,0),
  DCALLOWCOOLINGPOWERBTU NUMBER(9,0),
  DCMAXCOOLINGPOWERBTU NUMBER(9,0),
  DESCRIPTION CLOB
)
/

ALTER TABLE DATACENTER ADD CONSTRAINT DATACENTERID PRIMARY KEY (DATACENTERID)
/

ALTER TABLE COMPONENT ADD CONSTRAINT COMPONENTID PRIMARY KEY (COMPONENTID)
/

ALTER TABLE COMPONENT ADD CONSTRAINT IS_PART_OF_A FOREIGN KEY (DATACENTERID) REFERENCES DATACENTER (DATACENTERID)
/