ORA-00904:无效的标识符和ORA-02256:引用列的数量必须与引用的列匹配(oracle)

时间:2014-03-09 22:36:37

标签: sql oracle

嗨,伙计们,对不起,我是sql和数据库真正的新手,我正在尝试创建这3个表,但它不让我感谢这两种类型的错误,我正在看他们但不太了解如何修复它们,我认为我无法理解和巩固从其他表创建外键和主键的语法,这里是erorrs,后面是create table语句:

Table created.

1 row created.

Constraint DepartmentFK FOREIGN KEY(DepartmentName)
                                    *
ERROR at line 12: 
ORA-00904: "DEPARTMENTNAME": invalid identifier 
Constraint EmployeePK   PRIMARY KEY(EmployeeNumber),
                                    *
ERROR at line 9: 
ORA-00904: "EMPLOYEENUMBER": invalid identifier 
)
*
ERROR at line 11: 
ORA-02256: number of referencing columns must match referenced columns 

如果有帮助的话,我正试图回答这些问题:

7.5 Write a CREATE TABLE statement for the EMPLOYEE table. Email is required and is 
an alternate key, and the default value of Department is Human Resources. Cascade
updates but not deletions from DEPARTMENT to EMPLOYEE.

7.6 Write a CREATE TABLE statement for PROJECT table. The default value for MaxHours is 100.
 Cascade updates but not deletions from DEPARTMENT to EMPLOYEE. 

7.7 Write a CREATE TABLE statement for the ASSIGNMENT table. Cascade only deletions from
PROJECT to ASSIGNMENT; do not cascade either deletions or updates from EMPLOYEE to 
ASSIGNMENT.

7.8 Modify your answer to Review Question 7.7 to include the constraint that StartDate
 be prior to EndDate.

并且我的代码是:

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)

);

INSERT INTO DEPARTMENT VALUES (
    'Administration', 'BC-100-10', 'BLDG01-300', '360-285-8100');

CREATE TABLE EMPLOYEE (
EmployeeNumber  int         NOT NULL,
FirstName       char(25)    NOT NULL,
LastName        char(25)    NOT NULL,
Department      char(35)    DEFAULT 'Human Resources' NOT NULL,
Phone           char(12)    NULL,
Email           char(30)    NOT NULL,


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

);

CREATE TABLE PROJECT (
ProjectID   int         NOT NULL,
Name        char(30)    NOT NULL,
Department1 char(15)    NOT NULL,
MaxHours    int         DEFAULT 100 NOT NULL,
StartDate   DATE        NULL,
EndDate     DATE        NULL,

Constraint EmployeePK   PRIMARY KEY(EmployeeNumber),
constraint DepartmentFK FOREIGN KEY(DepartmentName)
    references DEPARTMENT(DepartmentName)
-- ON UPDATE CASCADE 
-- ON DELETE no ACTION 
);

CREATE TABLE Assignment(
ProjectID       Number       NOT NULL,
EmployeeNumber  Number      NOT NULL,
HoursWorked     Number      NULL,

Constraint  datecheck check (Assignment.StartDate < Assignment.EndDate),
Constraint ProjectIDEmpNumPK    PRIMARY KEY(ProjectID, EmployeeNumber),
constraint ProjectIDEmpNumFK FOREIGN KEY(ProjectID, DepartmentName)
    references EMPLOYEE(DepartmentName)
--ON DELETE CASCADE 
);

我试着查找这些错误,我只是按照我的书中的例子我很困惑:(

我终于让桌子工作了。重要的是要了解如何进行正确的外键声明,每个声明必须在每个子表中都是唯一的。

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)

);

INSERT INTO DEPARTMENT VALUES (
    'Administration', 'BC-100-10', 'BLDG01-300', '360-285-8100');

CREATE TABLE EMPLOYEE (
EmployeeNumber  int         NOT NULL,
FirstName       char(25)    NOT NULL,
LastName        char(25)    NOT NULL,
Department      char(35)    DEFAULT 'Human Resources' NOT NULL,
Phone           char(12)    NULL,
Email           char(30)    NOT NULL,
DepartmentName_FK char(35) NOT NULL,

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

);

CREATE TABLE PROJECT (
ProjectID   int         NOT NULL,
Name        char(30)    NOT NULL,
Department1 char(15)    NOT NULL,
MaxHours    int         DEFAULT 100 NOT NULL,
StartDate   DATE        NULL,
EndDate     DATE        NULL,
DepartmentName_FK1 char(30)   NULL,


Constraint  datecheck check (StartDate < EndDate),
Constraint ProjectIDPK   PRIMARY KEY(ProjectID),
Constraint DepartmentFK1 FOREIGN KEY (DepartmentName_FK1)
    references DEPARTMENT(DepartmentName)
-- ON UPDATE CASCADE 
-- ON DELETE no ACTION 
);

CREATE TABLE Assignment(
ProjectID       Number      NOT NULL,
EmployeeNumber  Number      NOT NULL,
HoursWorked     Number      NULL,
DepartmentName_FK2 char(30)   NULL,

Constraint ProjectIDEmpNumPK PRIMARY KEY(ProjectID, EmployeeNumber),
constraint ProjectIDEmpNumFK FOREIGN KEY(DepartmentName_FK2)
    references Department(DepartmentName)
--ON DELETE CASCADE 
);

如果有人注意到这些表格声明与我发布的问题有关,你可以告诉我吗?另外,我只需要弄清楚如何在语法上编写级联更新和删除.....

2 个答案:

答案 0 :(得分:0)

您忘了在EMPLOYEE表上为该FK创建一列  您的表格应如下所示

CREATE TABLE EMPLOYEE (
EmployeeNumber  int         NOT NULL,
FirstName       char(25)    NOT NULL,
LastName        char(25)    NOT NULL,
Department      char(35)    DEFAULT 'Human Resources' NOT NULL,
Phone           char(12)    NULL,
Email           char(30)    NOT NULL,
DepartmentName_FK char(35) NOT NULL,
Constraint EmployeePK   PRIMARY KEY(EmployeeNumber),
Constraint EmployeeAK1 UNIQUE(Email),
Constraint DepartmentFK FOREIGN KEY (DepartmentName_FK)
    references DEPARTMENT(DepartmentName));

立即尝试!


 看一下我写的关于constraints的这篇文章,它会帮助你更好地理解!

答案 1 :(得分:0)

实际上你的行中存在太多问题。例如,在员工中,您的外键位于列DepartmentName上,而在员工表中,您没有该列Department

而且:

  Constraint  datecheck check (Assignment.StartDate < Assignment.EndDate),

在分配表中,我认为你打算把它放在Project表中,因为在赋值中没有开始和结束日期

我创建了一个有效的fiddle供您检查。