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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:07:51+00:00 2026-06-01T14:07:51+00:00

I am using sqlite3 in python. I have two tables with id column in

  • 0

I am using sqlite3 in python. I have two tables with id column in each. These id columns in the tables can have different values. I want to create a new table with column id which can contain only the values by combining the values from the id columns of first two tables. I am able to successfully create foreign key(link) with any one of the table but not with both. I also tried creating a join view from the two tables and tried linking the third table with the join, but didn’t work.

  • 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-01T14:07:52+00:00Added an answer on June 1, 2026 at 2:07 pm

    You can use triggers to create the equivalent of a foreign key on the union of columns from two distinct tables. You’ll need to create triggers for inserts and updates on the constrained table, and for updates and deletes on the reference tables. If I’m not missing anything, you’ll need six triggers in total.

    See this link where the author pretty much tells you how to do it as it was written before sqlite started to enforce foreign keys:

    http://www.sqlite.org/cvstrac/wiki?p=ForeignKeyTriggers

    For example, this would be the INSERT trigger for the constrained table where the constrained table is foo, and the reference tables are bar1 and bar2`:

    pragma foreign_keys=on;
    
    create table bar1 (id integer primary key);
    create table bar2 (id integer primary key);
    
    insert into bar1 values (1);
    insert into bar1 values (2);
    insert into bar2 values (2);
    insert into bar2 values (3);
    
    create table foo (id integer);
    
    CREATE TRIGGER fk_insert_foo
    BEFORE INSERT ON foo
        FOR EACH ROW 
        BEGIN
          SELECT RAISE(ROLLBACK, 'insert on table foo violates foo.id '
                                 + 'constraint on union of bar1.id and bar2.id')
          WHERE NOT (EXISTS (SELECT * FROM bar1 WHERE bar1.id=NEW.id)
                     OR 
                     EXISTS (SELECT * FROM bar2 WHERE bar2.id=NEW.id));
        END;
    
    insert into foo values (1);
    insert into foo values (2);
    insert into foo values (3);
    select * from foo;
    insert into foo values (4); # <<<<<< this fails
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using sqlite3, I have two tables: products, orders. I want to know how many
I have a table created using Python + Sqlite3 c.execute('CREATE TABLE IF NOT EXISTS
I have a project built in Python 2.7, using SQLite3 as the database. I
So let's say I'm using Python 2.5's built-in default sqlite3 and I have a
Using sqlite3, python I have a crappy legacy file legacy_sales : item | year
Using sqlite3, I have a table that looks like this: +---------+-----------------+----------+-----------+--------+ | ArtId |
I have an app using SQLite3. It's running pretty well, but I would like
I have created a file named MyFile.db using SQLite3 from my C#.net application. This
What would anyone consider the most efficient way to merge two datasets using Python?
Using the sqlite3 standard library in python 2.6.4, the following query works fine on

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.