T-SQL Tag Database Architecture Design?

前端 未结 3 1687
伪装坚强ぢ
伪装坚强ぢ 2021-02-11 02:51

Scenario

I am building a database that contains a series of different tables. These consist of a COMMENTS table, a BLOGS table & an ARTICLES table. I want to be abl

相关标签:
3条回答
  • 2021-02-11 03:28

    usually many-to-many relationship implemented exactly as you describe it.

    Auto-incrementing IDs it is good idea since it guarantee that they will be unique.

    And you can use guids if you want to tag comments and articles with the same tag(instead of 6 tables you need just 5). But searching with guids may be more slow.

    0 讨论(0)
  • 2021-02-11 03:29

    Keeping a centralized table of tags is a good idea if you will ever need to do one of the following:

    1. Build a complete list of all tags (that is mixing blog tags, comment tags and article tags)
      • Update the tags so that they get updated everywhere: so that when you change sqlserver to sql-server, it gets changed anywhere: in blogs, articles and comments.

    Option 1 is very useful to build the tag clouds so I'd recommend to build a table of tags and reference it from your tables.

    If you won't ever need to update the tags as described in the option 2, you don't ever need surrogate key for them.

    You will most probably need a UNIQUE constraint on them anyway and there is no point not to make it a PRIMARY KEY, if you are not going to update them.

    This will also save you lots of joins: you don't need to join with the tags table to show the tags.

    GUIDs are more simple to manage, but theу make the indexes and link tables quite large in size.

    You can assign a numerical identifier to each table and link like this:

    tTag (tag VARCHAR(30) NOT NULL PRIMARY KEY)
    
    tTaggable (type INT NOT NULL, id INT NOT NULL, PRIMARY KEY (type, id))
    
    tTagLink (
            tag VARCHAR(30) NOT NULL FOREIGN KEY REFERENCES tTag,
            type INT NOT NULL, id INT NOT NULL,
            PRIMARY KEY (tag, type, id),
            FOREIGN KEY (type, id) REFERENCES tTaggable
            )
    
    tBlog (
            id INT NOT NULL PRIMARY KEY,
            type INT NOT NULL, CHECK(type = 1),
            FOREIGN KEY (type, id) REFERENCES tTaggable,
            …)
    
    tArticle (
            id INT NOT NULL,
            blog INT NOT NULL FOREIGN KEY REFERENCES tBlog,
            type INT NOT NULL, CHECK(type = 2),
            FOREIGN KEY (type, id) REFERENCES tTaggable,
            …)
    
    
    tComment (
            id INT NOT NULL PRIMARY KEY,
            article INT NOT NULL FOREIGN KEY REFERENCES tArticle,
            type INT NOT NULL, CHECK(type = 3),
            FOREIGN KEY (type, id) REFERENCES tTaggable,
            …)
    

    Note that if you want to delete a blog, an article or a comment, you should delete from tTaggable as well.

    This way, tTaggable is only used to ensure the referential integrity. To query all tags for an article, you just issue this query:

    SELECT  tag
    FROM    tTagLink
    WHERE   type = 2
            AND id = 1234567
    

    , so you get all tags by querying a single table, without any joins.

    0 讨论(0)
  • 2021-02-11 03:42

    You may also want to look at WordPress schema and database description to see how others are solving a similar problem.

    0 讨论(0)
提交回复
热议问题