ORA-00905:缺少关键字(约束外键)

时间:2014-03-09 04:12:31

标签: sql oracle

大家好,我对SQL完全不熟悉,我正试着在我的书中回答这个问题:

7.5为EMPLOYEE表写一个CREATE TABLE语句。电子邮件是必需的,是备用密钥,Department的默认值是人力资源。级联更新,但不是从DEPARTMENT到EMPLOYEE的删除。

我在Oracle iSQL * Plus中运行查询,我成功创建了department表,但是当我在满足这些要求时尝试创建employee表时,我在第12行得到了缺少的关键字错误

constraint DepartmentFK FOREIGN KEY(DepartmentName),

这是整个查询(我在尝试整个事情之前删除了部门表,但仍然会出现相同的错误)

CREATE TABLE DEPARTMENT (
DepartmentName  char(35) NOT NULL,
BudgetCode      char(30) NOT NULL,
OfficeNumber    char(15) NOT NULL,
Phone           char(12) NOT NULL,

Constraint DepartmentPK PRIMARY KEY(DepartmentName)

);

CREATE TABLE EMPLOYEE (
ProjectID   int      NOT NULL,
Name        char(30) NOT NULL,
Department  char(15) NOT NULL,
MaxHours    int      NOT NULL,
StartDate   char(8)  NULL,
EndDate     char(8)  NULL,
Email       char(30) DEFAULT 'Human Resources' NOT NULL,

Constraint EmployeePK   PRIMARY KEY(ProjectID),
Constraint EmployeeAK1 UNIQUE(Email),
constraint DepartmentFK FOREIGN KEY(DepartmentName),
 references DEPARTMENT(DepartmentName)
 ON UPDATE CASCADE 
 ON DELETE no ACTION 

);

我尝试按照本书中最相似的例子查找外键约束和引用,但我无法理解为什么我会收到此错误...

编辑:

我拿出了逗号,但我仍然遇到了这两个错误:

CREATE TABLE DEPARTMENT (
             *
ERROR at line 1: 
ORA-00955: name is already used by an existing object 
 ON UPDATE CASCADE
    *
ERROR at line 14: 
ORA-00905: missing keyword 

1 个答案:

答案 0 :(得分:1)

您必须删除

之后使用的comma
constraint DepartmentFK FOREIGN KEY(DepartmentName),

这是一个单位

constraint DepartmentFK FOREIGN KEY(DepartmentName) references DEPARTMENT(DepartmentName)

编辑: 因为,您已编辑了您的问题 -

Oracle数据库中没有

ON UDDATE CASCADE选项,这就是您收到错误的原因。

您的Department表格收到错误,因为该表格已经存在,可能是上次运行!