multiple tables need one to many relationship

后端 未结 5 1467
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 19:39

I have a SQL database with multiple tables: A, B, C, D. Entities in those tables are quite different things, with different columns, and different kind of relations between

5条回答
  •  [愿得一人]
    2020-12-05 19:51

    I had a similar "problem" with comments for more different object types (e.g. articles and shops in my case, each type has it's own table).

    What I did: in my comments table I have two columns that manage the linking:

    • object_type (ENUM type) that determines the object/table we are linking to, and
    • object_id (unsigned integer type that matches the primary key of your other tables (or the biggest of them)) that point to the exact row in the particular table.

    The column structure is then: id, object_type, object_id, author_id, date, content, etc.

    Important thing here is to have an index on both of the columns, (object_type, object_id), to make indexing fast.

提交回复
热议问题