I have 3 tables:
CREATE TABLE "Names" (
"Name" TEXT(20) NOT NULL,
"Gender" TEXT(20) NOT NULL,
PRIMARY KEY ("Name", "Gender")
);
CREATE TABLE "Snames" (
"Sname" TEXT(20) NOT NULL,
PRIMARY KEY ("Sname")
);
CREATE TABLE "People" (
"ID" INTEGER NOT NULL,
"Name" TEXT(20) NOT NULL,
"Sname" TEXT(20) NOT NULL,
"Gender" TEXT(1) NOT NULL,
"FatherID" INTEGER,
"MotherID" INTEGER,
PRIMARY KEY ("ID") ,
CONSTRAINT "Father" FOREIGN KEY ("FatherID") REFERENCES "People" ("ID"),
CONSTRAINT "Mother" FOREIGN KEY ("MotherID") REFERENCES "People" ("ID"),
CONSTRAINT "Sname" FOREIGN KEY ("Sname") REFERENCES "Snames" ("Sname"),
CONSTRAINT "Name" FOREIGN KEY ("Name", "Gender") REFERENCES "Names" ("Name", "Gender")
);
My problem is with the foreign key constraints on “FatherID” and “MotherID”, which reference their own table. Is it possible to only allow foreign keys where “M” is in the Gender column for “FatherID”, and “F” for “MotherID”? And is it possible to disallow the Mother/Father to reference the same row?
BASICALLY: Father’s must be male. Mother’s must be female. You can’t be your own mother/father.
I believe SQLite doesn’t support constraints that contain expressions with values obtained dynamically from other rows, with the notable exception of foreign keys.
You will have to create triggers to check the gender of the father and mother.
Using this table definition:
this could be the INSERT trigger (I’d let you write the UPDATE one):
Some simple tests: