Jump to content

Varying number of variables in mysql


python72

Recommended Posts

Is there a way to be able to vary number of fields in database? Just to explain I will use an example.

Lets say we have table with user details, the table has the following fields:

User Name - single value for each user

User DOB  - single value for each user

Kid's Names - this can vary, someone can have one kid, others can have several

 

So I am basically looking for a way to accomodate the varying number of kids names for different users, something like array in programming. What is the proper way to deal with this in mysql? I am new to database and web programming in general so all I can think of right now is to create more than enough fields, like Kid1, Kid2, Kid3 ... Kid20 and when processing read them till null or empty field appear, but I think there should be better way to do it?

 

Thanks for all your help.

Link to comment
Share on other sites

The proper way is to learn about this concept called database normalization. Core concept #1: if you find yourself wanting to create columns like X1, X2, X3... then you need a separate table.

 

The user table has an ID number and the simple user stuff (name, birthdate, etc). Then you create a table for the kids: an ID number, user ID, and kid (whatever that involves).

If a user has one kid then they'll have one row in the kids table. If they have two kids, two rows. And so on.

Link to comment
Share on other sites

Thanks for the answer, I thought about that but hoped there is an easier way. It seems that if there is few fields that can have variable number of entries the queries will be very resource and time consuming, am I right or not?

It is the easier way. At least it will be when you try to write logic for the whole thing: using multiple columns is hell to program for.

 

So I guess the answer is "no". It will not be resource or time consuming.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.