I am creating a blog or something like email, where the user can attach files. Technically there is no limitation in how many files he can add (forget about the possibility for now). If the user have added 10 attachment, how should I manage my table to store the url path of those attachment. Should I create 20 columns in the table for saving the url path of the attachment? What if the user wants to attach more than 20 files? I am using mysql database.
Share
This is a classic “one to many” relationship. Represent the “many” portion by a separate table, which will be a child endpoint of a foreign key whose parent endpoint is your “main” table.
Something like this:
You can now easily insert several rows into ATTACHMENT that all reference the same row from EMAIL.
The foreign key ensures that you can’t insert an attachment for a non-existent e-mail, nor you can delete the e-mail and leave its attachments “hanging in the air”.