I have three tables
USER
USER_ID
USERNAME
[additional fields]
ASSET
ASSET_ID
DISPLAY_NAME
[additional fields]
PAGE
PAGE_ID
[additional fields]
The USER table is what you would expect and standard USER table. Each user can add assets to their library, so there is a 1 to many relationship between USER and ASSET. Additionally, each ASSET is made up of multiple pages, and each PAGE can have multiple notes that is just for that user to see. My initial thought is to have a lookup table between all three tables:
USER_ASSET_PAGE
UAP_ID
USER_ID
ASSET_ID
PAGE_ID
NOTE
So now I have to query the USER_ASSET_PAGE table to get notes that correspond to a specific page of a specific document for a distinct user:
SELECT NOTE
FROM USER_ASSET_PAGE
USER_ID = {user_id} AND ASSET_ID = {asset_id} AND PAGE_ID = {page_id}
Is there a better way to structure this type of data? It feels like there maybe complications down the road.
Well based on your description, I personally would use the following scenario:
then, to query for notes from a specific user,asset and page:
This is obviously a normalized attempt. Of course it all depends on what your goals are and the expected data to be stored. With low amount of data, your denormalize design would be enough. However with major amounts of data, hundreds of users with several assets with hundreds of pages, you could face a great growth on your
USER_ASSET_PAGEtable.My approach would serve better an operational purpose, rather than a “data exploration” one. Later on if you would need to perform some major aggregation on these tables it would be a good idea to denormalize data like you did and look for some optimization as well.