How should I set up my database / table, if I do not know the number of fields I would populate per record?
For example, if I have a web form that allows a user to enter all the cars he owns, and I don’t want to limit him to a certain number, how would I store this in the database end?
The above problem extends to similar situations such as storing a user’s order (variable number of items per order) etc.
In Relational Database Management Systems (RDBMS) instead you create child records in a dependent table that relate child entities (cars) with parent entities (users). There is a concept known as database normalization, and the objective is that each table contains data for a single type of entity.
So you have a
usertable with the user information:Then another table for storing the information of each car of a user:
So instead of storing the info of the cars in the
usertable, you have a table for storing car (user_car) information related to each user by way of theuser_idcolumn. This is an example of a one-to-many relationship, in which one user can have many related cars.