foreign key mismatch with foreign key pair

  Kiến thức lập trình

I’m trying to create the following sql tables’ structure, but I can’t figure out the problem. Here’s an example:

DROP TABLE IF EXISTS "c";
DROP TABLE IF EXISTS "b";
DROP TABLE IF EXISTS "a";
CREATE TABLE "a" (
    "Field1"    INTEGER PRIMARY KEY
);
CREATE TABLE "b" (
    "Field1"    INTEGER PRIMARY KEY,
    "Field2"    INTEGER,
    FOREIGN KEY("Field2") REFERENCES "a"("Field1")
);
CREATE TABLE "c" (
    "Field1"    INTEGER PRIMARY KEY,
    "Field2"    INTEGER,
    "Field3"    INTEGER,
    FOREIGN KEY("Field2") REFERENCES "b"("Field1"),
    FOREIGN KEY("Field3") REFERENCES "b"("Field2")
);
insert into a values(1);
insert into b values(2,1);
insert into c values(3,2,1);

The last insert gives foreign key mismatch - "c" referencing "b"
What’s the problem?

LEAVE A COMMENT