Creating a table with a field (with a foreign key) which can reference many tables and maintain referencial integrity?

StackOverflow https://stackoverflow.com/questions/19927560

  •  30-07-2022
  •  | 
  •  

Question

what is the best way of creating a table which can hold a key to a lot of other tables? As far as I know I have two options:
1) I create a table with a lot of foreign key fields
2) I create two fields, one which indicates the referenced table and another field which holds the primary key of that table.

The latter has a lot of issues due to the fact there's no way to maintain referential integrity (because there's no foreign key to each table).

Besides a link to this table I want to add a description so I can show all notifications in a grid. By clicking a line in the grid I want to open the corresponding program and fix the issue in that program.

It's a bit hard to explain, perhaps this example explains better:
I need to create a system which handles task/notes/notifications for every program in our business application. We have invoices, sales-orders, deliveries, production-orders, etc Our software detects that something is wrong which any of these. For instance, if the profits on a sales-order are not high enough the order can't be validated automatically. In this case I want to create a notification for the sales-manager so that he can check out what's wrong with the sales-order.

FYI: Iam using Sybase SQL Anywhere 12.

Does it make any sense?

Was it helpful?

Solution

This can be solved in reverse way. Lets say that you have table Alerts where you are going to put all kind of alerts about bad things happened elsewhere. You may reference this table from ALL other tables in your system and create non-mandatory relationship from them. In short it may look like (i'm using MSSQL syntax):

create table Alerts(
   ID int not null identity,
   SomeInfoAboutTheProblem varchar(255),
   constraint PK_Alerts primary key (ID)
)

create table Invoices(
   ID....
   AlertID int NULL,
  ....
   constraint FK_Invoices2Alerts foreign key (AlertID) references Alerts(ID)
)

In case you cannot modify your tables with business information you may create "extention" table for Alerts that may store some specific problem information and actual reference to the problematic record. For example:

create table Alerts(
   ID int not null identity,
   SomeInfoAboutTheProblem varchar(255),
   constraint PK_Alerts primary key (ID)
)
create table Alerts_for_Invoices(
   AlertID int NOT NULL,
   InvoiceID int NOT NULL,
   SomeAdditionalInvoiceProblemInfo ....,
   constraint FK_Alerts_for_Invoices2Alerts foreign key (AlertID) references(ID),
   constraint FK_Alerts_for_Invoices2Invoices foreign key (InvoiceID) references Invoices(ID)
)

To show list of problems you may just select general information from Alerts table while opening the dialog you may select all appropriate information regading the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top