-3

I have a system where properties can be stored, and linked with multiple other entities (each with their own schema). Let's say that Documents can be linked with Users and Assets. What is the best practice format for linking these things together?

Here are some example tables that exist (each of the schemas containing these core tables has many more tables related):

Users.Users:
    UserId PK
    Username
    Name
    Email

Assets.Assets:
    AssetId PK
    Name
    Description
    Value

Documents.Documents:
    DocumentId PK
    Name
    Version

Here are the options I am considering for how to link the documents to the other entities:

  1. Store a linking table within the Documents schema. This has the advantage of a single place to look for all document links, but requires the Documents schema to refer back to other schemas, when the linking direction feels like it should be the other way.

    • (1A) With a table that has a column for each entity that can link to a document:
    Documents.DocumentLinks
        DocumentId FK NOT NULL
        UserId FK NULL
        AssetId FK NULL
        -- Future columns as needed
    
    • (1B) With a table that maps link types and IDs (an extra advantage here is that the schema never needs to change, but loses foreign key integrity):
    Documents.DocumentLinkTypes
        DocumentLinkTypeId PK
        Name
        -- Sample data: (1, 'User'), (2, 'Asset')
    
    Documents.DocumentLinks
        DocumentId FK
        DocumentLinkTypeId FK
        LinkedItemId --Cannot be FK
    
  2. Each table that links to documents stores its own linking table. This has the advantage of matching the direction in which we think about the data, but has the disadvantage of requiring a new table to be added whenever a new property can have documents linked to it (which could grow to be many places).

    Users.UserDocuments
        UserId FK
        DocumentId FK
    
    Assets.AssetDocuments
        AssetId FK
        DocumentId FK
    
  3. Any other options out there?

I can imagine that the choice comes down to what is most likely to happen - option 2 feel like the most logical to me in terms of data order, but if new links to documents are added regularly then option 1B is better. 1A seems like the "least technically good" option, but also is quick and easy to understand. I'm wondering if commenters have previous experience showing that one option is clearly not cut out, or if there is indeed a well-known best practice for this problem.

5
  • 6
    Why do you have multiple schemas? A schema is generally best used to represent a business domain, not an entity type. Commented Mar 31, 2022 at 10:54
  • Linking tables across schemas would require access to those schemas and is in general more complicated then having all your objects (tables, views, etc.) in one schema. Are you sure your logical partitions are necessary?
    – Jon Raynor
    Commented Mar 31, 2022 at 13:40
  • So I figured that posing a question in simple terms with relatable entities would make this easier to ask, but it seems like the focus has been on the schema separation and not the actual question I asked, which is unfortunate. These tables/schemas are not the actual tables/schemas I'm considering.
    – mft25
    Commented Apr 1, 2022 at 13:35
  • @mft25 the focus is on schemas, because it's a big red flag that your data model is poorly designed. Schemas should be mostly self contained, maybe a common/linking schema. Your question is casually crossing three schemas for what appears to be a fairly basic requirement, it makes it appear that the schema is not a useful level of abstraction and making your development more difficult.
    – Ryathal
    Commented Apr 4, 2022 at 12:37
  • @Ryathal as mentioned it was for the sake of the question (they are not the tables I am working with), and you are helping to answer the question, so thank you.
    – mft25
    Commented Apr 4, 2022 at 16:16

1 Answer 1

3

The multiple schema thing seems weird unless you have a lot of other stuff in the the schemas related to users/documents/assets. As far as linking tables in SQL the standard hasn't really changed in forever.

  • If the relation is one to many, add a FK to the many side.
  • If the relation is many to many, add a table with FKs to both tables
  • If the relation is one to one, pick a side to have a FK and add a unique constraint as well

The tricky part for you would be deciding what schema to add a join table to for many to many relationships since you seem to have too granular of a schema.

5
  • 1
    Definitely agree with your last sentence, which echoes @PhilipKendall's comment. Database schemas should divide business domains, not types of entities. Commented Mar 31, 2022 at 14:13
  • 1
    "If the relation is one to one, pick a side to have a FK and add a unique constraint as well." Or alternatively, you can share PK between two tables, where in the child table the PK is also a FK to the parent table.
    – Archie
    Commented Mar 31, 2022 at 19:27
  • Please see my other comment addressing schemas. The relationships are many to many, but also "many to many to many" because multiple items from the "Documents" table can be linked to multiple items in both the "Users" and "Assets" tables (and potentially more). If it was just "Assets" and "Documents" for example, then option 2 is the obvious choice (as you have implied with the second option in your list). Would you keep adding linking tables for each new many to many relationship, rather than using option 1A or 1B?
    – mft25
    Commented Apr 1, 2022 at 13:40
  • The fact documents can belong to users and assets isn't that important in analyzing, because you are talking about different relationships at that point. you can have a DocumentUser and DocumentAsset table. You do have to be a little careful to not introduce cyclic dependencies, but transitive relations or maintaining directionality (ie User>Asset>Document) on relations can solve that.
    – Ryathal
    Commented Apr 4, 2022 at 12:24
  • @Ryathal perhaps I didn't make it clear, the context of the question is that if more and more entities can be linked to the Documents table, then does it make sense to keep adding a new table for each thing that can link to Documents? Or does it make sense to have a single table for linking Documents with all the other entities that can be linked to Documents? Eg. After DocumentUser and DocumentAsset, should I also add tables (say) DocumentOrder, DocumentAppointment, DocumentReceipt and so on? Or should I change it up to one of the options in 1A/1B at some point?
    – mft25
    Commented Apr 4, 2022 at 16:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.