Sudarshana Atapattu
Sudarshana Atapattu

Reputation: 9

When i inserted data, it always says that foreign key mismatch.I can't find the reason for it?

CREATE TABLE Student 
(
    Registration_No INTEGER  NOT NULL ,
    NIC  varchar(25),
    Student_Name varchar(255),
    Address varchar(255),
    Home_Telephone_No varchar(25),
    Mobile_Telephone_No varchar(25),
    Email varchar(255),
    Education varchar(255),

    PRIMARY KEY (Registration_No, NIC)
);

CREATE TABLE Payment 
(
    Registration_No INTEGER NOT NULL,
    Registration_fee decimal(15,2),
    Annual_subscription decimal(15,2),

    FOREIGN KEY(Registration_No) 
        REFERENCES Student(Registration_No)
);

Error:

Foreign key mismatch - "Payment" referencing "Student" 1:]1INSERT INTO Payment(Registration_No,Registration_fee,Annual_subscription )VALUES(10,100.50,500);

Foreign key mismatch - "Payment" referencing "Student"

Upvotes: 0

Views: 98

Answers (1)

Harsh Gundecha
Harsh Gundecha

Reputation: 1197

  • your primary key in table Student is not Registration_No alone but combination of Registration_No and NIC
  • now when you are referring it from other table you have referred only PART of the combination which is not unique causing the error, the solution is to match the FK to PK or at least a unique column of parent table for it to work as expected, read more here -> https://stackoverflow.com/a/18435114/8057127

Upvotes: 3

Related Questions