0

I have a table where I am storing different documents of different source as follows

CREATE TABLE [dbo].[Document](
    [DocumentId] [int] IDENTITY(1,1) NOT NULL,
    [EntityId] [int] NOT NULL,
    [DocumentGuid] [uniqueidentifier] NOT NULL,
    [DocumentTypeCdId] [int] NOT NULL,
    [DocumentName] [nvarchar](500) NOT NULL,
    [DocumentType] [nvarchar](500) NOT NULL,
    [DocumentData] [nvarchar](max) NOT NULL,
    [IsSuppressed] [bit] NULL,
    [CreatedBy] [nvarchar](200) NULL,
    [CreatedDt] [datetime] NULL,
    [UpdatedBy] [nvarchar](200) NULL,
    [UpdatedDt] [datetime] NULL,
 CONSTRAINT [PK_Document] PRIMARY KEY CLUSTERED 
(
    [DocumentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

ALTER TABLE [dbo].[Document]  WITH CHECK ADD  CONSTRAINT [FK_Document_DocumentTypeCd] FOREIGN KEY([DocumentTypeCdId])
REFERENCES [dbo].[DocumentTypeCd] ([DocumentTypeCdId])
GO

ALTER TABLE [dbo].[Document] CHECK CONSTRAINT [FK_Document_DocumentTypeCd]
GO

EntityId will be from different source tables, so can I add this column to be a FK of all those source table. Currently I have nearly 10 Source tables. If not what is the better approach to handle this scenario

Thom A
  • 88,727
  • 11
  • 45
  • 75
Developer
  • 8,390
  • 41
  • 129
  • 238
  • 1
    A column can't be a foreign key of different columns from multiple tables, that just wouldn't work. How would it know what table to check it's validity against? – Thom A Oct 07 '21 at 15:38
  • I have a master table DocumentTypeCd which tells which it refers to – Developer Oct 07 '21 at 15:40
  • So what is the problem then? – Thom A Oct 07 '21 at 15:40
  • I just want to know is it a good practice – Developer Oct 07 '21 at 15:41
  • 2
    The different sources is you main problem. – jarlh Oct 07 '21 at 15:41
  • The problem sounds more like that you have 10 tables where you should have 1. I don't think we have all the information here, if i am honest. – Thom A Oct 07 '21 at 15:41
  • Yeah I agree but instead of creating 10 tables I created one and mapped that way with out adding FK relation – Developer Oct 07 '21 at 15:42
  • You could have 10 tables, if what you are trying to represent is a polymorphic association (derived classes in a language like C#). Then you also have an `Entity` table, which has a `Type` column, the primary key is made up of that plus the ID. Then the 10 child tables all have the same primary key, foreign-keyed to `Entity`, and a check constraint ensuring all have the same `Type` per table. So this `Document` table is then FK'd to `Entity`, not the child tables – Charlieface Oct 07 '21 at 19:26
  • Does this answer your question? [What is the best way to implement Polymorphic Association in SQL Server?](https://stackoverflow.com/questions/7000283/what-is-the-best-way-to-implement-polymorphic-association-in-sql-server) – Charlieface Oct 07 '21 at 19:26

1 Answers1

0

You have a problem in the design of your database. In such a case you need to have a parent ancestor table that hold the keys of all type of documents, then multiple children table, each one speciallized for a speciic document type.

This is called inheritance and children must not share the same key value (children table with excusion ids...)

SQLpro
  • 3,994
  • 1
  • 6
  • 14