Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database design, variable number of columns

I have a simple application in which users submit data into a table.

There are currently 3 fields the user can pass values to at the moment. I'm trying to find a solution where the number of description columns can vary, depending on the description list created by the user.

So far I have considered:

  1. having the user_input table have many nullable description fields, say from 1-15 and thus limiting the number of description fields the user can define to 15. This solution is very easy to query and maintain, however is limited to a set number of fields. (is this a viable and acceptable solution in general?)
  2. creating a table where each row would correspond to 1 description entered. This would allow the user to create unlimited number of description fields, however storing each all inputs would instead of 1 row now take n-rows, where n is the count of descriptions linked to the current description_list. Users are free to choose the amount of columns, however not so easy to query and maintain.

My current table is something like this:

CREATE TABLE `user_input` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`description_list_id` int(11) NOT NULL,
`description1` int(11) NOT NULL,
`description2` int(11) NOT NULL,
`description3` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Are there any other solutions?

like image 894
neo112 Avatar asked Oct 21 '13 17:10

neo112


People also ask

How many columns should a database have?

There is no precise guidance. A table could be as little as one column or as many as the max, 1024. However, in general, you'll probably see no more than 10-15 columns in a table in a well normalized database.

How many columns can a database have?

For the columns in a table, there is a maximum limit of 1024 columns in a table. SQL Server does have a wide-table feature that allows a table to have up to 30,000 columns instead of 1024.

How many columns are there in the dataset?

The number of columns ranges from 2 to 69: most datasets measure few variables, but several measure 30 or more. We consider a case study using SQL-as-a-Service to support "instant analysis" of weakly structured relational data at a multi-investigator science retreat.

How do I find the number of columns in MySQL?

To find the number of columns in a MySQL table, use the count(*) function with information_schema. columns and the WHERE clause.


2 Answers

Definitely option #2. Normalizaing is always the best option in a scenario like this. You are correct that it is more work, but you are overcoming the inevitable issue when you will need more than 15 descriptions.

like image 83
Joe Avatar answered Sep 17 '22 21:09

Joe


The second solution is preferable in terms of flexibility. If tomorrow you need to add more description fields with the first solution you will need to modify the table and the code to manage it.

The second solution can require a bit more work now but then it will handle 2 like 200 descriptions.

The first approach is more a quick and dirty solution for a small problem, the second also a good exercise if you have time to try something new.

like image 30
mucio Avatar answered Sep 16 '22 21:09

mucio