简体   繁体   中英

MySQL Database Design with tags across multiple tables

I am working on some web apps which should all use the same user table. The different applications all need different table designs, so I created one table for each app, with the UserID being a foreign key referring to the user table.

Now I want to add tags to all apps. The tags should be in one table for every app in order to easily query all tags from one user(for searching purposes, the search should be able to find everything tagged with that tag, no matter the app). Personally, I don't think splitting them up into multiple tables would be a good idea, but I am not that into database design so I might be wrong. My current attempt looks something like this:

[tags]

EntryID | UserID | Tag

The thing is that the EntryIDs of course would have to be unique across all app tables with this solution. For the notes app I need something like this:

[notes]

EntryID | UserID | title | content | etc.

For my calendar I have the following table:

[calendar]

EntryID | UserID | name | start | end | etc.

Now I don't know how to manage those EntryIDs. Should I create another table like this

[entries]

EntryID | UserID | type

with type being something like "note" or "calendar", and EntryID being the primary key? And should the type be something like an integer, or a string, or is there a possibility to kind of refer to another table in the type column? And should I then make the EntryIDs in the app tables into foreign keys referring to the entries table?

I put the userID in every table because I think this is going to speed up querying, for example when I need every tag one user has set across all apps. I know normalization usually prohibits this, but I again think that it would very much increase query speed and reduce load for both the MySQL server and my back-end.

I would appreciate every tip for structuring this, and thanks in advance!

You can use inheritance, similar to this:

在此处输入图片说明

I'm not sure what the role of the user is supposed to be here, exactly. In the model above, user "owns" an entry and (presumably) tags it. If you want multiple users to (be able to) tag the same entry, USER would need to be connected to the junctions table TAG_ENTITY.

For more on how to physically implement inheritance, see here .

You may also be interested in this and this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM