I have a m:n connection between the table "Bill" and the tables "GeneralArticles", "CalculatedArticles" and "AdditionalRecords".
These tables are straight-forward. Each has its own ID (Let's call them B_ID and for the other ones GA_ID, CA_ID and AR_ID) and some additional fields. The question is how to set up one m:n-connection-table between Bill and these three tables guaranteeing referential integrity?
My first idea was to simply create:
CREATE TABLE Bill_Articles(
[B_ID] [int] NOT NULL FOREIGN KEY REFERENCES Bill (B_ID) ,
[A_ID] [int] NOT NULL ,
Category [int] NOT NULL
CONSTRAINT [PK_Bill_Articles] PRIMARY KEY CLUSTERED
(
[B_ID] ASC,
[A_ID] ASC,
[Category] ASC
))
I've set up a table for the Categories to, so theoretically I could use 1 for General Articles, and so on, you get the idea. Yet the problem is how is it possible to refer to another table depending on the Category?
Another solution would be to create a View which contains this Category-ID and the ID of the Record:
Create View Articles
AS
Select 1 Category,
GA_ID A_ID FROM GeneralArticles
UNION
SELECT 2 Category,
CA_ID A_ID FROM CalculatedArticles
UNION
SELECT 3 Category,
AR_ID A_ID
FROM AdditionalRecords
Unfortunately when setting up the Bill_Articles table I get the error:
Foreign key 'FK__Bill_Ar_A_ID__7526B52E' references object 'Articles' which is not a user table.
Somehow I can't use this kind of View for providing unique keys.
Does anyone know a proper solution for this kind of problem? Thank you