How to implement multiple relationships in SQL Server?

I am trying to create a database for use with the ASP.net MVC application. Here is the scenario: There are three objects, and users can post their comments for each of these different objects. I'm just wondering how easy it is to put one table for comments and associate all the other objects with it. Obviously, the comment table requires 3 links (foreign key) for these tables, but, as you know, these foreign keys cannot be empty, and only one of them can be filled for each line. Is there a better way than implementing three different tables for the comments of each object?

+3
source share
3 answers

Or: one comment table for each entity type

Or: one main Entity table with child comments and specific EntityType tables.

  • EntityMaster: EntityID, foo, bar
  • Comments: EntityID, CommentID, UserID, ... PK (EntityID, CommentID, etc.)

For tables 3 Entity PK is EntityID

  • EntityOne: EntityID, EntityTypeID (check constraint = 1), ...
  • EntityTwo: EntityID, EntityTypeID (check constraint = 2), ...
  • EntityThree: EntityID, EntityTypeID (check constraint = 3), ...

The lack of a label or elegance in the fact that one parent has one comment table for 3 parents: this is wrong in the conditions of database development.

Personally, I would probably go for option 1 ...

Change to reflect:

Sometimes you have to look at data usage.

, , 1.

, 2 , .

+7

, . "" "-" . cerain, .

, .

+3

db, . CommentHistory Entity1 Entity2.

alt text

, Entity1, :

Select * from Comment where CommentHistoryId = 5 -- '5' it CommentHistoryId from Entity1
+1
source

Source: https://habr.com/ru/post/1782231/


All Articles