views:

30

answers:

1

Hi

I new in database design.

I have a question with my own few solution, what do you think?

Which tables should be created for all the kinds files that stored in my online store (images, attached email files, text files for store email body, etc.) ?

option 1: use seperate table for files types

files{
id
files_types_id FK
file_path
file_extension
}

files_types
id
type_name (unique)
}

option 2: use bool field for each file type

files{
id
file_path
file_extension
is_image_main
is_image_icon
is_image_logo
is_pdf_file
is_text_file
}

option 3: use one ENUM field 'file_type' for each file type

files{
id
file_path
file_extension
file_type (image_main,image_icon,image_logo,image_main,pdf,text) **enum**
}

Thank you, Yosef

+2  A: 

I would go with option 1 -- it's the "classic" fully normalized form.

I think of option 3 as the "poor man's normalization" -- the column is still constrained to the limited set of types, but you don't make the list of possible types available to database users. (In SQL Server, this would be done with a CHECK constraint.) Depending on your project and the (lack of) relevance of the data this can be worth doing, but "file type" information is useful and important and I wouldn't want to hide it.

To be complete, I'll toss in Option 4: create one table for every type of file:

files_text{ 
id 
file_path 
file_extension 
} 

files_pdf{ 
id 
file_path 
file_extension 
} 

files_image_main{ 
id 
file_path 
file_extension 
} 

etc.

This structure too can have its place, but it is generally horrible design, and you are wise not to have listed it. (Hmm, what does that make me?)

Philip Kelley
thank you, do you have classic example of enum use?(I didnt see use of emun in database)
Yosef
I am not familiar with MySql. If you are going with "option 3", read up on column and table constraints. There should be one, perhaps named the "check" constraint, that limits values that may be entered in a column. Checks often look like "> 0", "between 1 and 10", or "in ('image_main', 'image_icon', 'image_logo', 'image_main', 'pdf,text')"
Philip Kelley