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 9022421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:30:23+00:00 2026-06-16T05:30:23+00:00

I am not an expert in sql / sqlite.. suppose we have two tables:

  • 0

I am not an expert in sql / sqlite..
suppose we have two tables:

CREATE TABLE child (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
);

CREATE TABLE MyTableB(
  dog TEXT, 
  FOREIGN KEY(dogList) REFERENCES child(id)
);

how will the INSERT? is correct my createTable operations?
I would like to have:
a child can have more than one dog
a dog can have more children

EDIT

What if I wanted all the children and for each child a list of dogs associated with that child?

  • 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-16T05:30:25+00:00Added an answer on June 16, 2026 at 5:30 am

    Many-To-Many

    In order to support a child having zero or more dogs and a dog belonging to zero or more children, your database table structure needs to support a Many-To-Many relationship. This requires three tables:

    CREATE TABLE child (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT
    );
    
    
    CREATE TABLE dog (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        dog TEXT
    );
    
    CREATE TABLE child_dog {
        child_id INTEGER,
        dog_id INTEGER,
        FOREIGN KEY(child_id) REFERENCES child(id),
        FOREIGN KEY(dog_id) REFERENCES dog(id)
    };
    

    How to Insert

    An insert into each of the three tables must be separate SQL statements, but can take place in the context of same transaction. Inserts into the child_dog table (known as the mapping table) must happen after inserts into the child and dog tables. This is for two related reasons:

    1. You need to know the identifiers of both the child and the dog in order to
      to insert into this table.
    2. Because of the foreign key constraint, an insert into the child_dog table would fail if the child and/or dog referenced do not exist in the database or transaction.

    Here are some example SQL statements for insert:

    INSERT INTO child VALUES(NULL, 'bobby');
    SELECT last_insert_rowid(); -- gives the id of bobby, assume 2 for this example
    INSERT INTO dog VALUES(NULL, 'spot');
    SELECT last_insert_rowid(); -- gives the id of spot, assume 4 for this example
    INSERT INTO child_dog VALUES(2, 4);
    

    Inserting In Python

    Although your question did not mention python, there is a python tag on this question so I’ll assume you want to know how to do this in python. The sqlite3 module in python provides a nice little shortcut which saves you from having to run the ‘last_insert_rowid()’ function explicitly.

    # Import the sqlite3 module
    import sqlite3
    # Create a connection and cursor to your database
    conn = sqlite3.connect('example.db')
    c = conn.cursor()
    # Insert bobby
    c.execute("""INSERT INTO child VALUES(NULL, 'bobby')""")
    # The python module puts the last row id inserted into a variable on the cursor
    bobby_id = c.lastrowid
    # Insert spot
    c.execute("""INSERT INTO dog VALUES(NULL, 'spot')""")
    spot_id = c.lastrowid
    # Insert the mapping
    c.execute("""INSERT INTO child_dog VALUES(?, ?)""", (bobby_id, spot_id));
    # Commit
    conn.commit()
    conn.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not an SQL expert, here is my SQLite query on table Query (key:
I'm not an expert with either System.Data nor SQL Server, but have a need
I'm not very expert with Perl, so I preferred to ask you. I have
I am not an expert in Git, but I have seen this done before
I have a fairly large SQL Server database; I'd like to pull 4 tables
I am not an expert on SQL, so I am asking here some help
I am not an expert on SQl Server. Is this a valid pattern for
I have two tables: Users and Roles. A user may have more roles, so
I have these two different queries. This query pulls the records from posts table
We currently have a failover sql cluster with two nodes. For a new large

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.