nubicurio nubicurio - 11 months ago 70
MySQL Question

MySQL (involving arrays) Table Suggestion Needed

I'm trying to create a PHP form on a website that collects users' name, email, address, and their work experience. Name, email, and address all pretty straightforward and have their own field, but the work experience table consists of three fields: "time period", "employer" and "position", and multiple rows will be dynamically added by the user as required (a JavaScript is used to accomplish this on the website).

My question is, what is the best way to model this in the MySQL database? Do I use just one table like this:

| UserId | Name | Email | Address | Time | Employer | Position |

Or should I use two tables like this:

| UserId | Name | Email | Address |

| UserId | Time | Employer | Position |

My guts are telling me that the second solution might be what I need, since I don't know how to add dynamic arrays into a database. But how do I associate the same UserIds of the second table to the first table? If I stick to the first solution using only one table, is it possible to save arrays of Time, Employer and Position under one UserId? Keep in mind that I have no way of knowing in advance how many rows of work experience users will be creating.

Answer Source

Option 2 is the correct since it would create much less redundancy.

Table 1 | UserId | Name | Email | Address |

Table 2 | UserId | Time | Employer | Position |