oracle中列列表没有匹配的唯一键或主键

时间:2016-09-12 18:41:08

标签: oracle oracle10g

CREATE TABLE student_detailS01
(USN VARCHAR(20)  NOT NULL,
 STUDENT_NAME VARCHAR2(20) NOT NULL,
 BRANCH VARCHAR2(20) NOT NULL,
 CONTACT_NO NUMBER(10) NOT NULL,
 FATHERS_NAME VARCHAR2(20) NOT NULL,
 HOME_ADDRESS VARCHAR(50) NOT NULL,
 DOB DATE NOT NULL,
LOCAL_ADDRESS VARCHAR2(50),
EMAIL_ID VARCHAR2(20),
DOJ DATE,
HID NUMBER NOT NULL, 

 PRIMARY KEY (USN,HID)
 );

CREATE TABLE MESS(
S_NO NUMBER NOT NULL,
HID NUMBER REFERENCES STUDENT_DETAILS01(HID),
NO_OF_BREAKFAST INT,
NO_OF_MEALS INT,
AMT_OF_BREAKFAST INT,
AMT_OF_MEALS INT,
TOTAL INT NOT NULL,
PRIMARY KEY(S_NO)
);

student_details01表已执行,但是mess table出现以下错误:

  

此列列表没有匹配的唯一键或主键。

1 个答案:

答案 0 :(得分:0)

ORA-2270 : no matching unique or primary key for this column-list错误非常简单:当我们在外键中引用的列与父表上的主键或唯一约束不匹配时,会发生此错误。常见的原因是

  • 父母完全没有约束
  • 父表的约束是复合键,我们没有 引用了外键语句中的所有列。

要解决您的问题,请尝试以下操作:

CREATE TABLE MESS
(
   S_NO               NUMBER,
   S_HID              NUMBER,
   S_UNO              VARCHAR (20), --Added Column
   NO_OF_BREAKFAST    INT,
   NO_OF_MEALS        INT,
   AMT_OF_BREAKFAST   INT,
   AMT_OF_MEALS       INT,
   TOTAL              INT NOT NULL,
   CONSTRAINT MESS_pk PRIMARY KEY (S_NO),
   CONSTRAINT fk_id_dtls FOREIGN KEY
      (S_HID, S_UNO)
       REFERENCES student_detailS01 (HID, USN)
);