Hi,
I am currently building a small web application for publishing stories using PHP and MySQL. Each story may either be a article (with lots of fields and data), just a hyperlink/url, just an image, or a downloadable file with certain attributes (once again with lots of fields).
My problem is related to finding the optimal database layout for the system. I have thought about 3 different solutions that may or may not work (fields are just examples, don't get caught of with whether the data should be stored in the database or not):
1) Making one table for each type and just UNION them. E.g:
articles
id | date | title | author | preview | last_edited | category | content
hyperlinks
id | date | title | author | url
pictures
id | date | title | author | thumbnail | fullimg
files
id | date | title | author | filetype | filename | filecontent
Is it possible to make a SQL statement that will concat these in such a way that every field is added to the rows from the individual tables and the individual row is "tagged" with its type depending on which table its from? Or do I have to settle for just the common fields (3-4 field WILL be identical for each table) and to individual lookups for each row?
2) Making some sort of index table that references the correct table and row depending on the type?
index
id | type | title | date | referenceid
-> either articles, hyperlinks, pictures or files depending on the "type" field.
Is this possible in some way when using foreign keys? I suspect no, but not really sure what you can and can not do in MySQL.
3) Have ALL fields for ALL types in one and the same table and just make the specific field nullable (I dont really like this solution since it appears messy).
Any ideas?
I suspect I can not be the first person to have encountered the issue of references to different entity types/fields depending on some sort of type.
Ideas are highly appreciated.