Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database Schema: How the relationship can be designed between user, file and folder?

I was wondering how the database schema of Dropbox would be designed? I am thinking about three tables: User, File, Folder with following constraints.

Constraints:

  1. A user can contain multiple files and folders.
  2. Each file can be shared among multiple users.
  3. A file can be organized in folder for one user but not for another user(if the file is shared).
  4. Each folder can contain multiple files as well as other folders.

How would you create a relationship between them?

like image 392
Agent47DarkSoul Avatar asked Mar 16 '12 11:03

Agent47DarkSoul


People also ask

What is a relationship database schema?

A relational schema is a set of relational tables and associated items that are related to one another. All of the base tables, views, indexes, domains, user roles, stored modules, and other items that a user creates to fulfill the data needs of a particular enterprise or set of applications belong to one schema.

What are the 3 types of database schema?

Schema is of three types: Logical Schema, Physical Schema and view Schema.


2 Answers

This ERD fits the rules you've described:

ERD

Note that in this schema, every user has their own individualized view of folders. Files, on the other hand, are represented only once but can be assigned to one or more folders, even for different users.


EDIT: (slightly) expanded model:

With the addition of a single table to track physical folders and their contents, my proposed schema also addresses all of the additional functionality suggested by Branko Dimitrijevic:

enter image description here

like image 141
Joel Brown Avatar answered Sep 22 '22 15:09

Joel Brown


The Joel Brown's model is a good start, but let me tickle your imagination with another possibility:

enter image description here

This model has the following properties:

  • It emulates file system hard links. You can have a single file in multiple folders or even multiple times in the same folder, under different names. Ditto for folders.
  • It separates naming from content, which is necessary for the "hard links" paradigm. So, names are in FILE_IN_FOLDER and FOLDER_IN_FOLDER (instead of in FILE and FOLDER).
  • It properly enforces the "local" name uniqueness (you can't have two files or folders named the same way under the same parent folder).
  • A user owns neither files nor folders. User just owns links. This way, each user has her own private "directory tree", potentially sharing all files, or none or anything in between.

The question, of course, is whether the increased flexibility you get with this model is worth the extra complexity, but this is something only you can answer...

like image 31
Branko Dimitrijevic Avatar answered Sep 23 '22 15:09

Branko Dimitrijevic