简体   繁体   中英

Best way store an array of items with details in SQL table

I'm trying to find out the best way store array of items with details in SQL table. I have a user account database. The user have multiple input fields to enter multiple details like:

___ : ______ +(get more multiple field field)

User can input any details like

Output1 : Output2
Mobile : 2455...
email : sdf
city : dfs
Other : sf

On an average a user will use around 20 options

Since the fields (mobile, email etc.) are not known to me, I have to store Output1 field with the answer field ( output2 ).

I will be having a very huge user base, so I think it's not better to create separate tables for each user.

Is there any way to store and get the details in limited or single column.

Since both the attribute name and value comes from users, a typical 3-table model of saving many-to-many relationship is a bit of overkill.

I would just kept users and their attributes in two separate tables:

+---------+-----------+--------------+
| user_id | user_name | user_email   |
+---------+-----------+--------------+
| 1001    | John      | john@doe.com |
+---------+-----------+--------------+
| 1002    | Tim       | tim@doe.com  |
+---------+-----------+--------------+

+----------+-----------+--------------+--------------+
| field_id | user_id   | field_name   | field_value  |
+----------+-----------+--------------+--------------+  
| 1        | 1001      | Option1      | Option2      |
+----------+-----------+--------------+--------------+
| 2        | 1001      | Mobile       | 2345656565   |
+----------+-----------+--------------+--------------+
| 3        | 1001      | city         | dfs          |
+----------+-----------+--------------+--------------+
| 4        | 1002      | Other        | something    |
+----------+-----------+--------------+--------------+

Possibly with some additional columns for sorting, tagging, etc.

I would use 3 tables.

Table 1 - user. PK is UserId. Other fields are name, rank, serial number, etc

Table 2 - Attribute - Primary key is AttributeId. Other Field is attribute name. Examples of attribute names are emailAddress, cellphoneNumber, cityName.

Table3 - UserAttribute. Primary Key is UserId and AttributeId. Other field is Attribute value.

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