错误ORA-02270:此列列表没有匹配的唯一键或主键

时间:2014-10-17 17:03:59

标签: sql database oracle

我正在练习实验手册练习,我必须在其中创建6个表格。创造5是 成功的。

但有一行是错误

constraint GRADE_Designation_FK 
   FOREIGN KEY(Designation) References EMPLOYEE(Designation),

第7行的错误:

  

ORA-02270:此列列表没有匹配的唯一键或主键

2个链接表的查询是

create table EMPLOYEE
(
    Empno number(4) constraint EMPLOYEE_Empno_PK PRIMARY KEY,
    Name varchar2(10) not null,
    Designation varchar2(50),
    Qualification varchar2(10),
    Joindate date
);

create table GRADE
(
    Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
    Grade number(2),
    TotalPosts number(4),
    PostsAvailable number(4),

    constraint GRADE_Grade_CK  check(Grade between 1 and 20),
    constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts),
    constraint GRADE_Designation_FK FOREIGN KEY(Designation) References EMPLOYEE(Designation)
);

尝试

create table GRADE
(
    Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
    Grade number(2),
    TotalPosts number(4),
    PostsAvailable number(4),

    constraint GRADE_Grade_CK  check(Grade between 1 and 20),
    constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts)
);

create table EMPLOYEE
(
    Empno number(4) constraint EMPLOYEE_Empno_PK PRIMARY KEY,
    Name varchar2(10) not null,
    Designation varchar2(50) NOT NULL UNIQUE,
    Qualification varchar2(10),
    Joindate date default sysdate

    constraint EMPLOYEE_Designation_FK FOREIGN KEY(Designation) References GRADE(Designation),
);

现在出现新错误

  

约束EMPLOYEE_Designation_FK FOREIGN KEY(名称)参考文献GRADE(指定)      *

     

第8行的错误:

     

ORA-02253:此处不允许约束规范

3 个答案:

答案 0 :(得分:1)

你在错误的桌子上得到了约束。您应该在EMPLOYEE.DESIGNATION上创建一个外键,引用回GRADE.DESIGNATION。

所以你的表应该是这样的:

create table GRADE
(
Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
Grade number(2),
TotalPosts number(4),
PostsAvailable number(4),
constraint GRADE_Grade_CK  check(Grade between 1 and 20),
constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts),
);

create table EMPLOYEE
(
Empno number(4) constraint EMPLOYEE_Empno_PK PRIMARY KEY,
Name varchar2(10) not null,
Designation varchar2(50)
  constraint EMPLOYEE_FK1
    REFERENCES GRADE(DESIGNATION),
Qualification varchar2(10),
Joindate date
);

分享并享受。

答案 1 :(得分:1)

  

ORA-02270:此列列表没有匹配的唯一键或主键

这个错误非常自我解释,并告诉你错误。在您的情况下,您尝试在非主键列上创建外键,因此错误

constraint GRADE_Designation_FK FOREIGN KEY(Designation) 
               References EMPLOYEE(Designation)
                                  <--Here
Designation表中的

EMPLOYEE不是主键,您无法在非主键列上创建FK。你的表创建应该看起来像

create table GRADE
(
Designation varchar2(50) constraint GRADE_Designation_PK PRIMARY KEY,
employee_Empno number(4),
Grade number(2),
TotalPosts number(4),
PostsAvailable number(4),
constraint GRADE_Grade_CK  check(Grade between 1 and 20),
constraint GRADE_PostsAvailable_CK check(PostsAvailable <= TotalPosts),
constraint GRADE_Designation_FK FOREIGN KEY(employee_Empno) 
References EMPLOYEE(Empno));

答案 2 :(得分:0)

执行的顺序导致了我们这个问题,确保在添加这些约束之前创建了使用NOT NULL启用的表以避免此错误。