Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8477315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:25:55+00:00 2026-06-10T18:25:55+00:00

I have 3 tables: CREATE TABLE Names ( Name TEXT(20) NOT NULL, Gender TEXT(20)

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:25:57+00:00Added an answer on June 10, 2026 at 6:25 pm

    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:

    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"),
        CHECK (Gender IN ('M', 'F')),
        CHECK ("ID" NOT IN ("FatherID", "MotherID")));
    

    this could be the INSERT trigger (I’d let you write the UPDATE one):

    CREATE TRIGGER checkParentIdsOnInsert BEFORE INSERT ON People 
        WHEN new.FatherID IS NOT NULL OR new.MotherID IS NOT NULL
    BEGIN
        SELECT CASE     
        WHEN ((SELECT Gender FROM People AS t1 WHERE t1.ID=new.FatherID) = 'F' 
                AND (SELECT Gender FROM People AS t2 WHERE t2.ID=new.MotherID) = 'M')
           THEN RAISE(ABORT, 'Father must be male and mother female') 
        WHEN ((SELECT Gender FROM People AS t3 WHERE t3.ID=new.FatherID) = 'F')
           THEN RAISE(ABORT, 'Father must be male') 
        WHEN ((SELECT Gender FROM People AS t4 WHERE t4.ID=new.MotherID) = 'M')
           THEN RAISE(ABORT, 'Mother must be female') 
        END; 
    END;
    

    Some simple tests:

    sqlite> pragma foreign_keys=on;
    sqlite> INSERT INTO People (Name, SName, Gender, FatherID, MotherID) VALUES
       ...>     ("Jo", "Blo", "M", NULL, NULL);
    sqlite> INSERT INTO People (Name, SName, Gender, FatherID, MotherID) VALUES
       ...>     ("Za", "Bla", "F", NULL, NULL);
    sqlite> INSERT INTO People (Name, SName, Gender, FatherID, MotherID) VALUES
       ...>     ("Bad", "Kid", "M", 2, 1);
    Error: Father must be male and mother female
    sqlite> INSERT INTO People (Name, SName, Gender, FatherID, MotherID) VALUES
       ...>     ("Bad", "Kid", "M", 2, NULL);
    Error: Father must be male
    sqlite> INSERT INTO People (Name, SName, Gender, FatherID, MotherID) VALUES
       ...>     ("Bad", "Kid", "M", NULL, 1);
    Error: Mother must be female
    sqlite> INSERT INTO People (Name, SName, Gender, FatherID, MotherID) VALUES
       ...>     ("Good", "Kid", "M", 1, 2);
    sqlite> .headers on
    sqlite> .mode column
    sqlite> SELECT * FROM People;
    ID          Name        Sname       Gender      FatherID    MotherID  
    ----------  ----------  ----------  ----------  ----------  ----------
    1           Jo          Blo         M                                 
    2           Za          Bla         F                                 
    3           Good        Kid         M           1           2         
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have 2 tables CREATE TABLE if not exists tag_name( + tagid INTEGER PRIMARY
I have Created a table like CREATE TABLE [dbo].[tab1]( [Id] [int] NOT NULL, [Name]
I have two tables like this: CREATE TABLE table1_lang ( id serial NOT NULL,
I have the following table drop table names; create table names( name varchar(200), surname
I have a code: CREATE TABLE IF NOT EXISTS Person ( name varchar(24) ...
When I try to run this statement: CREATE TABLE 'score_table' ( 'name' text NOT
In MySQL I have these 3 tables : CREATE TABLE IF NOT EXISTS Seasons
I have the following table: $sql = CREATE TABLE received_queries ( sender_screen_name varchar(50), text
i have a table e.g: CREATE TABLE places (id UNIQUE, name, latitude, longitude) i
I have tried with ado.net create table columnName with unique name. as uniquename I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.