I think you need to reference the ID of table X in table B and A. You will need to define two foreign keys for table B and each foreign key should point to the required fields of one table. It means one foreign key per foreign table.