在Oracle中创建表时获取无效的数据类型

时间:2018-11-08 12:49:19

标签: sql oracle

因此,我在名为FACILITY的表中添加了一列。然后,我尝试创建一个名为CLUBMANAGER的表,并收到以下消息:

ERROR at line 2:
ORA-00902: invalid datatype

我的SQL语句是:

CREATE TABLE FACILITY(
facilityName    VARCHAR2(50)    NOT NULL,
rate            NUMBER(7,2),
status          VARCHAR2(25),
CONSTRAINT FACILITY_PKEY PRIMARY KEY(facilityName) );

ALTER TABLE FACILITY ADD MANAGER_EMAIL VARCHAR2(100);
ALTER TABLE facility ADD CONSTRAINT pk_fac UNIQUE (manager_email);
CREATE TABLE CLUBMANAGER (
    email       VARCHAR2(100)   NOT NULL,
    name        VARCHAR2(100)   NOT NULL,
    phoneNum    NUMBER(10)      NOT NULL,
    CONSTRAINT  CLUBMANAGER_PKEY PRIMARY KEY (email),
    CONSTRAINT  CLUBMEMBER_FKEY FOREIGN KEY (email) REFERENCES FACILITY (MANAGER_EMAIL));

第2行是:

email       VARCHAR2(100)   NOT NULL

我的SQL输出是:

    SQL> ALTER TABLE facility ADD CONSTRAINT pk_fac UNIQUE (manager_email);

Table altered.

SQL> CREATE TABLE CLUBMANAGER (
  2  emailVARCHAR(100)NOT NULL,
  3  nameVARCHAR(100)NOT NULL,
  4  phoneNumNUMBER(10)NOT NULL,
  5  CONSTRAINTCLUBMANAGER_PKEY PRIMARY KEY (email),
  6  CONSTRAINTCLUBMEMBER_FKEY FOREIGN KEY (email) REFERENCES FACILITY (MANAGER_EMAIL));
emailVARCHAR(100)NOT NULL,
            *
ERROR at line 2:
ORA-00902: invalid datatype

我正在使用Oracle Database XE 11.2。

2 个答案:

答案 0 :(得分:1)

从您发布的内容来看,没有无效的数据类型(至少,我看不到任何数据类型),但是缺少主键或唯一键-您无法创建不具有此功能的外键约束t指向这些键之一。

看看:首先,一个 dummy FACILITY表,随您的操作而改变:

SQL> CREATE TABLE facility (id NUMBER);

Table created.

SQL> ALTER TABLE facility ADD manager_email VARCHAR2 (100);

Table altered.

创建表CLUBMANAGER

SQL> CREATE TABLE clubmanager
  2  (
  3     email      VARCHAR2 (100) NOT NULL,
  4     name       VARCHAR2 (100) NOT NULL,
  5     phonenum   NUMBER (10) NOT NULL,
  6     CONSTRAINT clubmanager_pkey PRIMARY KEY (email),
  7     CONSTRAINT clubmember_fkey FOREIGN KEY
  8        (email)
  9         REFERENCES facility (manager_email)
 10  );
       REFERENCES facility (manager_email)
                            *
ERROR at line 9:
ORA-02270: no matching unique or primary key for this column-list

不,不行。添加唯一密钥,然后再次创建CLUBMANAGER

SQL> ALTER TABLE facility ADD CONSTRAINT pk_fac UNIQUE (manager_email);

Table altered.

SQL> CREATE TABLE clubmanager
  2  (
  3     email      VARCHAR2 (100) NOT NULL,
  4     name       VARCHAR2 (100) NOT NULL,
  5     phonenum   NUMBER (10) NOT NULL,
  6     CONSTRAINT clubmanager_pkey PRIMARY KEY (email),
  7     CONSTRAINT clubmember_fkey FOREIGN KEY
  8        (email)
  9         REFERENCES facility (manager_email)
 10  );

Table created.

SQL>

如您所见,一切正常。如果仍然无法解决您的问题,请发布自己的SQL * Plus会话,以便我们了解您的工作以及Oracle的响应方式。

答案 1 :(得分:0)

获得ORA-00902很有趣。

  

编辑:您需要在列名和数据类型之间加空格,并且   约束,例如email VARCHAR(100) NOT NULL而不是emailVARCHAR(100)NOT NULL

MANAGER_EMAIL必须是uniqueprimary key才能被foreign key引用。

因此,请使用ALTER TABLE FACILITY ADD MANAGER_EMAIL VARCHAR2(100) primary key [unique] ;