Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three dimensional database table

We have all been there - consider the following example - first, the client says "every user shall only have one profile picture", so we add a field for that to the users table - half a year later, requirements change and a user actually needs to have n profile pictures.

Now, this seems only possible if you add a new table such as user_pictures to handle the new cardinality 1:n instead of 1:1. Oftentimes this can get very complicated. Whenever I come across this problem, I wonder why we don't use all three dimensions that we can think in. A two dimensional table is limited in a way that it is somewhat incomplete - what if, referring to our problem with the profile picture again, the picture field in the users table had a depth, and that depth made the field an array that perfectly represented both cardinalities 1:1 and 1:n at the same time.

Table fields would simply become arrays and automatically support both cardinalities - wouldn't that be something? At least I would use it. Is there something like it out there already?

like image 410
weltschmerz Avatar asked Dec 14 '12 23:12

weltschmerz


1 Answers

The standard many-to-many approach, many users to many profile pictures, is easily covered by the three table approach:

Table: Users
Table: Pictures
Table: User_Pictures

However, if you move to a NoSQL approach, you can store a User document (usually in JSON format), that stores an array of profile pictures for that user in a single table.

@gordy +1 for the Oracle link. I wasn't sure if any RDBS supposed arrays.

like image 97
Adrian J. Moreno Avatar answered Sep 19 '22 15:09

Adrian J. Moreno