Link to home
Start Free TrialLog in
Avatar of Kinderly Wade
Kinderly WadeFlag for United States of America

asked on

can a data table contain more than one foreign key with same id?

Dear experts,

I have a question concerning with mysql data tables.

Here is the scenario. I have three tables. One table contains information of local quotes. Another table contains information of oversea quotes. I wish to setup two foreign key constraints in order table which can handle ids from local and oversea order tables.

here is my sample tables (DDL):

local table:

id   int auto_increment,
quote_header int,
cust_no varchar(10),
src_tbl varchar(1),
...... (there is more columns)

oversea table:

id   int auto_increment,
quote_header int,
cust_no varchar(10),
src_tbl varchar(1),
...... (there is more columns)

local and oversea table structure are not exactly the same (slight different in column counts and name)

Is it possible that I can create foreign key constraints in order table like this?

ALTER TABLE orders ADD FOREIGN CONSTRAINTS oversea_order_fk FOREIGN KEY (ref_id, src_tbl) REFERENCES oversea (id, src_tbl)
ALTER TABLE orders ADD FOREIGN CONSTRAINTS local_order_fk FOREIGN KEY (ref_id, src_tbl) REFERENCES local (id, src_tbl)

The src_tbl column is used for referencing to either local or the oversea table because both local and oversea table can have an auto number of 1. If I have a composite key then I can have two #1s existing in orders table that can be
distinguished by src_tbl column as in 'O' for oversea and 'L' for local. Example will be:

order table:

id int,
ref_id int
src_tbl

id       ref_id      src_tbl
1            1            O  ---> refers to row 1 in oversea table
2            1             L ----> refers to row 1 in local table

If not possible what else can I do in order to reference order's ref_id with local and oversea tables?

Let me know if anyone needs more data in order to have a better picture of this situation.

Thanks.
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kinderly Wade

ASKER

Thank you Guy Hengel and ste5an. Those are the answers that I am looking for. I just raised the points which I can allocate more points for you guys. Great answers.
Thank you Guy Hengel and ste5an. Those are the answers that I am looking for. I just raised the points which I can allocate more points for you guys. Great answers. Both repliers has great solution because Guy Hengel points out that it is not possible with my method but there is a work around and generally described the work around. Ste5an actually goes into detail with the work around which stated clearly how it shall be done. These answers save me time and point me at the right direction. Thanks again.