Designing a tag system that can tag multiple db tables

I want to allow users to tag items so that they can search for them using tags. What is the best way to achieve this? So far, the solution I came up with has only added two additional tables to my current db system.

<db Trackable product 1>
int id;
info etc
</>

<db Trackable product 2>
int id;
info etc
</>

//defines the M:M relationship between Tag and various types of Trackable products
<db TagLink>
int trackableProd1Id
int trackableProd2Id
int tagId
</>

<db Tag>
int tagId
tag name etc
</>

Is this a good way? The advantage of this approach is that it should scale well and also allows me to add more tracked products in the future by simply adding a column to the TagLink table. This, of course, is not a good idea, if I plan to track 10 tables, but for 3-4 tables this should work well, right?

+3
2

"TrackableProd_N_id" TagLink, ,

   TagLink table
      int ProdGroup    -- "points" to table 1 vs. table 2 etc.
      int ProductId
      int TagId

, , "" ProdGroup ProductId ( ).

+1

, " " ( m: n, ). :

tags
    id INT NOT NULL AUTO INCREMENT
    name VARCHAR NOT NULL
    .
    .
    .
    possibly other fields
    .
    .
    .
    PRIMARY KEY (id)

items_you_want_to_tag
    id INT NOT NULL AUTO INCREMENT PRIMARY KEY
    name VARCHAR NOT NULL
    .
    .
    .
    possibly other fields
    .
    .
    .
    PRIMARY KEY (id)

xref
    tag_id INT NOT NULL
    item_id INT NOT NULL
    FOREIGN KEY (tag_id) REFERENCES tags(id)
    ON UPDATE CASCADE ON DELETE CASCADE,
    FOREIGN KEY (item_id) REFERENCES items_you_want_to_tag(id)
    ON UPDATE CASCADE ON DELETE CASCADE,
    PRIMARY KEY (tag_id, item_id)

, .

, . , ( Trackable product 1 Trackable product 2 ), . , .

, :)

UPDATE:

, , , , ( mjv;)).

+2

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


All Articles