How to model mutually exclusive relationships in SQL Server

I need to add functionality to an existing application, and I came across a data situation in which I am not sure how to model. I will limit myself to creating new tables and code. If I need to change the existing structure, I think that my client can reject the offer. Although, if this is the only way to do it right, this is what I will need to do.

I have a table of elements that allows me to refer to any number of tables, and these tables can grow over time. An element can only associate me with one other table, but a record in another table can contain many elements associated with it.

Examples of linked tables / entities are Person, Vehicle, Building, Office. These are all separate tables.

EXAMPLE elements: Pen, Stapler, Cushion, Tyre, A4 Paper, Plastic Bag, Poster,Decoration"

For example, Postermay be allocated to Personor Officeor Building. In the future, if they add a table Conference Room, it can also be added to this.

My inner thinking:

Item
{
  ID,
  Name
}

LinkedItem
{
  ItemID,
  LinkedToTableName,
  LinkedToID
}

The field then LinkedToTableNameallows me to identify the correct table for the link in my code.

I am not too happy with this decision, but I can’t remember anything. Please help! :)

Thank!

+5
source share
3 answers

. .

, . . ORM, . , - Person, Vehicle, - , , , .

, . , , ( , ).

: , , . , , - , - "" (Person ..) ( ).

, . Item , .

, - , . . , , "". , , , ( ) , , .

, , Linq to SQL ORM.

. : SQL Server.

+12

- , , , . - Entity. -/ Entity . , EntityID PK a FK . Entity (). Entity; Entity () .

model_mutually_exclusive_01

+9

.

, , , sql . sql FROM .

, , - , (, ), type_id, , , - .

, , , .

0

All Articles