简体   繁体   中英

Database Design for Unknown number of inputs

I am trying to store some data in database but could not figure out how the database should be designed for maximum productivity...

User has an option to choose number of entries in a form. And then these entries are required to be saved in the database. It is however not known how many entries the user wants eg they can be 1 or 100.

How is it better to save such data?

What i have in mind is to store the number of entries in database with the userID and put the inputs into an array, serialize it and store it in the database.

I would probably do the following:

+---------------+
| Table: users  |
+----+----------+
| id | username |
+----+----------+
|  1 | tanzeel  |
|  2 | john     |
+----+----------+

+-----------------------------------------------+
| Table: entries                                |
+----+---------+------------+-------------------+
| id | user_id | entry_name | single_entry_data |
+----+---------+------------+-------------------+
|  1 |       1 | prop1      | ################  |
|  2 |       1 | prop2      | ################  |
|  3 |       1 | prop3      | ################  |
|  4 |       2 | prop1      | ################  |
|  5 |       2 | prop2      | ################  |
|  6 |       1 | prop4      | ################  |
+----+---------+------------+-------------------+

The entries table stores n entries for each user associated with the user_id which is a FK (foreign key) reference to the PK (primary key) column id of table users . When you want to retrieve the data you can fire a select query as follows to get the entries for a particular user.

SELECT * FROM entries A
INNER JOIN users B
ON A.user_id = B.id
AND B.id = 1;

If there are the same options for different input field types, like a text description, you should use an approach like this.

table       columns                  example values
-----       -------                  --------------
users       id, name, password       12, "Johnny", "afad13cf1941"
forms       id, user                 42, 12
inputs      id, form, type, options  6, 42, "checkbox", "Want newsletter?"

If there are different options for different input fields, like a checked state which only belong to a checkbox, try the following. But in this case you would have to create a table for each input type you want to provide, in this example checkboxes , textfields and images .

table       columns                  example values
-----       -------                  --------------
users       id, name, password       12, "Johnny", "afad13cf1941"
forms       id, user                 42, 12
inputs      form, type, input        42, "checkbox", 8

checkboxes  id, text, checked        8, "Want newsletter?", false
textfields  id, text                 9, "Welcome to that page"
images      id, size, caption        10, 720, "The Mona Lisa"

The type column tells use in which table you will find the options of the input field under the id from the input column.

Note that in both examples, every column called id is the primary key. In the very last table a combination of type and input would be the primary key.

It may be that a nosql-database fits better for what you want to do. - look up eg couchdb.

If you want to / have to use mysql and you need to be able to search for single values, do as Tanzeel's and sharethis. If what you want to do is to fetch the entire set of parameters given a user / and / or form id, you should probably best store the serialized data as a varchar or text field. If you choose to serialize, see if json fits your bill. Then you have a lot of tools to handle your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM