我正在尝试在Oracle 11g中创建表,但是出现ORA-00904错误。创建表的语法为:
make check
我得到的错误是:
CREATE TABLE Worker( nif VARCHAR2(9 CHAR) CONSTRAINT PK_Nif PRIMARY KEY, nameworker VARCHAR2(50 CHAR) CONSTRAINT NN_NameWorker NOT NULL, specialty VARCHAR2(50 CHAR) CONSTRAINT NN_Specialty NOT NULL, CONSTRAINT CH_Speciality CHECK (speciality ='ENGINEERING' OR speciality= 'SUPPORT' OR speciality='DEVELOPMENT'), yearsold INTEGER, CONSTRAINT CH_Years CHECK (yearsold >= 1), CONSTRAINT NN_Years NOT NULL, nifCompany VARCHAR2(9 CHAR) CONSTRAINT NN_NifCompany NOT NULL, CONSTRAINT FK_NifCompany FOREIGN KEY (nifCompany) REFERENCES Company(nif) );
我不知道错误在哪里。
答案 0 :(得分:1)
您有几个问题;错误的直接原因是此行:
CONSTRAINT NN_Years NOT NULL,
基于先前的命名,您可能打算在列定义中包括内联:
yearsold INTEGER CONSTRAINT NN_Years NOT NULL,
CONSTRAINT CH_Years CHECK (yearsold >= 1),
第二个问题是,列定义中的名称specialty
与检查约束中的speciality
不一致。因此您需要修复列名和非空约束名称:
speciality VARCHAR2(50 CHAR) CONSTRAINT NN_Specialty NOT NULL,
修复这些问题,并假设您确实有一个Company
表,该表具有与此指定的FK匹配的PK,将为您提供:
CREATE TABLE Worker(
nif VARCHAR2(9 CHAR) CONSTRAINT PK_Nif PRIMARY KEY,
nameworker VARCHAR2(50 CHAR) CONSTRAINT NN_NameWorker NOT NULL,
speciality VARCHAR2(50 CHAR) CONSTRAINT NN_Specialty NOT NULL,
CONSTRAINT CH_Speciality CHECK (speciality = 'ENGINEERING'
OR speciality = 'SUPPORT' OR speciality = 'DEVELOPMENT'),
yearsold INTEGER CONSTRAINT NN_Years NOT NULL,
CONSTRAINT CH_Years CHECK (yearsold >= 1),
nifCompany VARCHAR2(9 CHAR) CONSTRAINT NN_NifCompany NOT NULL,
CONSTRAINT FK_NifCompany FOREIGN KEY (nifCompany) REFERENCES Company(nif)
);
Table WORKER created.
desc worker
Name Null? Type
---------- -------- -----------------
NIF NOT NULL VARCHAR2(9 CHAR)
NAMEWORKER NOT NULL VARCHAR2(50 CHAR)
SPECIALITY NOT NULL VARCHAR2(50 CHAR)
YEARSOLD NOT NULL NUMBER(38)
NIFCOMPANY NOT NULL VARCHAR2(9 CHAR)
答案 1 :(得分:0)
我认为问题是您在列定义期间指定约束名称。你不能这样做。如果要给约束起名字,则应指定不同的约束。 试试:
CREATE TABLE Worker(
nif VARCHAR2(9 CHAR) ,
nameworker VARCHAR2(50 CHAR) ,
specialty VARCHAR2(50 CHAR),
CONSTRAINT CH_Speciality CHECK (speciality ='ENGINEERING' OR speciality= 'SUPPORT' OR speciality='DEVELOPMENT'),
yearsold INTEGER,
CONSTRAINT CH_Years CHECK (yearsold >= 1),
CONSTRAINT NN_Years check(yearold is NOT NULL),
nifCompany VARCHAR2(9 CHAR) ,
CONSTRAINT FK_NifCompany FOREIGN KEY (nifCompany) REFERENCES Company(nif),
CONSTRAINT PK_Nif PRIMARY KEY(nif),
CONSTRAINT NN_NameWorker check(nameworker is NOT NULL),
CONSTRAINT NN_Specialty check(specialty is NOT NULL),
CONSTRAINT NN_NifCompany check(nifCompany is NOT NULL)
);
让我知道您是否还有其他问题