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.
Share
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 arebar1and bar2`: