I’m not very well traversed in database design so I’ve come to you guys for help. I have a table with records of users. The tablet looks like thing like this:
+----------+----------+----------+----------+----------+----------+----------+---------
| id | username | password | firstName| lastName | birthDate| pictures| .....
+----------+----------+----------+----------+----------+----------+----------+---------
| . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | .....
. . . . . . .
The pictures field indicates how many pictures a user has uploaded.
I have written a feature that allows users to upload pictures to our servers. The file uploaded is named to something random. Users are only allowed 6 images.
I am currently getting the location of the file and storing the locations in a table that looks like this:
+----------+----------+----------+----------+----------+----------+----------+---------
| id | username | default | pic1 | pic2 | pic3 | pic4 | .....
+----------+----------+----------+----------+----------+----------+----------+---------
| . | . | . | . | . | . | . |
| . | . | . | . | . | . | . | .....
. . . . . . .
Assuming a user has more then 0 pictures, when a users profile is visited the ‘default’ field is called upon to get which column stores the address of the default picture. For example if ‘default’ is equal to 2, the data from column ‘pic2’ would be pulled and returned first, then the other non empty columns would return their respective data.
My first question is: is this method of having multiple tables a bad idea? Is implementing a design where a picture has its own column a bad idea?
As you guys can see this method is not scaleable in the least bit. What would be a good, efficient, scaleable design to implement this profile system? I am expecting to have 50,000 – 60,000 users.
Also if possible recommend me a good book that will give me a crash course in database design! (crash course please nothing too long or detailed!)
Unless there are very specific reasons why you need this design (highly doubt it) I think you should go with something like:
This scales well (assuming you have an index on user_id) and also you can easily change your mind on the max pictures per user restriction. The restriction will, of course, be enforced by your application.