开发者

Database Design: Trouble Connecting Ideas

开发者 https://www.devze.com 2023-03-18 08:48 出处:网络
I\'ve hit a bit of a wall ... here is my simplified schema: TABLE users id TABLE assets id userid TABLE presentations

I've hit a bit of a wall ... here is my simplified schema:

TABLE users
id

TABLE assets
id
userid

TABLE presentations
id
userid

At this point, it's easy to create a relationship between USERS -> ASSETS and USERS -> PRESENTATIONS ... but where I run into a bit of a brain fart is when I introduce TEMPLATES for the PRESENTATIONS:

TABLE templates
id

You see, each TEMPLATE has several positions... not all TEMPLATES have the same amount of positions. Each position houses an ASSET or is empty.

I'm having a hard time trying to figure out which way is the best way to connect PRESENTATIONS -> TEMPLATES (and position) -> ASSETS ... and keep in mind, not all ASSETS are associated with a PRESENTATION.

Proposed Schema:

TABLE presentations
id

TABLE templates
id

TABLE positions
id
templateid
assetid

But there is a problem with this. This assumes that each TEMPLATE can just a random amount of POSITIONS. Yes, a TEMPLATE has a 1 - n relationship with POSITIONS, but not really. Because a TEMPLATE wouldn't be a TEMPLA开发者_运维技巧TE if it wasn't repeatable. TEMPLATE 1 has 4 positions, and will always have 4.


OK, let's see as promised:

table Presentation
int id
varchar name
int fk_template (references template.id)
....

table Template
int id
varchar name
.....

table Position
int id
int zorder (if it is the 1st, 2nd, 3rd position of the given template)
int fk_template (references Template.id)
.....

table Asset
int id
varchar name
varchar description
....

table AssetForPresentation
int fk_asset (references Asset.id)
int fk_presentation (refernces Presentation.id)
int fk_position (ferences Position.id)

Now you have templates that have fixed positions. Every presentation uses one template. For every presentation and every position inside this presentation you can define one asset or none. You must make sure that in table AssetForPresentation only position ids are used that are actually member of the template used by this presentation. If a position of a given presentation has no asset, just don't create an entry in table AssetForPresentation.


I am not sure if I understood correctly:

table templates
id

table position
id
fk_templateid 
fk_assetid

Being fk_assetid nullable to allow positions without assets.

And if each presentation uses exactly one template add a fk in table presentation referencing templates.id.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号